Socket Operations
• CommentsThere 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