2009-05-05

Socket Operations

(This post is part of the TCP/IP .NET Sockets FAQ)

There are a few logical operations that may be performed on a TCP/IP socket, regardless of whether the socket is synchronous or asynchronous. Each of the operations below is marked "immediate" (meaning it is completed immediately) or "delayed" (meaning it depends on the network for completion).

Constructing (immediate) - TCP/IP sockets use the InterNetwork (for IPv4) or InterNetworkV6 (for IPv6) AddressFamily, the Stream SocketType, and the Tcp ProtocolType.
MSDN links: Socket

Binding (immediate) - A socket may be locally bound. This is normally done only on the server (listening) socket, and is how a server chooses the port it listens on. See Using Socket as a Server (Listening) Socket for details.
MSDN links: Bind

Listening (immediate) - A bound socket notifies the OS that it is almost ready to receive connections by listening. In spite of the term "listening", this operation only notifies the OS that the socket is about to accept connections; it does not actually begin accepting connections, though the OS may accept a connection on behalf of the socket. See Using Socket as a Server (Listening) Socket for details.
MSDN links: Listen

Accepting (delayed) - A listening socket may accept an incoming connection. When an incoming connection is accepted, a new socket is created that is connected to the remote side; the listening socket continues listening. The new socket (which is connected) may be used for sending and receiving. See Using Socket as a Server (Listening) Socket for details.
MSDN links: Accept, BeginAccept, EndAccept, AcceptAsync

Connecting (delayed) - A (client) socket may connect to a (server) socket. TCP has a three-way handshake to complete the connection, so this operation is not instantaneous. Once a socket is connected, it may be used for sending and receiving. See Using Socket as a Client Socket for details.
MSDN links: Connect, BeginConnect, EndConnect, ConnectAsync

Reading (delayed) - Connected sockets may perform a read operation. Reading takes incoming bytes from the stream and copies them into a buffer. A 0-byte read indicates a graceful closure from the remote side. See Using Socket as a Connected Socket for details.
MSDN links: Receive, BeginReceive, EndReceive, ReceiveAsync

Writing (delayed) - Connected sockets may perform a write operation. Writing places bytes in the outgoing stream. A successful write may complete before the remote OS acknowledges that the bytes were received. See Using Socket as a Connected Socket for details.
MSDN links: Send, BeginSend, EndSend, SendAsync

Disconnecting (delayed) - TCP/IP has a four-way handshake to terminate a connection gracefully: each side shuts down its own outgoing stream and receives an acknowledgment from the other side.
MSDN links: Disconnect, BeginDisconnect, EndDisconnect, DisconnectAsync

Shutting down (immediate) - Either the receiving stream or sending stream may be clamped shut. For receives, this is only a local operation; the other end of the connection is not notified. For sends, the outgoing stream is shut down (the same way Disconnect does it), and this is acknowledged by the other side; however, there is no notification of this operation completing.
MSDN links: Shutdown

Closing (immediate or delayed) - The actual socket resources are reclaimed when the socket is disposed (or closed). Normally, this acts immediate but is actually delayed, performing a graceful disconnect in the background and then actually reclaiming the socket resources when the disconnect completes. Socket.LingerState may be set to change Close to be a synchronous disconnect (delayed, but always synchronous), or an immediate shutdown (always immediate).
MSDN links: Close, LingerState

(This post is part of the TCP/IP .NET Sockets FAQ)

5 comments:

  1. Hello,
    Can you give me sample example for 'how to close client socket properly?
    In my application when client exit(here I closed reader,writer and close socket) and when client start again client able to connect (BeginConnect() succeeds) but unable to send data to server(In such case I have to restart my server application to make it running).I guess client socket not closing properly.Please provide me solution.

    ReplyDelete
    Replies
    1. The problem is with your server. If you close the client socket, then that's all you have to do.

      Delete
    2. Thanks for the reply.
      But how to handle such scenario, so that client will connect properly and data will transfer properly.
      I have implemented Keep alive.
      Please provide some basic steps to achieve this.

      Delete
    3. I'm sorry, Sam, but there are no basic steps.

      I recommend reading my TCP/IP blog posts, and then reading the items listed in the Resources blog post: http://nitoprograms.blogspot.com/2009/05/tcpip-resources.html

      Then you'll have enough information to design a custom TCP/IP protocol.

      If you want a simpler solution, you could use Web API, WCF, or .NET remoting.

      Delete
    4. Thanks Stephen. I will read your TCP/IP blog posts.
      And thanks for your suggestions I will also think on that.

      Delete