Re: Socket Server... Why two?

From: dragoncoder (pktiwary_at_gmail.com)
Date: 12/14/04


Date: 13 Dec 2004 15:10:50 -0800


>> accept() -> select() -> recv() --- in a blocking way ---

I hope you meant non-blocking way

>>Both wait for data to be received before to unblock.

Not exactly. The accept just accepts a connection and returns as soon
as the connection is established. There is no data transfer till now.
It is just an information to the application that now one more socket
can send data so, be prepared to read from this socket also.

>>But what's the advantage and disavantage in the first and in second
solution?

When the connections have been established, any socket can data for the
application. If you have just one socket descriptor and you have
nothing to do without that data, you can do a blocking recv(). But if
you have more than one descriptors through which data can come and you
can proceed when any of them is available with the data, the
non-blocking recv() is done.

The advantage of non-blocking recv() is that you can proceed with the
job as soon as there are any data available on any of the descriptors.
Consider an example of a chat server. There can be thousands of users
connected at the same time and data can come from any user for any
other user. So, you can not do a blocking recv() because this will
cause other users to wait until the recv() is complete. I don't know of
any disadvantages of this approach.

Remember, select does not read any data. It just notifies the
application that xyz socket is available for reading. After that you
can call read() or recv() accordingly.



Relevant Pages

  • Re: on the semantics of connect(): EINTR, EALREADY, EINPROGRESS
    ... assume fd is a file descriptor referring to a blocking, ... If the connection cannot be established ... the socket, connectshall block for up to an unspecified timeout ... Since it's then essentially a non-blocking connect, ...
    (comp.unix.programmer)
  • Re: recv() hangs until SIGCHLD ?
    ... we have a thread blocked in recv() for more than 12 ... Can you clarify what you mean by "the socket has already been closed by the ... You mean the other end of the TCP connection shut it down? ... It is incorrect to assume that a return of 0 in non-blocking mode ...
    (Linux-Kernel)
  • Re: How can I tell when a remote TCP connection is closed?
    ... Recv only returns zero if the peer has ... to write to the socket and I get an broken pipe error. ... >I have a client with a TCP connection to a server. ... >need the code to be portable), is there a way that I can tell if the remote ...
    (microsoft.public.win32.programmer.networks)
  • Re: how to know if socket is still connected
    ... connection is still ok, if not will reconnect. ... If the server has closed the connection, then a recv() on the ... socket will raise an exception. ...
    (comp.lang.python)
  • Re: Socket Server... Why two?
    ... I hope you meant non-blocking way ... The accept just accepts a connection and returns as soon ... It is just an information to the application that now one more socket ... nothing to do without that data, you can do a blocking recv(). ...
    (comp.unix.programmer)