Socket programming is a way of connecting two nodes on a network to communicate with each other. Basically, it is a one-way Client and Server setup where a Client connects, sends messages to the server and the server shows them using socket connection. One socket (node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while the client reaches out to the server. Before going deeper into Server and Client code, it is strongly recommended to go through TCP/IP Model.
Before creating client’s socket a user must decide what ‘IP Address‘ that he want to connect to, in this case, it is the localhost. At the same time, we also need the ‘Family‘ method that will belong to the socket itself. Then, through the ‘connect‘ method, we will connect the socket to the server. Before sending any message, it must be converted into a byte array. Then and only then, it can be sent to the server through the ‘send‘ method. Later, thanks to the ‘receive‘ method we are going to get a byte array as answer by the server. It is notable that just like in the C language, the ‘send’ and ‘receive’ methods still return the number of bytes sent or received.
In the same way, we need an ‘IP address’ that identifies the server in order to let the clients to connect. After creating the socket, we call the ‘bind‘ method which binds the IP to the socket. Then, call the ‘listen‘ method. This operation is responsible for creating the waiting queue which will be related to every opened ‘socket‘. The ‘listen‘ method takes as input the maximum number of clients that can stay in the waiting queue. As stated above, there is a communication with the client through ‘send‘ and ‘receive‘ methods.
Note: Don’t forget the conversion into a byte array.
To run on Terminal or Command Prompt:
- First save the files with
.csextension. Suppose we saved the files as client.cs and server.cs.
- Then compile both the files by executing the following commands:
$ csc client.cs
$ csc server.cs
- After successful compilation opens the two cmd one for Server and another for Client and first try to execute the server as follows:
- After that on another cmd execute the client code and see the following output on the server side cmd.
- Now you can see the changes on the server as soon as the client program executes.
- What is Regular Expression in C#?
- How to set the Background Color of the RichTextBox in C#?
- How to set the Height of the Elements present in the ListBox in C#?
- How to Add Text in the RichTextBox in C#?
- How to set the Location of the RichTextBox in C#?
- How to set the Font of the Content present in the RichTextBox in C#?
- How to set the Visibility of ListBox in C#?
- How to set the Foreground Color of the RichTextBox in C#?
- How to sort elements in the ListBox in C#?
- How to style the Border of ListBox in C#?
- How to set the Font of the ListBox in C#?
- How to add Items in ListBox in C#?
- How to set the Foreground Color of a ListBox in C#?
- How to set the Background Color of a ListBox in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.