|
-
October 10th, 2002, 06:26 AM
#1
Socket related query
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.
-
October 10th, 2002, 08:14 AM
#2
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.
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
October 10th, 2002, 08:25 AM
#3
yes i need the code pls send as soon as possible
-
October 10th, 2002, 08:30 AM
#4
-
October 10th, 2002, 08:46 AM
#5
I have post U private message with some code.
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
October 10th, 2002, 10:25 AM
#6
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...
Code:
int iOptval = 2000;
if(setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char *) &iOptval, sizeof(int)))
// Error -> call 'WSAGetLastError()'
-
October 10th, 2002, 10:51 AM
#7
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
}
}
Last edited by dimm_coder; October 10th, 2002 at 10:55 AM.
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
October 10th, 2002, 03:09 PM
#8
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... 
Just wanted to point out that there are different possibilities....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|