As a self-learner I tried my programming skills on sockets and threads. I will explain how to communicate on a network using C# code but before I start I would like to mention that my code is for half-duplex communication in which the sender must wait to receive a message.
The .Net framework provides the TCPListner and TcpClient classes for communication. A socket is an end point for the inter-process communication. The application is divided into two phases; they are:
- Server
- Client
Let us first see the server code. Start Visual Studio and choose "File" -> "New" -> "Project..." then choose Console application. Then import the following three namespaces:
using System.Net;
using System.Net.Sockets;
using System.IO;
We use the IP address of the machine for the server. I am using 127.0.0.1 because I wanted to test my application on the local system but you can use any IP address on the network .
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener serv = new TcpListener(ip, 800);
serv.Start();
Console.WriteLine("server started");
I have created the object TCPListner and passed two parameters to its constructor; first is the IP address of the machine, The second is the port number on which the server will listen for client requests. Once the Start method is called the server starts.
Socket _socket = serv.AcceptSocket();
NetworkStream ns = new NetworkStream(_socket);
I have created a socket; AcceptSocket is the method responsible for accepting all the pending requests. You can understand sockets by the simple analogy to electrical sockets in our home. When we want to charge our mobile phone we connect our charger to a socket and charge it. Similarly when clients want to connect with the server they plug into a server socket. I have created a network stream here because network stream is the class that provides the methods for sending and receiving data over a stream.
The server is now ready. The network stream is ready. The next part to do is to write and read data on the network.
For this I have two methods, Send and Receive; both will take a network stream as a parameter.
Thread th = new Thread(() => send(ns));
th.Start();
Thread th1 = new Thread(() => receive(ns));
th1.Start();
I have called both methods with a thread. Don't be confused with "()=>"; I prefer to use anons when using threads however you can use your own traditional way to call parameterized methods in threads.
public static void send(NetworkStream ns)
{
StreamWriter sr = new StreamWriter(ns);
sr.WriteLine(Console.ReadLine());
sr.Flush();
receive(ns);
}
To write data to the stream you need a stream writer and call the write line method to write string data on the network stream, then you need to flush the stream. You can see here I have called the receive method here just because once it has written the data on the stream it will wait for the incoming data and when it reads the data it will prepare to write the data.
public static void receive(NetworkStream ns)
{
StreamReader sr = new StreamReader(ns);
Console.WriteLine(sr.ReadLine());
ns.Flush();
send(ns);
}

Join the conversation! Your thoughts help the community grow.