Introduction
In order to do communication between two applications in client/server environment, we need some type of addressing, and mechanism or protocol to do proper communication. Communication can be done in two ways:
- Connection Oriented
In connection oriented service, we establish a proper connection between client and server through some sort of handshaking. For this, we have a protocol or standard called TCP. I.e., TCP provides us service to send data in connection oriented environment.
- Connection Less
In connection less service, we do data transfer without any connection between client and server. For this, we have a protocol or standard called UDP. I.e., UDP provides us service to send data without taking care of client activation.
Both ways have there advantages and disadvantages and can be used according to the type of application. Like, TCP provides reliable and secure transfer of data but slow, while UDP provides unreliable transfer of data but fast.
Now, to implement the above protocols, we require some sort of end points which are provided by Sockets. As you know, we have lots of ports available in computer to do end to end communication, and Sockets work on these ports.
.NET framework provides us a higher level of abstraction upon sockets in the form of TcpListener
, TCPClient
and UDPClient
. TcpListener
is a high level interface for server application and TCPClient
is for client application. They are both implemented over sockets but provide easier and high level interface. To do end to end communication between TcpListener
and TCPClient
without any complications, .NET provides NetworkStream
. We can use both stream reader and writer over NetworkStream
through StreamReader
and StreamWriter
.
Here, I�ll show you a simple client/server application which performs simple messaging using TcpListener
, TCPClient
and NetworkStream
, StreamReader
and StreamWriter
.
Here is the code:
TCP Server
This is the server side code which listens on a particular port using TcpListener
.
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
namespace TCPSocketServer
{
public class Server : Form
{
Button btnStartServer;
private StreamWriter serverStreamWriter;
private StreamReader serverStreamReader;
public Server()
{
this.btnStartServer = new Button();
this.btnStartServer.Text = "Start Server";
this.btnStartServer.Click += new System.EventHandler(this.btnStartServer_Click);
this.Controls.Add(this.btnStartServer);
}
public static void Main(string[] args)
{
Server tcpSockServer = new Server();
Application.Run(tcpSockServer);
}
private bool StartServer()
{
TcpListener tcpServerListener = new TcpListener(4444);
tcpServerListener.Start();
Console.WriteLine("Server Started");
this.btnStartServer.Enabled = false;
Socket serverSocket = tcpServerListener.AcceptSocket();
try
{
if (serverSocket.Connected)
{
Console.WriteLine("Client connected");
NetworkStream serverSockStream = new NetworkStream(serverSocket);
serverStreamWriter = new StreamWriter
(serverSockStream);
serverStreamReader = new StreamReader(serverSockStream);
}
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
return false;
}
return true;
}
private void btnStartServer_Click(object sender,System.EventArgs e)
{
if (!StartServer())
Console.WriteLine("Unable to start server");
while (true)
{
Console.WriteLine
("CLIENT: "+serverStreamReader.ReadLine());
serverStreamWriter.WriteLine("Hi!");
serverStreamWriter.Flush();
}
}
}
}
TCP Client
This is the client side code which connects to the server through a particular port on which the server is listening client�s request, using TCPClient
.
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
namespace TCPSocketClient
{
public class Client : Form
{
private Button btnConnectToServer;
private Button btnSendMessage;
private StreamReader clientStreamReader;
private StreamWriter clientStreamWriter;
public Client()
{
this.btnConnectToServer = new Button();
this.btnConnectToServer.Text = "Connect";
this.btnConnectToServer.Click +=
new System.EventHandler(btnConnectToServer_Click);
this.btnSendMessage = new Button();
this.btnSendMessage.Text = "Send Message";
this.btnSendMessage.Top += 30;
this.btnSendMessage.Width += 20;
this.btnSendMessage.Click += new System.EventHandler(btnSendMessage_Click);
this.Controls.Add(this.btnConnectToServer);
this.Controls.Add(this.btnSendMessage);
}
public static void Main(string[] args)
{
Client tcpSockClient = new Client();
Application.Run(tcpSockClient);
}
private bool ConnectToServer()
{
try
{
TcpClient tcpClient = new TcpClient("localhost",4444);
Console.WriteLine("Connected to Server");
NetworkStream clientSockStream = tcpClient.GetStream();
clientStreamReader = new StreamReader(clientSockStream);
clientStreamWriter = new StreamWriter(clientSockStream);
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
return false;
}
return true;
}
private void btnConnectToServer_Click(object sender,System.EventArgs e)
{
if (!ConnectToServer())
Console.WriteLine("Unable to connect to server");
}
private void btnSendMessage_Click(object sender,System.EventArgs e)
{
try
{
clientStreamWriter.WriteLine("Hello!");
clientStreamWriter.Flush();
Console.WriteLine("SERVER: "+clientStreamReader.ReadLine());
}
catch(Exception se)
{
Console.WriteLine(se.StackTrace);
}
}
}
}