Advertisement

Sending Integers

Started by October 04, 2002 02:01 PM
34 comments, last by BioSquirrel 22 years, 4 months ago
The kind of data coming through the network is specified by whatever protocol you are using - quite possibly something you made up yourself, if you are writing both the server and client...

[edited by - spock on October 4, 2002 6:03:15 PM]
Still confused. The sample program works and I don't see a pointer to szMessage.

int *Player1xpos;
Player1xpos = &Player1.xpos

if (sendto(sock_Send, Player1xpos, (sockaddr*)&addr_Dest, sizeof(addr_Dest)) == SOCKET_ERROR)
printf("Error: sendto() failed.");


This doesn't work.

[edited by - BioSquirrel on October 4, 2002 6:16:29 PM]
Advertisement
That's because szMessage is already a pointer (to the first element in the array - that's how arrays work in C).

Hint:

if (sendto(sock_Send, (const char*)&Player1.xpos,sizeof(Player1.xpos), ...


[edited by - spock on October 4, 2002 6:31:02 PM]
Almost have it. The only problem I have is getting the integer into a const char.
int iInt = 10;
char *pInt = (char *)&iInt

This of course doesn''t work if winsock or whatever expects a null terminator.
Well, it accepts that and the program actually works now...just have to test it.
Advertisement
sendto() doesn't expect nor need a terminator (you specify the size of the data in the third parameter, after all) so all you need to do is cast the pointer so that it will compile. You don't need a separate pointer variable - see my hint above.

I'm not sure why the second parameter is prototyped as a const char* in winsock - it really should be const void* because it's a pointer to data of unspecified type.

[edited by - spock on October 4, 2002 6:49:58 PM]
quote:
Original post by spock
I''m not sure why the second parameter is prototyped as a const char* in winsock - it really should be const void* because it''s a pointer to data of unspecified type.


It''s because the words char and byte are basically interchangeable since a char requires one byte of memory. It''s more sensible to think about data as a series of bytes (or chars) rather than a series of unknowns (voids).
________________________________________________"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
Argh...now when I try to do the same thing with the receiving end, it says it cannot convert from const char * to char *
Are you doing something similar to this?

int receivedInteger;

recvfrom(socket, (char *)&receivedInteger, sizeof(int), ...);


With some degree of error checking, of course.
________________________________________________"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

This topic is closed to new replies.

Advertisement