Click to See Complete Forum and Search --> : Socket related query


smart
October 10th, 2002, 06:26 AM
in my client/server socket application

after establishing the connection bitween client socket and server socket.

server socket waits for client to send some data. it stays there untill client sends some thing like

//iReceived = recv (clntInfo->lSocket, clntInfo->rcvbuff, 100, 0);

it will not step to next statement.

what i have to do is ... i have to wait for 1 min and if client doesen't send anything in this time period i have to close the socket.
how to do this... i am programming in C
its very urgent.

dimm_coder
October 10th, 2002, 08:14 AM
use select() function.
It's last parameter is a struct timeval and U can set it for maximum time U need to wait.
Read about it in doc's U have. If U haven't doc's write me ( here or pm ) and I ' ll provide U some code.

smart
October 10th, 2002, 08:25 AM
yes i need the code pls send as soon as possible

smart
October 10th, 2002, 08:30 AM
yes i need the code

dimm_coder
October 10th, 2002, 08:46 AM
I have post U private message with some code.

Andreas Masur
October 10th, 2002, 10:25 AM
Originally posted by dimm_coder
use select() function.
It's last parameter is a struct timeval and U can set it for maximum time U need to wait.
Read about it in doc's U have. If U haven't doc's write me ( here or pm ) and I ' ll provide U some code.
The timeout you pass to select is only the timeout for the select function itself. It does not influence 'recv()' directly. Of course if you use 'select()' to determine whether there is a socket available for receive you indirectly influence 'recv()' as well.

Nevertheless if you want to set the timeout for the 'recv()' function itself you can set a socket options...

int iOptval = 2000;

if(setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char *) &iOptval, sizeof(int)))
// Error -> call 'WSAGetLastError()'

dimm_coder
October 10th, 2002, 10:51 AM
Andreas, of caurse, I mean that U mast call recv() next.

I mean next:

//
// that only part from my code.. without some adaptance
//
fd_set fdRead;
int nRet;
char buf[1024];

// sock - it's your socket with client
// U may also add another to

FD_ZERO( &fdRead );
FD_SET( sock, &fdRead );

struct timeval; // TIMEVAL for Win to
timeval.tv_sec = 60; // for U

// wait not more than 60 sec.
nRet = select( 0, &fdRead, NULL, NULL, &timeval );
if ( SOCKET_ERROR == nRet )
{
break;
}
if ( nRet > 0 )
{
// if client have send data
if ( FD_ISSET( sock, &fdRead ))
{
nRet = recv( sock, buf, sizeof(buf), 0 );

if ( nRet == 0 || SOCKET_ERROR == nRet )
{
bIsError = true;
}

// process your data
}
else
{ // client haven't send data
}
}

Andreas Masur
October 10th, 2002, 03:09 PM
Originally posted by dimm_coder
Andreas, of caurse, I mean that U mast call recv() next.

Yes..that is the common way of doing it...I did it too all the time... :cool:

Just wanted to point out that there are different possibilities....