![]() |
|
How to send and receive sockets with out waiting for a reply... |
Don Freeman
Member #5,110
October 2004
![]() |
How do you use sockets to send / receive data without waiting. I have a very basic knowledge of networking, and I can now finially send and receive data...but I have to wait for the data. This works fine in a turn based game, but what about real time gaming? I've never dealt with multi threading before...and all of my windows programming uses allegro and is all single threaded using lock step processing. I've looked at libnet...but can't get it to compile with my program. I keep getting link errors about default libs...and when I ignore default libs...even more link errors. So...I've just started playing with WinSock. Also...how to keep from waiting for a client to join, or at least allow for additional processing to happen while I wait for a client. And what about if you run client before server...you get an error! How do you correct this? I can't guarranty that the server will always be ran first! I've included the (crappy) source for the client and server below:
Client:
Thanks in advance. -- |
kazzmir
Member #1,786
December 2001
![]() |
In liue of someone posting something more helpful look up the 'select' function. Basically it lets you poll the sockets for information and if theres anything there do something with it. If not move on. |
ReyBrujo
Moderator
January 2001
![]() |
Check this (the whole thread is interesting too). You have two ways, marking the socket as asynchronic or using select to multiplex the socket. -- |
BAF
Member #2,981
December 2002
![]() |
Check into nonblocking socket I/O, and MSG_DONTWAIT for recv(). |
Myrdos
Member #1,772
December 2001
|
This is how I do it: int blockParam = 1;//used to specify a non-blocking socket if (ioctlsocket(winSocket, FIONBIO, (u_long FAR*)&blockParam) != 0) // use WSAGetLastError() to figure out what went wrong else //it's all good And now you need to check if your send or receive would have blocked, and distinguish that case from "real" errors. result = send(winSocket, (char*)message, messageSize, 0); if (result == SOCKET_ERROR) { winsockCode = WSAGetLastError(); if ((winsockCode != WSAEWOULDBLOCK) && (winsockCode != WSAENOBUFS)) //we hit some socket error, probably best to close the socket else //couldn't send anything this time, try again later }//if result
result = recv(winSocket, (char*)buffer, dyBufferSize, 0); if (result == SOCKET_ERROR) { winsockCode = WSAGetLastError(); if ((winsockCode != WSAEWOULDBLOCK) && (winsockCode != WSAEMSGSIZE)) //hit an error else //just need to wait, nothing received this time }//if result
__________________________________________________ |
Don Freeman
Member #5,110
October 2004
![]() |
Ok...I'm using 'select' right now. It lets me tell when a socket is ready to send or receive data...so I can call them without having to wait! Now...how do I allow for other processing or a timeout for connection requests?!? Can 'select' do this as well? I don't want someone to click host game and then.................wait for eternity for someone to join or not! I like how DirectPlay connections work (or at least how they look and feel, don't know about the programming aspect). I don't want to turn to DirectPlay...portability issues. Thanks for all the excellent advice and help so far... -- |
Frank Drebin
Member #2,987
December 2002
![]() |
Quote: Can 'select' do this as well?
yes. |
ReyBrujo
Moderator
January 2001
![]() |
Check my example. You put the listening socket in as ever. However, when one of the sockets is set, you first check the listening socket to see if it is the one that is ready. If so, you process the accept call; otherwise you know it is one of the connected sockets that sent some data. -- |
Don Freeman
Member #5,110
October 2004
![]() |
Thanks! That's kinda what I thought...just wanted to make sure that if I tried it...it was my fault if I did it wrong!
Thanks again all... -- |
|