   UDP questions
  I''ve been unable to find UDP tutorials or references with google, so I''ll ask here instead:Is there a good reference about the combination of c++, winsock and UDP?How to get the length of the data of an UDP package?Is there a special variable type for UDP packages to use instead of string or char*?how to print the contents of an UDP package on screen without it ending at a 0 character?If you open two games on the same computer, both using the same port to receive things, and you send something to that port, will both games receive it, or only one, and how can you know which one? 
  1. Tutorials:first, read this:http://www.madwizard.org/view.php?page=tutorials.networking.contents&lang=cppthen read msdn...2. recvfrom returns the size of the received datagram3. What do you mean "special variable" ? %)4. To print the contents of the UDP package you have to print its contents... ))) That means that only you know what your sent packets contain, so you need to treat them the same way on the receiving side5. Basically, you can't start two games which would listen for incoming datagrams on the same interface/port... The first game started will bind the address, and when the second attempts to bind its socket to the same address - it gets WSAEADDRINUSE ... Modern OSes are designed to assign a unique socket name to each socket in case you don't bind them explicitly... (A socket name is some entity, by which the OS and networking subsystem of the OS can identify a socket, this entity is usually ip address + port in TCP/IP - check msdn for sockaddr reference)   [edited by - roookie on February 3, 2004 8:43:20 AM];  you cant have two games/apps/sockets whatever recv on the same portwhen you call the recv function (i think) that it will tell you how many bytes were received...thus the length of the data in the udp packetthere is no special variable type....just cast it to char*MYSTRUCTURE instance;...send(socketid, (char*)&instance, sizeof(instance));..  you get the ideaprinting the contents depends on what the contents areif it is a structure like above then you need to just print out the member variables of that struct.there are good references because i have some of them printed off at home but i dont remember where i got them fromi will post them when i get home and find out "A soldier is a part of the 1% of the population that keeps the other 99% free"  - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA ;  I found these using google:this, this, and this. I hope one of them answers your questions.   EDIT:>> They don't seem to, after inspection  sorry.[ Galactic Conquest | Bananas | My dead site | www.sgi.com | Goegel ]   [edited by - Kurioes on February 3, 2004 8:48:00 AM];  Thanks for the answers The links look very good, thanks.With "special variable type" I mean a struct, a class, or whatever, to describe a package instead of a string or char*, because a char* has a certain length, and if you load your package into it, and want to get the length, you get the length of the char, and not of the package, right?And if I fprint the char*, it stops displaying it immediatly when it encounters a 0 char, even if there are still other chars after that one, and I want to see the whole package.Is there a way that's better than writing something myself that goes through it char by char?   [edited by - Boops on February 3, 2004 9:09:32 AM];  I got a working UDP module at http://www.structweb.com/?show=project&project=9 . It has a "few" bugs, but you should get a general idea of how to use UDP. It also has a few references to docs I used though I can''t tell you if there is an UDP doc in there.recvfrom returns the received packet length, or -1 if an error occured.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/recvfrom_2.aspTo send a packet you need to specify the location of the buffer in which you have the data to be sent, plus you need to give the length of the packet you are going to send.You probably make a protocol, and in this protocol you should define the structure and length of messages.For example, our connect4 project at school used a packet of 255 bytes, 4 bytes opcode, the rest data and we defined it with a struct.struct Message{  int opcode;  char data[251];};Then when sending:Message myMessage;myMessage.opcode = 100;  //IDENTIFICATION OPCODEmyMessage.data[0] = "H";myMessage.data[1] = "I";sendto(mysocket, &myMessage, sizeof(Message), etc etc...//send over the Message structureretrieving it is a *bit* more tricky...char buffer[MESSAGE_SIZE];int nBytes = recvfrom(serverSocket, buffer, MAX_MESSAGE_SIZE, 0, &from, &fromlen);Message* msg;msg = (Message*)buffer;// msg->opcode is 100// msg->data is "HI"Checking all data should be done with a debugger. Outputting binary data to the screen won''t help you much anyway as you can''t read it. If you can''t use a debugger you might want to write everything binary to a file and analyse it with a program that can display hex, which is a whole different story.Your last question: You can''t... a socket is bound to a port. Once this port is taken, you can not open a second socket on this same port.You game will tell you "UNABLE TO CONNECT TO SERVER" or if it''s a really smart game, it will open up a connection on another port.Anyway, trying to bind a second socket on the same port will cause an error on the bind() operation. ;  I''m slightly getting it more and more But here''s a tricky question: appearantly there exists something like a "Trailer" that can sometimes also be added to an UDP package, how do I send that with it? ;  Here''s my dump packet function. Not sure how bug free this is, but i used it. void DumpRawPacket(CRawPacket *rawpack){char buffer[10000];char *end=NULL;end=buffer;int linecounter=0;for (int i=0; i<rawpack->PayloadSize; i++) {if (linecounter==0)end += sprintf(end,"%04X: ",i);end += sprintf(end,"%02X ",rawpack->payload[i]);linecounter++;if (linecounter == 8)end += sprintf(end, "  ");if (linecounter == 16) {end += sprintf(end, "\n");log.Print("%s",buffer);buffer[0]=0;end=buffer;linecounter=0;}}if (linecounter) {end += sprintf(end, "\n");log.Print("%s",buffer);buffer[0]=0;end=buffer;linecounter=0;}//sprintf(end,"\n");}-=[ Megahertz ]=- 