Jump to content
  • Advertisement
Sign in to follow this  
kzar

A few networking questions

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been trying to learn about sockets programming in c and I have been confused about a few things while trying to make some programs. When you make a server you should use select instead of making the sockets non-blocking because apparently non-blocking checks use alot of CPU but say if I wanted to also look at keyboard input or do somthing else at the same time how would I do it? For instance if it was a mmorpg and I wanted npc's to move about and things. I read that when you make a server and you do a recieve it could be 1 message, 2 messages, part of a message or 1 and a bit. I am a bit confused though how I would handle this. Theres two programs I was trying to make and I think I need to do somthing differently for each, in one the first byte gives the messages length so you at least know how long the message is. But the other program would be a kind of telnet chat server, which would have to wait untill it recieves a '\n' and I have no idea how I would know how much of a buffer to allocate and things so that I could end up with a string that had the whole message in! And the last problem I am confused about I hit when trying to make a program to get a webpage. I can do the GET request and get some back and all, but without knowing how long the page is, how am I supposed to know how big a buffer to make! They are probably pretty stupid questions but they are really confusing me! Thanks

Share this post


Link to post
Share on other sites
Advertisement
why don't you know the lenght of the buffer ?! on each receive the recv function
return the number of received bytes so you can create dynamic memory buffers .
look at this code from msdn:

while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( ConnectSocket, recvbuf, 32, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
printf( "Bytes Recv: %ld\n", bytesRecv );
}
this code will completely get your GOT html page until it return 0,
suppose the page is entirly 64 byte then it loops 2 time .you should save buffer somewhere else for other processing .

Share this post


Link to post
Share on other sites
I kind of get the idea there but how would I put the whole thing into one string?

Share this post


Link to post
Share on other sites
I was thinking for the telnet chat one I could read a buffer of say 100, if I recieved 1 or more then I know its sent somthing and I need to loop through the buffer checking for '\n'. If I find a '\n' I can allocate space for all the characters up to and including the '\n' and copy it in, but If I don't find a '\n' then I need somewhere to store the message so far so I can use it later. I'm thinking I should maybe do a structure for each client socket that has the actual socket file descriptor thing but also space for an unfinished message. aahh confusing!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!