This article will shows you, how to create a client and a server application that communicates using sockets. This article also contains the sample program. Read on to learn more!
The classes and enumerations in the System.Net.Sockets namespace provide an implementation of the popular Windows Sockets (Winsock) interface for use with .Net languages.
The original sockets were developed as part of the UNIX operating system, and they have been widely used as a simple way to pass data between programs. They are widely used on the internet and can be used between programs on a single machine as well.
Sockets are similar to telephone communications between people working for different companies. If I’m going to contact you from my phone, I need to know you company phone number and your extension. In socket communication, the “Phone number” is the IP address of the machine you want to talk to. You may know you want to talk to. You may know this as a dotted IP address of the form 127.0.0.1 or as a more humanly friendly representation of www.domainname.domaintype
Just as everyone in an office has an extension on the same phone number, every program on a machine that wants to use sockets uses a unique port number. Port number ranges from zero upwards: The reserved port number ranges from 0 to 1024. These reserved ports are used for official purposes or by Web servers and mail servers. You are free to use port numbers above 1024 for your own use.
A server process can reserve a port number and then sit on it waiting for incoming calls. A client process makes a call by opening a socket and specifying the IP address and port number it wants to connect to. If the address and port number are correct, the two process will be connected.
How the sockets can be used?
After the whole discussion about the sockets, the question arises in our mind that how we can use sockets. Now the answer is, System.Net.Sockets namespace contains a Socket class that will do everything you want, Microsoft recommends that you use the two classes that it supplies to represent either end of a socket connection. TCPClient represents the client end, whereas TCPListener represent the server end.
At the client end, you create a TCPClient, passing it the IP address of the machine you want to connect to and the port that the server process is using.
When the connection has been established, the GetStream() method returns a reference to a Stream object, which is used to read and write through the sockets:
The Stream’s Read() and Write() methods can be used to pass data through the socket, but because they use bytes, it is necessary to convert character data into bytes before sending. As an alternative, you can use the Send() and Receive() methods that TCPClient inherits from socket, which also work with byte arrays.
TCPListener implements a parallel set of functionality that helps implement the server side of a socket connection. A TCPListener is created to listen on particular socket, which obviously has to match the ones that clients will be calling in on:
Once created, the Start() method starts the object listening for network connections. There are two ways in which a listener can connect to incoming clients. One way is to call AcceptSocket() or AcceptTCPClient(), both of which will block until a client connects. Alternatively, the server can periodically call the TCPListener’s Pending() method, which returns true if any clients are waiting to connect. If there are clients waiting, calls to AcceptSocket() or AcceptTCPClient() will connect immediately.
Calls to AcceptSocket() or AcceptTCPClient() returns a Socket reference, so the server code can use the Send() and Receive() methods to pass data through the connection. Once the conversation is finished, the Stop() method stops the TCPListener from listing for network traffic.
After the discussion about the Socket class and the methods that are involve in communication we now turn our concentration towards the application making. Use the following steps to create a client and server application:
- Create a console application for client and place the following code:
- Select the New console application option from the startup page and place the following code for server:
- Run the both application either on the same machine or on a different machine without any trouble, providing that TCP/IP networking is installed correctly.
- The converted and trimmed line is printed out, and then the code loops around, reading and displaying lines until a line appears that start with a “.”. At this point, the Stop() call tells the socket to stop listening for network traffic, and the program terminates.
Note: First run server and then the client application.
· Create a console application in visual basic.net for socket client.
· Create a console application in visual basic.net for socket server.
· Connect the server and client on the selected port and using GetStream() method read and write messages. The program will terminate on “.”. Means Stop() method executes when it reads the “.” at the start of the line.