Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Listen a socket for client request for 10 seconds

Reply
Thread Tools

Listen a socket for client request for 10 seconds

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      06-18-2007

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.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-18-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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;
> }

[snip]

That's C++. Why did you post to comp.lang.c?

But before you post to comp.lang.c++, you need to be aware that
sockets are not defined by either the C language or the C++ language.
Try a newsgroup that deals with your operating system, most likely
comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Aditya
Guest
Posts: n/a
 
      06-19-2007
On Jun 18, 9:52 am, "(E-Mail Removed)"
<(E-Mail Removed)> 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;
> }
>
> 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



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-19-2007
Aditya said:

<snip>

> 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


Not necessarily true, and in fact rather misleading.

The OP would do better to take this up in comp.unix.programmer where he
will get expert advice.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Aditya
Guest
Posts: n/a
 
      06-19-2007
On Jun 19, 1:07 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> Aditya said:
>
> <snip>
>
> > 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

>
> Not necessarily true, and in fact rather misleading.
>
> The OP would do better to take this up in comp.unix.programmer where he
> will get expert advice.
>
> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
> email: rjh at the above domain, - www.


Hi Richard
Sorry if was wrong... I am just a newbie. But please do tell me if I
am misleading. What is the place I am wrong?


Thanks
Aditya

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-19-2007
Aditya wrote:
> On Jun 19, 1:07 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> Aditya said:
>>
>> <snip>
>>
>>> 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

>> Not necessarily true, and in fact rather misleading.
>>
>> The OP would do better to take this up in comp.unix.programmer where he
>> will get expert advice.
>>

>
> Hi Richard
> Sorry if was wrong... I am just a newbie. But please do tell me if I
> am misleading. What is the place I am wrong?
>

The advice and the reason (what do you do when accept returns? How to
you manage the timeout?) are both off topic! The best advice is to move
the question to comp.unix.programmer.

One more thing, please please don't use silly abbreviations like "u",
they make you post hard to read.

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-19-2007
Aditya said:

> On Jun 19, 1:07 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> Aditya said:
>>
>> <snip>
>>
>> > 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

>>
>> Not necessarily true, and in fact rather misleading.
>>
>> The OP would do better to take this up in comp.unix.programmer where
>> he will get expert advice.

>
> Sorry if was wrong... I am just a newbie. But please do tell me if I
> am misleading. What is the place I am wrong?


One of the mistakes you made was in trying to offer Unix programming
advice in a newsgroup where it is not topical and therefore where any
mistakes you make risk not being picked up.

To learn about the /other/ mistake you made, ask in
comp.unix.programmer, since it's not topical here in comp.lang.c.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-19-2007
Aditya wrote:
>

.... snip ...
>
> 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


u hasn't been on this newsgroup for some time. His name is
singular, so you should use the verb 'is', not are.

I.e. don't use silly geekspeak on Usenet. It is NOT cool.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert Seconds to X Hours Y Minutes Z Seconds? 00_CP_D12 ASP .Net 3 02-22-2008 10:37 AM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Convert seconds to minutes and seconds tshad ASP .Net 7 03-11-2005 11:27 PM
Converting seconds to (Days, Hours, Minutes, seconds) Stu C Programming 7 03-07-2005 08:44 AM
Bad ServerSocket ! Listen! Listen! Listen! Donny Java 13 08-05-2004 06:51 PM



Advertisments