Can the accept() be used after using one successful connect() with the same TCP port ?

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Louie Ch » Tue, 18 Mar 2003 15:16:54



Dear sir/madam,

I am using the glibc under the Linux platform.

Does anyone know if I can use the functions "listen() & accept()"
after one successful TCP connection with the function "connect" ?
Please refer to the following concept codes !!

int sockfd, new_connect_fd;

sockfd = socket(domain, type, protocol);
connect(sockfd, sockaddr, addrlen);                     <--- connect() and connection
successful !
listen(sockfd, backlog);
new_connect_fd =  accept(sockfd, sockaddr, addrlen);    <--- Can the
accept() be used after using connect() ?

Thanks for your help !

Best Regards. Louie

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Michael Fu » Tue, 18 Mar 2003 23:31:53



> I am using the glibc under the Linux platform.

> Does anyone know if I can use the functions "listen() & accept()"
> after one successful TCP connection with the function "connect" ?
> Please refer to the following concept codes !!

> int sockfd, new_connect_fd;

> sockfd = socket(domain, type, protocol);
> connect(sockfd, sockaddr, addrlen);                        <--- connect() and connection
> successful !
> listen(sockfd, backlog);
> new_connect_fd =  accept(sockfd, sockaddr, addrlen);       <--- Can the
> accept() be used after using connect() ?

Experimentation is a great way to learn.  Rather than ask if something
can be done, try it yourself and see if it can be done.

Be sure to add error checking to your code so you'll know if the
function calls succeeded or failed, and if they failed, why they
failed.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Road Runner New » Wed, 19 Mar 2003 09:33:12


Connect is not used with accept - returns a socket after it has received
data.  use accept with bind, and listen.



> > I am using the glibc under the Linux platform.

> > Does anyone know if I can use the functions "listen() & accept()"
> > after one successful TCP connection with the function "connect" ?
> > Please refer to the following concept codes !!

> > int sockfd, new_connect_fd;

> > sockfd = socket(domain, type, protocol);
> > connect(sockfd, sockaddr, addrlen); <--- connect() and connection
> > successful !
> > listen(sockfd, backlog);
> > new_connect_fd =  accept(sockfd, sockaddr, addrlen); <--- Can the
> > accept() be used after using connect() ?

> Experimentation is a great way to learn.  Rather than ask if something
> can be done, try it yourself and see if it can be done.

> Be sure to add error checking to your code so you'll know if the
> function calls succeeded or failed, and if they failed, why they
> failed.

> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by j.. » Thu, 20 Mar 2003 00:22:54



> Dear sir/madam,

> I am using the glibc under the Linux platform.

> Does anyone know if I can use the functions "listen() & accept()"
> after one successful TCP connection with the function "connect" ?
> Please refer to the following concept codes !!

> int sockfd, new_connect_fd;

> sockfd = socket(domain, type, protocol);
> connect(sockfd, sockaddr, addrlen); <--- connect() and connection
> successful !
> listen(sockfd, backlog);
> new_connect_fd =  accept(sockfd, sockaddr, addrlen);       <--- Can the
> accept() be used after using connect() ?

You're mixing apples and oranges. A listening socket can't be used for
anything but accepting connections. It's not connected to anything.

A connected socket is, well, connected. You can't use it as a
listening socket.

If you're serious about network programming, get this book:

http://www.amazon.com/exec/obidos/ASIN/013490012X/qid=1048000908/sr=2...

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Irfan Bond » Thu, 20 Mar 2003 09:02:28



> Dear sir/madam,

> I am using the glibc under the Linux platform.

> Does anyone know if I can use the functions "listen() & accept()"
> after one successful TCP connection with the function "connect" ?
> Please refer to the following concept codes !!

> int sockfd, new_connect_fd;

> sockfd = socket(domain, type, protocol);
> connect(sockfd, sockaddr, addrlen);                        <--- connect() and connection
> successful !
> listen(sockfd, backlog);
> new_connect_fd =  accept(sockfd, sockaddr, addrlen);       <--- Can the
> accept() be used after using connect() ?

> Thanks for your help !

> Best Regards. Louie

Server Side Calls : socket, bind, listen, accept.

Client Side Calls : Socket, connect

Then client side or Server side can use recv, send to send or receive
data from the other peer.

You don't want to mix client side and server side calls.

In TCP case anyways the fd that u get can  be used for bidirectional
communication anyways.

So Client and server can both receive and send data.
Irfan.

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Alexander Kotelniko » Fri, 21 Mar 2003 02:03:32


>>>>> On Tue, 18 Mar 2003 15:22:54 GMT


j>

Quote:>> Dear sir/madam,

>> I am using the glibc under the Linux platform.

>> Does anyone know if I can use the functions "listen() & accept()"
>> after one successful TCP connection with the function "connect" ?
>> Please refer to the following concept codes !!

>> int sockfd, new_connect_fd;

>> sockfd = socket(domain, type, protocol);
>> connect(sockfd, sockaddr, addrlen); <--- connect() and connection
>> successful !
>> listen(sockfd, backlog);
>> new_connect_fd =  accept(sockfd, sockaddr, addrlen);   <--- Can the
>> accept() be used after using connect() ?

j>
j> You're mixing apples and oranges. A listening socket can't be used for
j> anything but accepting connections. It's not connected to anything.

You are wrong. Look at TCP state diagram. Simultaneous open is allowed
in TCP, and Stevens writes about it in UNPv1 TCP Connection
Establishment and Termination

--
Alexander Kotelnikov
Saint-Petersburg, Russia

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Valentin Nechaye » Fri, 21 Mar 2003 02:45:39



>>> sockfd = socket(domain, type, protocol);
>>> connect(sockfd, sockaddr, addrlen); <--- connect() and connection
>>> successful !
>>> listen(sockfd, backlog);
>>> new_connect_fd =  accept(sockfd, sockaddr, addrlen);       <--- Can the
>>> accept() be used after using connect() ?

j>> You're mixing apples and oranges. A listening socket can't be used for
j>> anything but accepting connections. It's not connected to anything.
AK> You are wrong. Look at TCP state diagram. Simultaneous open is allowed
AK> in TCP, and Stevens writes about it in UNPv1 TCP Connection
AK> Establishment and Termination

I think you mixed *sockets* and *ports*. BSD sockets don't allow socket
to switch to listening state after connect(), and don't allow to do
connect on socket which is listening. This has no common with TCP states,
TCP ports (as numbers in per-host addressing space), TCP by itself, and
simultaneous opening of a port using different sockets (which may require
SO_REUSEADDR and SO_REUSEPORT).

-netch-

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by j.. » Fri, 21 Mar 2003 03:46:49



> >>>>> On Tue, 18 Mar 2003 15:22:54 GMT

> j>

> >> Dear sir/madam,

> >> I am using the glibc under the Linux platform.

> >> Does anyone know if I can use the functions "listen() & accept()"
> >> after one successful TCP connection with the function "connect" ?
> >> Please refer to the following concept codes !!

> >> int sockfd, new_connect_fd;

> >> sockfd = socket(domain, type, protocol);
> >> connect(sockfd, sockaddr, addrlen); <--- connect() and connection
> >> successful !
> >> listen(sockfd, backlog);
> >> new_connect_fd =  accept(sockfd, sockaddr, addrlen);      <--- Can the
> >> accept() be used after using connect() ?
> j>
> j> You're mixing apples and oranges. A listening socket can't be
> j> used for anything but accepting connections. It's not connected
> j> to anything.

> You are wrong. Look at TCP state diagram. Simultaneous open is
> allowed in TCP, and Stevens writes about it in UNPv1 TCP Connection
> Establishment and Termination

A simultaneous open is when two applications send a SYN to each other
at about the same time, such that the segments pass each other on the
wire, and are received by each side before any ACK is. It doesn't have
anything to do with the socket API.
 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Alexander Kotelniko » Fri, 21 Mar 2003 04:24:23


>>>>> On Wed, 19 Mar 2003 19:45:39 +0200

VN>

>>>> sockfd = socket(domain, type, protocol);
>>>> connect(sockfd, sockaddr, addrlen); <--- connect() and connection
>>>> successful !
>>>> listen(sockfd, backlog);
>>>> new_connect_fd =  accept(sockfd, sockaddr, addrlen);   <--- Can the
>>>> accept() be used after using connect() ?

j> You're mixing apples and oranges. A listening socket can't be used for
j> anything but accepting connections. It's not connected to anything.
AK> You are wrong. Look at TCP state diagram. Simultaneous open is allowed
AK> in TCP, and Stevens writes about it in UNPv1 TCP Connection
AK> Establishment and Termination
VN>
VN> I think you mixed *sockets* and *ports*. BSD sockets don't allow socket
VN> to switch to listening state after connect(), and don't allow to do
VN> connect on socket which is listening. This has no common with TCP states,
VN> TCP ports (as numbers in per-host addressing space), TCP by itself, and
VN> simultaneous opening of a port using different sockets (which may require
VN> SO_REUSEADDR and SO_REUSEPORT).

Oops, I was wrong, but in another way. I remembered that there is a
branch from LISTEN to SYN_SEND in TCP state diagram (and from SYN_SENT
we can achieve simultaneous open), but I had not remembered that it is
used not on OPEN, but on SEND, so this really can not occur with BSD
sockets.

Sorry for misinformation,
--
Alexander Kotelnikov
Saint-Petersburg, Russia

 
 
 

Can the accept() be used after using one successful connect() with the same TCP port ?

Post by Valentin Nechaye » Fri, 21 Mar 2003 04:47:30



> You're mixing apples and oranges. A listening socket can't be used for
> anything but accepting connections. It's not connected to anything.
> A connected socket is, well, connected. You can't use it as a
> listening socket.

It was possible in some versions of network stack in Linux 2.2 branch.
In it one could do delisten() (which was implemented hackerly, as another
socket call with incorrect parameters combination) to close listening
and then reopen it; and one could do disconnecting (closing of connection)
keeping socket bound to some port. Idea of this was to keep binding to
low port (which requires root) after dropping root privilege (or even
specific capability) and keeping possibility to open or close listening
and/or connection in arbitrary moments. But this died in the next version.

-netch-

 
 
 

1. TCP Socket Using connect in Non Blocking - Results in errors using recv()

The following is part of a client application running on a Solaris 8
platform.

To enable a timeout to be set when using TCP Sockets connect()

I have first set the socket to non-blocking using getsockopt() and
fcntl()
I then connect(), in case of an EINPROGRESS error I call select() with
a timeout.

On succesfully completing the connection I then use fcntl() to return
the flags to - setting the socket to blocking.

I subsequently use a listening thread which repeatedly calls recv()
the flags in recv() are set to 0 so I expect recv() to work in
blocking mode.

I have tested this against two seperate server applications
1) A Message Router (server) running on a PC running Windows NT
(server uses CAsyncSocket). Everything seems to work OK. Messages are
received at the client and recv() blocks until the next message is
available.
2) A Messsage Router (Server) running on a LynxOS  box. In this case I
experience problems at the Listener (client) the recv() does not
appear to block and continuously returns -1, trapping the errno =
EINVAL (appears to correspond to MSG_OOB flag set and socket still non
blocking. In this case I'm lost for the cause of the problem.

a)Can I change the flags for a socket after connection.
b)Is it possible that the Router (server) is
disconnecting/reconnecting the socket
c)Should recv() block if the flags are set to zero regardless of the
flags set for the socket

If you can help

2. Netscape can not run Java applete

3. Accepting only known clients to connect using ssh

4. DataRace PCMCIA Fax Modem under .99pl14 on T1950CT Laptop

5. System is crashing if I am using more then one disk on scsi controler :(

6. FreeBSD 3.0/Compaq SmartII Raid Controller

7. Socket Question (Stopping an accept using the listening port?)

8. Mail Check applet

9. Connect Linux box with Win95 using TCP/IP

10. Using C, what ports are being used by which programs?

11. Will Linux Connect to a Windows 98 WorkGroup using a TCP/IP Protocol?

12. Determining what port are currently used/needed and then using iptables to block the rest?

13. can not connect myself using tcp