Half Duplex Communication on Network With C#

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:

  1. Server
  2. 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);
}

Our server is ready. Now let's go to the client code. In the server I used the TCPListner class. In the client I use the TCPClient class. The object of the class calls the connect method and passes the IP address and port number of the server; see:

IPAddress ip = IPAddress.Parse("127.0.0.1");
_client = new TcpClient();
_client.Connect(ip, 800);


We have created a network stream in the server so we just need to get that stream into the client and read and write data from and to the stream. Again I have created two methods, send and receive, to read and write data and call both methods in two different threads.

i
f (_client.Connected)
{
    Console.WriteLine("Connected to server");
    NetworkStream ns = _client.GetStream();

    Thread th = new Thread(() => receive(ns));
    th.Start();

    Thread th1 = new Thread(() => send(ns));
    th1.Start();
}
else
{
    Console.WriteLine("Server Offline");
}

 

public static void receive(NetworkStream ns)

{ 

    StreamReader sr = new StreamReader(ns);

    Console.WriteLine(sr.ReadLine());

    ns.Flush();

    send(ns);   

}

public static void send(NetworkStream ns)

{  

    StreamWriter sr = new StreamWriter(ns);

    sr.WriteLine(Console.ReadLine());

 

    sr.Flush();

    ns.Flush();

    receive(ns);   

}

How to test the application:

  1. Run the server

  2. Run the Client

  3. Type "hello client" on server console and hit Enter

  4. Type "hello Server" on client console and hit Enter

Full Duplex Communication is coming soon.