check requested socket connection

check requested socket connection

Post by Markus Fischbache » Fri, 12 Oct 2001 21:16:56



hi all!

how to get the information that someone want to connect to my socket.
when calling accept the function block until a connect appear (i will not
use nonblocking sockets).
id like to check if someone request a connection before calling accept.
ive tried to use select, but select return allways a "ready
filedescriptor", so the timeout of 5 seconds has no affect.
im sure that nobody try to connect.
eg:

int hSocket;    // Sockethandle
.
.
some code for create, bind, listen...
.
.
fd_set fd;
FD_ZERO (&fd);
FD_SET (hSocket, &fd);
timeval tv = { 5, 0 };
select (hSocket + 1, &fd, NULL, NULL, &tv);        // allways return 1 ->
timeout of 5 seconds has no affect!

why?
thx in advance for help!

markus

 
 
 

check requested socket connection

Post by Joe Halpi » Fri, 12 Oct 2001 22:32:55



> hi all!

> how to get the information that someone want to connect to my socket.
> when calling accept the function block until a connect appear (i will not
> use nonblocking sockets).
> id like to check if someone request a connection before calling accept.
> ive tried to use select, but select return allways a "ready
> filedescriptor", so the timeout of 5 seconds has no affect.
> im sure that nobody try to connect.
> eg:

> int hSocket;    // Sockethandle
> .
> .
> some code for create, bind, listen...
> .
> .
> fd_set fd;
> FD_ZERO (&fd);
> FD_SET (hSocket, &fd);
> timeval tv = { 5, 0 };
> select (hSocket + 1, &fd, NULL, NULL, &tv);        // allways return 1 ->
> timeout of 5 seconds has no affect!

> why?

Because select() doesn't tell you when someone is trying to connect,
only that a read() won't block. And, in fact, if you try to read from
a listening socket, it won't block, it will immediately return -1 with
errno indicating that the socket is not connected (which it's not).

Put the descriptor in the write mask instead, and wait for that to
come active, then call accept().

Joe
--
Remember Flight 93

 
 
 

check requested socket connection

Post by Markus Fischbache » Fri, 12 Oct 2001 23:56:27


put the descriptor to write mask has no effect.
its the same like before.
are special socket options necessary?

i use:

int hSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

struct sockaddr_in adr;
memset (&adr, 0, sizeof (adr));
adr.sin_family = AF_INET;
adr.sin_port = htons (5600 /* some port */);

int optval = 1;
setsockopt(hSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&optval ,sizeof(int));
setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&optval ,sizeof(int));
bind(hSocket, psa, sizeof(sockaddr))

listen(hSocket, 10 /* max. connects */)

markus




> > hi all!

> > how to get the information that someone want to connect to my socket.
> > when calling accept the function block until a connect appear (i will
not
> > use nonblocking sockets).
> > id like to check if someone request a connection before calling accept.
> > ive tried to use select, but select return allways a "ready
> > filedescriptor", so the timeout of 5 seconds has no affect.
> > im sure that nobody try to connect.
> > eg:

> > int hSocket;    // Sockethandle
> > .
> > .
> > some code for create, bind, listen...
> > .
> > .
> > fd_set fd;
> > FD_ZERO (&fd);
> > FD_SET (hSocket, &fd);
> > timeval tv = { 5, 0 };
> > select (hSocket + 1, &fd, NULL, NULL, &tv);        // allways return
1 ->
> > timeout of 5 seconds has no affect!

> > why?

> Because select() doesn't tell you when someone is trying to connect,
> only that a read() won't block. And, in fact, if you try to read from
> a listening socket, it won't block, it will immediately return -1 with
> errno indicating that the socket is not connected (which it's not).

> Put the descriptor in the write mask instead, and wait for that to
> come active, then call accept().

> Joe
> --
> Remember Flight 93

 
 
 

check requested socket connection

Post by Joe Halpi » Sat, 13 Oct 2001 00:24:02



> put the descriptor to write mask has no effect.  its the same like
> before.  are special socket options necessary?

> i use:

> int hSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

> struct sockaddr_in adr;
> memset (&adr, 0, sizeof (adr));
> adr.sin_family = AF_INET;
> adr.sin_port = htons (5600 /* some port */);

> int optval = 1;
> setsockopt(hSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&optval ,sizeof(int));
> setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&optval ,sizeof(int));
> bind(hSocket, psa, sizeof(sockaddr))

> listen(hSocket, 10 /* max. connects */)

There are a few errors in here, which will not compile, so I'm not
sure what code you're actually using, but this works for me.

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>

int main()
{
  int hSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

  struct sockaddr_in adr;
  memset (&adr, 0, sizeof (adr));
  adr.sin_family = AF_INET;
  adr.sin_port = htons (5600 /* some port */);

  int optval = 1;
  setsockopt(hSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&optval ,sizeof(int));
  setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&optval ,sizeof(int));
  bind(hSocket, (const sockaddr*)&adr, sizeof(sockaddr));

  listen(hSocket, 10 /* max. connects */);
  fd_set fd;
  FD_ZERO (&fd);
  FD_SET (hSocket, &fd);
  timeval tv = { 5, 0 };
  select (hSocket + 1, NULL, &fd, NULL, &tv);

  return 0;

Quote:}

Joe
--
Remember Flight 93
 
 
 

check requested socket connection

Post by Markus Fischbache » Sat, 13 Oct 2001 01:16:35


Which compiler and flags do you use?
My application is multithreaded... to big for posting...

Short description:
Ive a thread listening for new connects.
In an infinite loop, i check with select() if there are possible clients
willing connect.
There are about 7 client which will connect.
In this time, select() returns 1.
Processor usage is 0%.
After this 7 clients are connected and the select() timed out once, i get a
SIGALRM!? But why?
First i have not catched the signal so my process exited. Now ive defined a
signalhandler for this sig. -> process run.
Now the select() function always return immediate with 1.
The processor usage increase very fast up to 80-90%.
The result -> process cores.
Is it necessary to "reset" the descriptor like an event?

my system:
unixware 7.1.1 with gnu 2.95.2pl1

ps: the code ive wrote below are only fragments, not for compiling :)




> > put the descriptor to write mask has no effect.  its the same like
> > before.  are special socket options necessary?

> > i use:

> > int hSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

> > struct sockaddr_in adr;
> > memset (&adr, 0, sizeof (adr));
> > adr.sin_family = AF_INET;
> > adr.sin_port = htons (5600 /* some port */);

> > int optval = 1;
> > setsockopt(hSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&optval
,sizeof(int));
> > setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&optval
,sizeof(int));
> > bind(hSocket, psa, sizeof(sockaddr))

> > listen(hSocket, 10 /* max. connects */)

> There are a few errors in here, which will not compile, so I'm not
> sure what code you're actually using, but this works for me.

> #include <stdlib.h>
> #include <errno.h>
> #include <unistd.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <strings.h>

> int main()
> {
>   int hSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

>   struct sockaddr_in adr;
>   memset (&adr, 0, sizeof (adr));
>   adr.sin_family = AF_INET;
>   adr.sin_port = htons (5600 /* some port */);

>   int optval = 1;
>   setsockopt(hSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&optval
,sizeof(int));
>   setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&optval
,sizeof(int));
>   bind(hSocket, (const sockaddr*)&adr, sizeof(sockaddr));

>   listen(hSocket, 10 /* max. connects */);
>   fd_set fd;
>   FD_ZERO (&fd);
>   FD_SET (hSocket, &fd);
>   timeval tv = { 5, 0 };
>   select (hSocket + 1, NULL, &fd, NULL, &tv);

>   return 0;
> }

> Joe
> --
> Remember Flight 93

 
 
 

check requested socket connection

Post by Joe Halpi » Sat, 13 Oct 2001 01:47:38



> Which compiler and flags do you use?

I tried this on AIX 4.3.3, HP-UX 11.0, Solaris 8, Linux (RH 7.1) and
FreeBSD 4.3. No flags (except that Solaris needs -lsocket).

Quote:> My application is multithreaded... to big for posting...

> Short description:
> Ive a thread listening for new connects.  In an infinite loop, i
> check with select() if there are possible clients willing connect.
> There are about 7 client which will connect.  In this time, select()
> returns 1.  Processor usage is 0%.  After this 7 clients are
> connected and the select() timed out once, i get a SIGALRM!? But
> why?

The only reason I can think of why you would get SIGALARM is 1) the
code calls alarm() somplace, or 2) someone did kill -ALRM <pid> from
the command line.

Quote:>  First i have not catched the signal so my process exited. Now ive
> defined a signalhandler for this sig. -> process run.

You'd probably be better off finding out why you're getting the signal.

Quote:>  Now the select() function always return immediate with 1.  The
> processor usage increase very fast up to 80-90%.  The result ->
> process cores.  Is it necessary to "reset" the descriptor like an
> event?

> my system:
> unixware 7.1.1 with gnu 2.95.2pl1

There's too much going on there, and too many unknowns, for me to
guess what's happening. Try to write the smallest program you can that
shows the problem.

Quote:> ps: the code ive wrote below are only fragments, not for compiling
> :)

That doesn't help people here understand what's going on though.

Does the sample code I posted work on your machine? If so, there's
something else going on in your program that's causing the issue. The
fact that you're getting SIGALARM when you don't expect it suggests
that select() is probably not the problem. Threading can introduce
additional possibilities.

Joe
--
Remember Flight 93

 
 
 

check requested socket connection

Post by David Schwart » Sat, 13 Oct 2001 04:59:02



> how to get the information that someone want to connect to my socket.
> when calling accept the function block until a connect appear (i will not
> use nonblocking sockets).

        You have two choices:

        1) Use a function that blocks until a connection is made

        2) Use non-blocking sockets.

        You have to choose one, there is no third alternative.

        DS

 
 
 

check requested socket connection

Post by Markus Fischbache » Sat, 13 Oct 2001 16:57:34


ive tried to use nonblocking sockets some time ago.
i had no luck...it wont work.
do you have a nonblocking socket class?

markus




> > how to get the information that someone want to connect to my socket.
> > when calling accept the function block until a connect appear (i will
not
> > use nonblocking sockets).

> You have two choices:

> 1) Use a function that blocks until a connection is made

> 2) Use non-blocking sockets.

> You have to choose one, there is no third alternative.

> DS

 
 
 

check requested socket connection

Post by Markus Fischbache » Sat, 13 Oct 2001 16:59:12


ill try to write such program.
i post it here...

thx, markus




> > Which compiler and flags do you use?

> I tried this on AIX 4.3.3, HP-UX 11.0, Solaris 8, Linux (RH 7.1) and
> FreeBSD 4.3. No flags (except that Solaris needs -lsocket).

> > My application is multithreaded... to big for posting...

> > Short description:

> > Ive a thread listening for new connects.  In an infinite loop, i
> > check with select() if there are possible clients willing connect.
> > There are about 7 client which will connect.  In this time, select()
> > returns 1.  Processor usage is 0%.  After this 7 clients are
> > connected and the select() timed out once, i get a SIGALRM!? But
> > why?

> The only reason I can think of why you would get SIGALARM is 1) the
> code calls alarm() somplace, or 2) someone did kill -ALRM <pid> from
> the command line.

> >  First i have not catched the signal so my process exited. Now ive
> > defined a signalhandler for this sig. -> process run.

> You'd probably be better off finding out why you're getting the signal.

> >  Now the select() function always return immediate with 1.  The
> > processor usage increase very fast up to 80-90%.  The result ->
> > process cores.  Is it necessary to "reset" the descriptor like an
> > event?

> > my system:
> > unixware 7.1.1 with gnu 2.95.2pl1

> There's too much going on there, and too many unknowns, for me to
> guess what's happening. Try to write the smallest program you can that
> shows the problem.

> > ps: the code ive wrote below are only fragments, not for compiling
> > :)

> That doesn't help people here understand what's going on though.

> Does the sample code I posted work on your machine? If so, there's
> something else going on in your program that's causing the issue. The
> fact that you're getting SIGALARM when you don't expect it suggests
> that select() is probably not the problem. Threading can introduce
> additional possibilities.

> Joe
> --
> Remember Flight 93

 
 
 

check requested socket connection

Post by fried_e » Sun, 14 Oct 2001 02:32:07



> ive tried to use nonblocking sockets some time ago.
> i had no luck...it wont work.
> do you have a nonblocking socket class?

If you give accept the approriate non-blocking flag, it will return if
no one is trying to connect.  Then you check the error value.  I can
see how this would not be threadsafe, but I don't know the semantics
of thread-safing variables (I have heard that it is possible, though).
 I think a lot of the BSD libraries are built around the old style
multi-process model, rather than the thread model, and they work great
there.

Could you post an outline of your application--I have to wonder why
you insist
on doing things this way.  There are a lot of solutions to specific
client-server problems.

Also, you have read, and re-read, Richard Stevens "Unix Network
Programming"?  He has a lot on threads in the second edition.

 
 
 

check requested socket connection

Post by David Schwart » Sun, 14 Oct 2001 05:56:57



> ive tried to use nonblocking sockets some time ago.
> i had no luck...it wont work.

        What does "it won't work" mean?

Quote:> do you have a nonblocking socket class?

        You can't do much better than the existing functions. The functions
worth wrapping (like 'select' and 'poll') are too specialized to wrap in
a generic class. How you use them depends too much on what you're trying
to do.

        DS

 
 
 

check requested socket connection

Post by Andrew Giert » Sun, 14 Oct 2001 23:39:25


 Joe> Because select() doesn't tell you when someone is trying to connect,

selecting a listening socket for reading tells you exactly that.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

check requested socket connection

Post by Andrew Giert » Sun, 14 Oct 2001 23:50:47



 >> Which compiler and flags do you use?

 Joe> I tried this on AIX 4.3.3, HP-UX 11.0, Solaris 8, Linux (RH 7.1)
 Joe> and FreeBSD 4.3. No flags (except that Solaris needs -lsocket).

The code you posted does not compile on FreeBSD 4.x (failure to
include <sys/time.h>), and even when compiled does not do what you
seem to expect: it simply waits for 5 seconds, regardless of whether
any clients show up, and then exits.

Adding the missing include and changing

  select (hSocket + 1, NULL, &fd, NULL, &tv);

to

  select (hSocket + 1, &fd, NULL, NULL, &tv);

gives a working program (which exits either after 5 seconds or as soon
as any client connects).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

check requested socket connection

Post by Joe Halpi » Mon, 15 Oct 2001 02:07:13





>  >> Which compiler and flags do you use?

>  Joe> I tried this on AIX 4.3.3, HP-UX 11.0, Solaris 8, Linux (RH 7.1)
>  Joe> and FreeBSD 4.3. No flags (except that Solaris needs -lsocket).

> The code you posted does not compile on FreeBSD 4.x (failure to
> include <sys/time.h>),

No, I added that when I compiled it on FreeBSD.

Quote:> and even when compiled does not do what you seem to expect: it
> simply waits for 5 seconds, regardless of whether any clients show
> up, and then exits.

You're right, it doesn't work the way I was thinking. I've been using
the write mask in a dispatcher for many years, but I'm using it to
detect when a non-blocking connect completes or fails, not this. Had
my head on sideways I guess.

Apologies to all for the confusion.

Joe
--
Remember Flight 93

 
 
 

1. Opening a socket connection while in a socket connection.

Hello,

I am new to using sockets and am working on a project that requires
me to audit services accessed under a client/server program (TCP/IP)
socket connection.

What I want to do is send a datapacket to a collector machine each
time a client accesses a service on the server.  It seems that
I am unable to do this from the server.  Basically, in short form
I need to be able to do the following:

1. Client connects (TCP/IP socket connection) to server
2. Client requests a service from the server
3. At that point I want to send a datapacket to a audit server.

Any Ideas?  I am familiar with most of the concepts involved in
using sockets.  Maybe I have been working on this to long and have
gotten tunnel vision.

Thanks

***********************************************************
Brent Curtiss


Academic Computing Services
The Ohio State University
***********************************************************

2. Installing ST19171WC disk on Sparc 5

3. Checking for a closed socket connection

4. are we getting hacked?

5. Teles & MPPP : "Stepping over missing frame "

6. how to check socket connection using C ?

7. please read this packet log: Crack attempt?

8. Help checking a TCP/IP socket connection

9. : One socket connection, several client requests?

10. checking socket connections

11. After MaxClient Connection, What will happend to the Client Connection Request?

12. Weird: get a socket connection without a running socket server !