getting readable data w/winsock
is there a way i can convert the character arrays returned by recv() to ints/floats? also is there a function that returns the size of a datatype in bytes?
Quote:
Original post by jchmack
is there a way i can convert the character arrays returned by recv() to ints/floats?
also is there a function that returns the size of a datatype in bytes?
You can cast the individual elements to the datatype you want. Charaters can be treated like ints in some ways, you do arthmetic on them. For example the statement 'B' + 1 would give you 'C' or 67 in ASCII. You could also use a union.
the sizeof(); function should do what you want.
Patrick
char* buffer = recv( ... );
int some_int = *reinterpret_cast<int*>( buffer );
hopefully buffer is at least sizeof(int) in size <-- this answers your other question
it will interpret the first sizeof(int) bytes from that buffer and store it in some_int...
you might consider creating some kind of message object to deal with this kind of low-level interaction...
Actually, i'm happy to share an implementation that I wrote and have been using for a little while to interface with network code:
http://www.noidea128.org/sourcefiles/15490~
http://www.noidea128.org/sourcefiles/15491~
I don't advertise it to be 'bug-free', but it works quite well - every 2 weeks or so I find myself tweaking something small inside it.
int some_int = *reinterpret_cast<int*>( buffer );
hopefully buffer is at least sizeof(int) in size <-- this answers your other question
it will interpret the first sizeof(int) bytes from that buffer and store it in some_int...
you might consider creating some kind of message object to deal with this kind of low-level interaction...
Actually, i'm happy to share an implementation that I wrote and have been using for a little while to interface with network code:
http://www.noidea128.org/sourcefiles/15490~
http://www.noidea128.org/sourcefiles/15491~
I don't advertise it to be 'bug-free', but it works quite well - every 2 weeks or so I find myself tweaking something small inside it.
omg ty tons guys
also When i use the recieve()(recv()) function (in my tcp programs) and there is no data in the stream/socket my program pauses until it recieves a message. Is there a way i can check if there is data in the stream/socket so i can implement something like:
if(data)
{
recieve()
}
Thank you all in advance
also When i use the recieve()(recv()) function (in my tcp programs) and there is no data in the stream/socket my program pauses until it recieves a message. Is there a way i can check if there is data in the stream/socket so i can implement something like:
if(data)
{
recieve()
}
Thank you all in advance
There's a number of ways to not block on recv()
select() is one
recv() is another, if you make your socket non-blocking
then there are OS specific ways, too, but I try to stay portable
select() is one
recv() is another, if you make your socket non-blocking
then there are OS specific ways, too, but I try to stay portable
ok i tried to do this but it doesnt compile
char* buffer = recv(m_socket, recvbuf, sizeof(int), 0);
int some_int = *reinterpret_cast<int*>( buffer );
doesnt recv() return the number of bytes recieved?
c:\program files\microsoft visual studio\myprojects\game2\network.h(104) : error C2440: 'initializing' : cannot convert from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
char* buffer = recv(m_socket, recvbuf, sizeof(int), 0);
int some_int = *reinterpret_cast<int*>( buffer );
doesnt recv() return the number of bytes recieved?
c:\program files\microsoft visual studio\myprojects\game2\network.h(104) : error C2440: 'initializing' : cannot convert from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Sorry, I misspoke... recv() doesn't return a char*, you have to pass the buffer to recv() - it returns the number of bytes received.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement