Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

does enet can send and receive struct?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 hsine   Members   -  Reputation: 112


Posted 09 January 2013 - 09:57 AM

client code like this :


</p><div>   typedef struct _T{</div>
<div>        int id;</div>
<div>        char* testdata;</div>
<div>    }T, *PT;</div>
<div>    PT pt;</div>
<div>    memset(pt,0,sizeof(&pt));</div>
<div>    pt->id=1;</div>
<div>    pt->testdata="test datya";</div>
<div>    char pack[128];</div>
<div>    memcpy(&pack,pt,sizeof(&pt));    ////////////////////////////  pack got nothing???</div>
<div>    ENetPacket *packet=enet_packet_create((const void*)&pack,128,ENET_PACKET_FLAG_RELIABLE);</div>
<div>    if(NULL == packet){</div>
<div>        fprintf(stderr,"create packet with error\n");</div>
<div>        exit(EXIT_FAILURE);</div>
<div>    }</div>
<div> </div>
<div>    enet_peer_send(peer,0,packet);</div>
<div> </div>
<div>    enet_host_flush(client);</div>
but the server can';t  get anything ...? what wrong with it?

Edited by rip-off, 10 January 2013 - 10:31 AM.


#2 Bacterius   Crossbones+   -  Reputation: 10560


Posted 09 January 2013 - 11:03 PM

Your use of pointers is wrong. "pack" is already a pointer, being an array. Also, writing "PT pt" and later on "pt->id = 1" indicates you typedef'd a pointer or array type to a non-pointer type, which is bad because it makes it hard to see what is a pointer and what isn't. I strongly recommend you rewrite the code without writing any * (asterisk) in your typedef's. Then, fix the code to properly pass to memcpy and memset what they need. In any case, assuming there are no other bugs, your immediate fix is:







But the code is not easy to follow for the reason I mentioned above, and you should rewrite it if you wish to keep your sanity.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.


- Pessimal Algorithms and Simplexity Analysis

#3 RulerOfNothing   Members   -  Reputation: 1273


Posted 10 January 2013 - 12:45 AM

Actually, sizeof(&pt) takes the size of a pointer, so that code will only copy 4 or 8 bytes depending on the platform. You want memcpy(pack,pt,sizeof(*pt)); or memcpy(pack,pt,sizeof(T));

#4 hsine   Members   -  Reputation: 112


Posted 10 January 2013 - 10:00 AM

after i  fix the code ,but  i still can't send struct to remote server ? why ?

#5 rip-off   Moderators   -  Reputation: 9508


Posted 10 January 2013 - 10:30 AM

First note, the intermediate buffer is unnecessary. You could just pass the address of the structure to enet to initialise the packet (obviously adjusting the size parameter you are passing too.


However, your structure contains a pointer. A pointer sent over the network is meaningless. One option is to change to a fixed length array, something like this:

const int MaxPacketStringLength = 100;
</p><div>struct Packet {</div>
<div>        int id;</div>
<div>        char data[MaxPacketStringLength];</div>

This has two major drawbacks, a naive client will send too much data for short strings, and it also limits the maximum size of the data.


Another way is to serialise the structure into a contiguous byte array. This is possibly what a ENetPacket is for.  Enet might provide functions to help you do this. Another point is that you need to also handle endianness. Again, ENet might provide helpers, or simply expose the usual hton*() and ntoh*() functions.

#6 rip-off   Moderators   -  Reputation: 9508


Posted 10 January 2013 - 10:35 AM

Sometimes it can help if you post your current code after making changes. As I mentioned before, use [code] tags , or the WYSIWYG functionality in the editor to insert pretty code blocks, like this:


int main() {

    printf("Hello, world");



#7 Servant of the Lord   Crossbones+   -  Reputation: 24051


Posted 10 January 2013 - 01:23 PM

My experience is limited in this area, but wouldn't memcpy()ing a struct into bytes cause problems because of alignment? Couldn't the struct by packed different (variables in the same order, but extra padding between them for performance purposes) on the server and the client, possibly writing and reading too little or too many bytes?


Is the packing of a struct standardized by C++ for POD types?

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]

#8 rip-off   Moderators   -  Reputation: 9508


Posted 11 January 2013 - 04:03 AM

Good point, packing must be taken into consideration too. I actually meant memberwise serialisation of the structure, with "smart" handling of dynamically sized members such as strings and containers.


Is the packing of a struct standardized by C++ for POD types?

The OP appears to be using C, (potentially via a C++ compiler). That said, I believe neither language guarantees any sort of standard packing behaviour.

Edited by rip-off, 11 January 2013 - 04:05 AM.

#9 Ravyne   GDNet+   -  Reputation: 10419


Posted 11 January 2013 - 04:41 AM

Packing and also endianess can be a problem. Explicit serialization of some kind is the most robust solution, IMO.

throw table_exception("(ノ ゜Д゜)ノ ︵ ┻━┻");

#10 Zaoshi Kaba   Crossbones+   -  Reputation: 5221


Posted 11 January 2013 - 04:50 AM

Either I'm retarded when it comes to this post, or code in OP is wrong on way too many levels:

line 05.: variable is a pointer pointing to invalid memory address;

line 06.: memset'ing inaccessible memory and invalid size (or at least illogical);

line 07.: dereferencing invalid pointer;

line 08.: dereferencing invalid pointer and you cannot assign string like that, did this code even compile?;
line 10.: memcpy will change pointer (the one on the stack), that's not what OP wants, also size is incorrect;

line 11.: sending invalid data (stack pointer) and line 10 didn't actually copy any string, again not what OP expects;


I hope I'm not wrong anywhere, seems way too fishy.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.