Re: Wait on a server socket for 10 seconds



On Jun 18, 3:44 pm, "silverburgh.me...@xxxxxxxxx"
<silverburgh.me...@xxxxxxxxx> wrote:
Hi,

I read the following code which open a server socket for client
request.
However, i would like to know how can I change it so that i just
listen for client requestfor 10 seconds, after that, it bows out?

Code:

// Create socket for listening for client connection requests.
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
std::cout << "cannot create listen socket";
return;
}

// Bind listen socket to listen port. First set various fields in
// the serverAddress structure, then call bind().
// htonl() and htons() convert long integers and short integers
// (respectively) from host byte order (on x86 this is Least
// Significant Byte first) to network byte order (Most Significant
// Byte first).
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(listenPort);

if (bind(listenSocket,
(struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0) {
std::cout << "cannot bind socket";
return;
}

// Wait for connections from clients.
// This is a non-blocking call; i.e., it registers this program with
// the system as expecting connections on this socket, and then
// this thread of execution continues on.
listen(listenSocket, 10);

int count = 0;

while (1) {
std::cout << "Waiting for TCP connection on port " << listenPort
<< " ...\n";

// Accept a connection with a client that is requesting one. The
// accept() call is a blocking call; i.e., this thread of
// execution stops until a connection comes in.
// connectSocket is a new socket that the system provides,
// separate from listenSocket. We *could* accept more
// connections on listenSocket, before connectSocket is closed,
// but this program doesn't do that.
clientAddressLength = sizeof(clientAddress);
connectSocket = accept(listenSocket,
(struct sockaddr *) &clientAddress,
&clientAddressLength);
if (connectSocket < 0) {
std::cout << "cannot accept connection ";
return;
}

Use select() to do the timeout is a good way:

int reliable_recvfrom(int fd, void *data, int size, int flags,
struct sockaddr *from, socklen_t *fromlen, int policy, int timeout)
{
fd_set set;
struct timeval tv;
int retval;
int count = 0;
time_t start_time;

if (timeout <= 0) {
return RELIABLE_ERR_PARAM;
}

#if 1
set_fl(fd, O_NONBLOCK);
#endif

start_time = time(NULL);
while (count < size) {
int sel_ret;

FD_ZERO(&set);
FD_SET(fd, &set);

tv.tv_sec = start_time + timeout - time(NULL);
tv.tv_usec = 0;

if (tv.tv_sec <= 0) {
retval = RELIABLE_ERR_TIMEOUT;
goto err_return;
}

printf("before select with timeout %d, %d\n", tv.tv_sec,
tv.tv_usec);
sel_ret = select(fd + 1, &set, NULL, NULL, &tv);

if (sel_ret < 0) {
/* maybe be interrupt by signal many times,
* but we don't care this now...
*/
perror("reliable_recvfrom select error.\n");
continue;
} else if (sel_ret == 0) {
retval = RELIABLE_ERR_TIMEOUT;
goto err_return;
}

if (FD_ISSET(fd, &set)) {
int data_ret;

data_ret = recvfrom(fd, ((char *)data) + count,
size - count, flags, from, fromlen);
if (data_ret < 0) {
perror("recvfrom error.\n");
retval = RELIABLE_ERR_RW;
goto err_return;
}
count += data_ret;
if (policy == RELIABLE_POLICY_ONCE) {
return data_ret;
} else {
continue;
}
}
}

retval = RELIABLE_ERR_NONE;
err_return:
return retval;
}


.



Relevant Pages