Client Server Application using C#

Download: Project Archive

In this simple article you will learn “How to program a client _server application in Console.

Requirements:

We need Following Namespaces

reference

*Threading will be used at server side to get multiple clients on stream.

How it works:

Client:
In main method we create a TcpClient through which it will connect  to Server.

static void Main(string[] args)
{
TcpClient client = new TcpClient("server IP",5000);
NetworkStream ns = client.GetStream();
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
ParameterizedThreadStart pts = new ParameterizedThreadStart(recieve);
Thread t = new Thread(pts);
t.Start(sr);
while (true)
{
String s = Console.ReadLine();
sw.WriteLine(s);
sw.Flush();

}
}

NetwrokStream class help us in making a connection between server and client. Stream Reader is used to read the message from server, Stream writer to write to stream or to transfer the message from client to server.

while (true)

{

String s = Console.ReadLine();

sw.WriteLine(s);

sw.Flush();

}

This piece of code will help us in sending messages to server.

How client receive message:

public static void recieve(Object obj)
{
    string s = "";
    StreamReader sr = (StreamReader)obj;

    while(true)
    {
       s= sr.ReadLine();
       Console.WriteLine(s);
    }
}

StreamReader at client side is used to read messages from Server. You read a message form server and write it on console.

Server:

At server side we need a arraylist to store multiple client and to forecast the message to each client. I am using three threads: Main thread, second thread to read and third one is to connect the client.

static ArrayList list = new ArrayList();

static void Main(string[] args)
{

Thread te= new Thread(run);

te.Start();

while (true)

{

String consread = Console.ReadLine();

for (int i = 0; i < list.Count; i++)

{

Socket soket = (Socket)list[i];

StreamWriter writer = new StreamWriter(new NetworkStream(soket));

writer.WriteLine(consread);

writer.Flush();

}

This piece of code is getting each client from the list and sending the message to it.

How server read:

public static  void read(Object obj)
{

String read = "";

StreamReader strread = (StreamReader)obj;

while(true)
{

read=strread.ReadLine();

Console.WriteLine(read);

}

}

Connecting a client:

public static void run ()
{

Program socket_store = new Program();

IPAddress ipadrs = IPAddress.Parse("IP of the server");

TcpListener tcplsnr = new TcpListener(ipadrs, 5000);

tcplsnr.Start();

while (true)
{
Socket sokt = tcplsnr.AcceptSocket();

Console.WriteLine("Client connected");

list.Add(sokt);

NetworkStream stre = new NetworkStream(sokt);

StreamReader stread = new StreamReader(stre);

StreamWriter strwr = new StreamWriter(stre);

ParameterizedThreadStart pts = new ParameterizedThreadStart(read);

Thread t1 = new Thread(pts);

t1.Start(stread);

}
}

Run function run in new thread. We define it to get the client connected to server. When a client is connected, it is stored in ArrayList, create a new thread fro reading from that client.

StreamReader stread = new StreamReader(stre);

Thread t1 = new Thread(pts);

t1.Start(stread);

—————————————————————————————-

that’s it !

About:

Danish Kamal khan

Computer Science Dept.

G-C-University Lahore Pakistan.