Advertisement

Sending Integers

Started by October 04, 2002 02:01 PM
34 comments, last by BioSquirrel 22 years, 4 months ago
A while ago, I wanted to be able to make a game send integers (like player positions) with UDP. I wanted to be able to broadcast from a server so I used this example code: void UDPBroadcast() { SOCKADDR_IN addr_Broadcast; // Broadcast Adress in Socks Format SOCKET sock_Send; // sending socket char szMessage[256]; // message to send int val = 1; // I don''t know exactly // it''s used as an option in setsockopt() short destport; // destination port // Prepare the message sprintf(szMessage, "This is a UDP broadcast message."); // Get the port to send to printf("Enter destport: "); cin >> destport; // Create UDP socket if ((sock_Send = socket(AF_INET, SOCK_DGRAM, 0)) < 0) printf("Error: socket() failed."); // Fill in target addr (for a broadcast message) memset((char *) &addr_Broadcast, 0, sizeof(addr_Broadcast)); addr_Broadcast.sin_family = AF_INET; addr_Broadcast.sin_addr.s_addr = htonl(INADDR_BROADCAST); addr_Broadcast.sin_port = htons(destport); // Allow socket to broadcast if (setsockopt(sock_Send, SOL_SOCKET, SO_BROADCAST, (char*)&val, sizeof(int)) == SOCKET_ERROR) printf("Error: setsockopt() failed."); // Send it if (sendto(sock_Send, szMessage, 256, 0, (sockaddr*)&addr_Broadcast, sizeof(addr_Broadcast)) == SOCKET_ERROR) printf("Error: sendto() failed."); closesocket(sock_Send); // close the socket } When I tried sending integers this way by replacing szMessage with an integer, people told me I was sending it the wrong way. How would I broadcast an integer using this code?
This is just a question but... What''s the point to send an integer beside of a number in a char[] for example ?
Advertisement
What do you mean?
quote:
Original post by BioSquirrel
When I tried sending integers this way people told me I was sending it the wrong way.


They probably meant that you are supposed to send it in network byte order - that is, converting it using htonl() or htons() before sending. If you do not, it'll still work as long as both the server and client use the same endianness, but using network byte order is the standard way to avoid this particular endian issue.

[edited by - spock on October 4, 2002 4:50:41 PM]
Yes, I think I remember that was the problem.
Why do you send let''s say "int i = 10" rather then a string buffer containing "10" ?
Advertisement
OK I think I almost have it working. One error... when I use this send command:

if (sendto(sock_Send, Player1.xpos, 256, 0, (sockaddr*)&addr_Dest, sizeof(addr_Dest)) == SOCKET_ERROR)

...it says:

''sendto'' : cannot convert parameter 2 from ''int'' to ''const char *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
The second sendto() parameter should be a pointer to the data you want to send. Take the address of the integer and cast as necessary.

[edited by - spock on October 4, 2002 5:50:18 PM]
quote:
Original post by Anonymous Poster
Why do you send let''s say "int i = 10" rather then a string buffer containing "10" ?


Becase BioSquirrel doesn''t want to send a string, he wants to send an integer. It would be kind of silly to convert it to a string, send it and then have to convert it back to an integer again on the other side, wouldn''t it?
And how do you know the type of data that is coming through the network ? Won''t it be read like a buffer ?

This topic is closed to new replies.

Advertisement