Sign in to follow this  
jchmack

getting readable data w/winsock

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sorry, I misspoke... recv() doesn't return a char*, you have to pass the buffer to recv() - it returns the number of bytes received.

Share this post


Link to post
Share on other sites
Quote:

recv() is another, if you make your socket non-blocking


so if i make my socket non-blocking the program will jut run past the recv() if there is no data?

how do i do this?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you can pass other things besides chars, but remeber its a pointer your working with, and a char is 1 byte (8 bits) wide (in most compilers)

so for instance
int a=100;
send(socket,&a,sizeof(int));

would work because an integer is 32 bits, or 4 bytes big (on most compilers)
so give it the pointer and size and it will work

you may have to get familiar with memcpy, for instance

char *temp=malloc(6);
memset(temp,0,2+sizeof(int)); //we allocated 6 bytes 2 chars and 4 bytes for int
temp[0]=*"a"; //something usefull
int a=234;
memcpy(temp+1,&a,sizeof(int)); //copy the int into our char string
temp[5]=20; //something else useful

send(socket,temp,2+sizeof(int));
free(temp);

thats how you throw in lots of data and send it
hope that helps

remember pointers are our friends






Share this post


Link to post
Share on other sites
Guest Anonymous Poster
or

char *temp=malloc(6)
memset(temp,0,6);
int a;
recv(socket,temp,6);
memcopy(&a,temp+1,4);

now you have int a filled and the temp[0] and temp[5] values recieved

i may have the exact syntax off a little bit

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if(Select(....)){

if there is nothing waiting in the specified socket this never happens, and never blocks

recv(...)
}

check up on msdn for select and recv etc more the exact details

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this