Is recv() a blocking function....as in it waits until data is received before returning? |
Yes, it is a blocking function. You can set a timeout for it using the
setsockopt
function and the
SO_RCVTIMEO
parameter.
http://msdn.microsoft.com/en-us/library/ms740476(v=vs.85).aspx
edit: maybe im using the recv function wrong on server side. Say you make a buffer string. when you call receive with buffer string as the second parameter does that mean that receive automatically stores the received data in the buffer |
Yes.
1 2 3 4 5 6 7 8 9 10
|
char buf[5];
//s is a SOCKET that has been created using the socket function
//and is ready to listen (either it has already called listen()/accept()
//on the server side or has called connect() on the client side)
int iResult = recv(s, buf, 5, 0);
//Here, buf will store iResult bytes received if iResult is greater
//than zero. Note that iResult may be less than 5
|
on client side to i have to continuosly connect to server before sending and receiving data or do i just connect once then call send() whenever i want to send data? |
If the server does not call
closesocket
on the
accept()
ed connection, then the client only needs to call
connect()
once. If the server does call closesocket, then the client needs to re-establish a connection with
connect()
as the socket has been invalidated.
As for your issues with non-blocking sockets, I haven't had any experiences with it besides the tutorials (which are incomplete). I thought at first, after a call to
WSAAsyncSelect
, Windows notifies the Window you passed in of each network event that occurs. I thought that, for instance, if you receive a
FD_CONNECT
event, then you should call
accept
in the event handler, if you receive a
FD_READ
event, then you should call
recv
in the event handler, etc. This may not be the case since I haven't found a complete tutorial on the subject yet.
Another plausible use (again I'm not sure of this), is you call
WSAAsyncSelect
. Then later, for instance call
recv
and your program continues in non blocking fashion. When the data is ready from that
recv
call,
FD_READ
is sent to your handler.
Again, I'm unsure as to how
WSAAsyncSelect
actually works (1. Do you call WSAAsyncSelect and then Windows just notifies your Window of incoming events? Or, 2. Do you call WSAAsyncSelect to set up a non-blocking socket, then you proceed as normal calling
recv
,
listen
,
accept
as normal except for the fact that they're now non blocking, with their respective events getting sent to your handler when the non-blocking calls are ready to actually be executed?) So, I won't be able to help you with asynchronous sockets until I actually figure out their guts. Is there anybody else with experience in this regard who can help?