Advertisement

sending text messages

Started by November 27, 2000 03:15 PM
13 comments, last by djsteffey 23 years, 11 months ago
@ncsu: I don''t know jackshit about the APIs involved in networking, but I do know that your previous suggestion (splitting up the 70-byte text message into 4 packets) is a Very Bad Idea(tm). Why? Because every package you send will use up an additional header - 20 bytes I think). So you''d actually send 70 bytes of data plus 4 x 20 bytes of headers over the network. It''s definitely worth figuring out how to obtain the packet length on the receiver.

cu,
Prefect
Widelands - laid back, free software strategy
As you can see the 2 philosphies of packet transmission. The streamming method and the struct casting method. The struct casting method makes it easier to go from data -> packet -> data however it makes it very difficult to dynamicly read in packets of varying sizes. I would go with the streamming method, as the added flexiblity and some simple optimzaitons makes it better than the struct method in almost everyway. You can negoiate the packet protocol at startup and avoid the overhead of sending the size field for each packet which are fixed in size throughout the games lifetime. Using the size fields only for packets which really need it (messages, etc.). It will save you a significant amount of protocol overhead if you send many packets/sec.

Good Luck

-ddn
Advertisement
Actually my method is quite simple with Dplay. I can actually use it to compress the map (which varies based on data in it) and send it across in 16 sections. Despite not knowing exactly how many bytes were sent in any of the 16 packets I can flawlessly read it back on the other side because I know how to read it. You just have to keep track of algorithms used.

But really it depends on what you best understand how to use. And your preference. Sending a set amount of data everytime is fine. I actually do that for certain packets. An action packet always contains 7 bytes. And the order of those bytes is always the same.

Ben
I think we''re quite obviously gonna need some code here.
This doesn''t use structs, but expains how simple it is anyway...

  #define MAX_PACKET_LEN 64000 // about 64k for UDP datagramschar buffer[MAX_PACKET_LEN];int text_message_length;char text_message_buffer[MAX_PACKET_LEN];// store datarecv(socket, buffer, 64000, 0);// check packet type (first byte)switch (buffer[0]){case TEXT_MESSAGE: // you don''t even really need this... text_message_length = *((int*)&buffer[1]);  // get the text strcpy(text_message_buffer, &buffer[1 + sizeof(int)]; break;// other packets}  


In fact, here''s a simple version with structs:

  #define MAX_PACKET_LEN 64000 // max size of packet#define MAX_DATA_LEN 63995 // minus header (i think correct)typedef struct packetType{ char type; int length; char data[MAX_DATA_LEN];} packetType;// ...packetType *incoming = new packetType;recv(socket, (char *)incoming, sizeof(packetType), 0);switch (incoming->type){case TEXT_MESSAGE: // already done! incoming->length contains length // and incoming->data contains text! break;// other packet types...}delete incoming;  


Easier to explain with code
Yup. Minus the length variable in my code. I just use the first byte of the data array since most messages would be wasting a byte otherwise.

Ben

This topic is closed to new replies.

Advertisement