In this article we are going to see how to create a chat application using C#, for this we have to create a two projects in Console Applications one for client and another for server.
please dis-connet the internet while checking with this code.
Server:
First Let we see about Server, Which will get the request from client .Get the IP address of the Server Machine and Get the username and Port at which server has to watch for request.Socket is the used to receive and accept the request in TCP as stream.
C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatServer
{
class Program
{
static Socket soc;
static Socket acc;
static int port =
9000;
static IPAddress
ip;
static Thread rec;
static string name;
static string GetIp()
{
string hostname = Dns.GetHostName();
IPHostEntry ipentry = Dns.GetHostEntry(hostname);
IPAddress[] addr = ipentry.AddressList;
return addr[addr.Length - 1].ToString();
}
static void RecV()
{
while (true)
{
Thread.Sleep(500);
byte[] buffer = new
byte[300];
int rece = acc.Receive(buffer, 0,
buffer.Length, 0);
Array.Resize(ref
buffer, rece);
Console.WriteLine(Encoding.Default.GetString(buffer));
}
}
static void Main(string[] args)
{
rec = new Thread(RecV);
Console.WriteLine("Your
Local Ip is " + GetIp());
Console.WriteLine("Please
enter your name");
name = Console.ReadLine();
Console.WriteLine("Please
enter HostPort");
string prt = Console.ReadLine();
try
{
port = Convert.ToInt32(prt);
}
catch
{
port = 9000;
}
ip = IPAddress.Parse(GetIp());
soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Bind(new IPEndPoint(ip,
port));
soc.Listen(0);
acc = soc.Accept();
rec.Start();
while (true)
{
byte[] sdata = Encoding.Default.GetBytes("<"+name+">"+Console.ReadLine());
acc.Send(sdata, 0, sdata.Length, 0);
}
}
}
}
Client :
Now we see for the client same steps , but difference is here is send is the request we are make a separate thread for see the message from server.
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatClient
{
class Program
{
static string name;
static int port =
9000;
static IPAddress
ip;
static Socket soc;
static Thread rec;
static string GetIp()
{
string hostname = Dns.GetHostName();
IPHostEntry ipentry = Dns.GetHostEntry(hostname);
IPAddress[] addr = ipentry.AddressList;
return addr[addr.Length - 1].ToString();
}
static void RecV()
{
while (true)
{
Thread.Sleep(500);
byte[] buffer = new
byte[300];
int rece = soc.Receive(buffer, 0,
buffer.Length, 0);
Array.Resize(ref
buffer, rece);
Console.WriteLine(Encoding.Default.GetString(buffer));
}
}
static void Main(string[] args)
{
rec = new Thread(RecV);
Console.WriteLine("Please
enter your name");
name = Console.ReadLine();
ip = IPAddress.Parse(GetIp());
Console.WriteLine("Please
enter HostPort");
string prt = Console.ReadLine();
try
{
port = Convert.ToInt32(prt);
}
catch
{
port = 9000;
}
soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(new IPEndPoint(ip,
port));
rec.Start();
byte[] data = Encoding.Default.GetBytes("<" + name + ">
Connected");
soc.Send(data, 0, data.Length, 0);
while (soc.Connected)
{
byte[] sdata = Encoding.Default.GetBytes("<" + name + ">"
+ Console.ReadLine());
soc.Send(sdata, 0, sdata.Length, 0);
}
}
}
}
Output:
Server ;
Client :
From this article , I hope you can learn how to create a sample chat application.


