On Jun 18, 9:52 am, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
Quote:
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;
}
>
Thank you.
Hi
Though this is not a part of C programming, still I am going since i
have worked some stuff like this.
For the functions u are using u need to use Unix APIs like socket.h
and fcntl.h
As the comments in this code specify that the accept() is a blocking
mode function, so u need to call in in unblockking mode(Sorry if the
terminology I am using is not very precise).
For that take the flags in some variable stat and flip the flags to
non blocking mode
stat = fcntl(listenSocket, F_GETFL, NULL);
stat |= O_NONBLOCK;
fcntl(listenSocket, F_SETFL, stat);
Start a loop that works for 10seconds.
call the function accept(). If there is nothing which accept() can
work upon , it goes to the next statement. So, u can continue with
this for 10 seconds, apply the condition with accept() that if
something is returned by accept(), break the loop.
After the loop completes donot forget to flip back the flags to
blocking mode.
stat = fcntl(listenSocket, F_GETFL, NULL);
stat = (~O_NONBLOCK);
fcntl(listenSocket, F_SETFL, stat);
This worked for me!!!
Thanks
Aditya