A socket is an End – Point of a two-way communication link between two programs running on the network.
A server application normally listens to a specific port waiting for connection request from client. When a connection request arrives from the client, the client and server establish a dedicated connection over which they can communicate. During the connection process, the client assigned a local port number, and binds a socket to it. Client talks to server by writing to socket and get information from the server by reading from it. Similarly, the server gets a new local port number. Server also binds a socket to its local port and communicates with the client by reading from and writing it.
Also client and server must agree on a protocol. They are two type of communication protocol uses for socket programming, they are TCP/IP communication and UDP/IP communication. In this article we use TCP/IP as communication protocol.
More about TCP
TCP is a connection-oriented communication protocol which guarantees that sent packet will arrive at the intended receiver undamaged and in the correct sequence. TCP allows protocols like HTTP to send information across a network as simply and reliably as writing to a file on local machine. If packets of information don’t arrive at the recipient, TCP ensures that the packets are sent again. If the packets arrive out of order, TCP reassemble them in correct order transparently to receiving application. If duplicate packets arrive, TCP discard them.
In C# we need two programs for communicating a socket application.
1. Server Socket Program
A server socket program running on a computer has a socket that bound to port number on the same computer and listening to the client’s incoming requests.
2. Client Socket Program
Client socket program have to know the IP address of the computer that the server socket program resides and the port number assign for listening for client request.
Once the connection is established between server and client, they can communicate through their own socket. Since programs that communicate via TCP process the data they send and receive as stream of bytes, programmers sometimes refer to Socket as “Stream Socket”.
Establishing a Simple TCP Server
Establishing a simple server with TCP and stream sockets require five steps.
1. Create an object class of TcpListener of namespace System.Net.Sockets.
This class represents a TCP stream socket through which a server can listen for requests. Creating new TCPListener , as in
TcpListener server = new TcpListener( ipAddress, port );
2. The second step is the connection process is to call TcpListener’s Start method, which causes the TcpListener object to begin listening for connection requests. The server listens indefinitely for a request, the execution of the server-side application waits until some client attempts to connect with it. The server creates a connection to the client when it receives a connection request. An object of class Socket (namespace Syetem.Net.Socket) manages a connection to client. Method AcceptSocket of class TcpListener accepts a connection request. This method return a Socket object upon connection,
Socket connection = server.AcceptSocket();
When the server receives a request, AcceptSocket calls method Accept of the TcpListener’s underlying Socket to make the connection. This is an example of how networking complexity is hidden from the programmer. You simply place the preceding statement in a server-side program the classes of namespace System.Net.Sockets handle the details of accepting request and establishing connections.
3. The third step establishes the stream used for communication with the client. In this step, we create a NetworkStream object that uses the Socket object representing the connection to perform the actual sending and receiving data.
4. Step four is the processing phase, in which the server and client communicate using the connection established in the third step. In this phase, the client uses BinaryWriter method Write and BinaryReader method readString to perform the appropriate communication.
5. The fifth step is the connection-termination phase. When the client and server have finished communicating , the server calls method Close of the Binaryreader, BinaryWriter ,NetworkStream and Socket to terminate the connection.
Establishing a Simple TCP Client
There are four steps to creating simple TCP client.
1. First, we create an object of class TcpClient (namespace System.Net.Sockets0 to connect to the server. The connection established by calling TcpClient method Connect. One overload version of this method takes two arguments the server’s IP address and its port number,
TcpClient client = new TcpClient();
client.Connect( serverAddress, serverPort );
The serverPort is an int that represents the port number to which the server application is bound to listen for connection requests. The serverAddress can be either and IPAddress instance or a string that specifies the server’s hostname or IP address. Method Connect also has an overload version to which you can pass an IPEndPoint object that represents an IP addresss/port number pair. TcpClient method Connect calls Socket method Connect to establish the connection. If the connection is successful, TCPClient method Connect returns a positive integer otherwise it returns 0.
In step two, the TcpClient uses its GetStream method to get a NetworkStream so that it can write to and read from the server. We then use the NetworkStream object to create a BinaryWriter and a BinaryReader that will be used to send information to and receive information from the server, respectively.
The third step is the processing phase, in which the client and the server communicate. it is very similar to the server process.
After the transmission is complete, step four requires the client to close the connection by calling method Close() on each of BinaryReader, BinaryWriter, NetworkStream and TcpClient. This closes each of the stream and the TcpClient’s socket to terminate the connection with the server.
TcpClient client = new TcpClient();
client.Connect( serverAddress, serverPort );
The serverPort is an int that represents the port number to which the server application is bound to listen for connection requests. The serverAddress can be either and IPAddress instance or a string that specifies the server’s hostname or IP address. Method Connect also has an overload version to which you can pass an IPEndPoint object that represents an IP addresss/port number pair. TcpClient method Connect calls Socket method Connect to establish the connection. If the connection is successful, TCPClient method Connect returns a positive integer otherwise it returns 0.
In step two, the TcpClient uses its GetStream method to get a NetworkStream so that it can write to and read from the server. We then use the NetworkStream object to create a BinaryWriter and a BinaryReader that will be used to send information to and receive information from the server, respectively.
The third step is the processing phase, in which the client and the server communicate. it is very similar to the server process.
After the transmission is complete, step four requires the client to close the connection by calling method Close() on each of BinaryReader, BinaryWriter, NetworkStream and TcpClient. This closes each of the stream and the TcpClient’s socket to terminate the connection with the server.