Name

Socket

Synopsis

This class implements a standard Berkeley socket. Each socket is constructed with the address family to use, the socket type (datagram or stream), and the protocol that the socket will use. Every socket must be bound to a local endpoint before you can use it. The Bind() method takes a System.Net.IPEndPoint object that contains the local IP address and port number to bind the socket to. Bind() must be called before any connection can be made through the socket. To establish a connection to a remote address, use Connect().

To listen for connections from remote clients, use Listen() to set the socket in listening mode where it waits for incoming connections. The integer argument to Listen() specifies how many remote connection requests can be queued at one time, waiting for a socket connection. A call to Accept() returns a new socket that connects to the first pending connection request in the listening queue. This new socket exists only for this connection, and is destroyed once the connection is closed.

Data is written to a socket using Send(). Data from a specified buffer is sent through the socket to its remote endpoint. Data is read from a socket using Receive(). Receive() gets data from the socket connection and stores it in a specified receive buffer.

You can set several socket options to control the behavior of a socket with SetSocketOption(). This method requires a SocketOptionLevel value, which determines the type of socket option to set. For example, ...

Get C# in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.