How To C Chat Server
How To C Chat Server
How To C Chat Server
The main idea behind the C# Chat Server is that each Client send
messages to the Server and the Server broadcast the message to all
Clients currently connected to the Chat Server . From the following picture
you can see how a C# TCP Chat Server is handling communication
between Client to Client .
C# chat server example
The C# Chat Server program has two sections.
1. C# Chat Server
2. C# Chat Client
Create the C# Chat Server and C# Chat Client are two separate C#
projects and compile and build the program. Open a DOS prompt and run
the Server Program first and then run the Client program .
In the Client program, Enter a Chat name and click " Connect to Server
" button . Then you can see the message in the Server program User "
Joined Chat Room " . Similarly you can connect more than one Clients at
the same time and start chatting each other. If you plan to run more than
one client, it is better to copy the .exe file in separate folders and run
from the .exe file.
1. Chat Server
2. C# Chat Client
Chat Server
clientsList.Add(dataFromClient, clientSocket);
When the Server gets a message from any client , it select all the Clients
from the clientsList and send the message ( Broadcast ) to all Clients
( ie we can say Broadcast ) in the clientsList . So all Client can see the
message of each others and they can communicate through Server.
When Server gets a message from any of the connected Chat Client , the
Server Broadcast the message to all Clients. Here we implement a
function broadcast for sending messages to all Clients .
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static Hashtable clientsList = new Hashtable();
serverSocket.Start();
Console.WriteLine ("Chat Server Started ....");
counter = 0;
while ((true))
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
clientsList.Add(dataFromClient, clientSocket);
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine("exit");
Console.ReadLine();
}
if (flag == true)
{
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says
: " + msg);
}
else
{
broadcastBytes = Encoding.ASCII.GetBytes(msg);
}
broadcastStream.Write(broadcastBytes, 0,
broadcastBytes.Length);
broadcastStream.Flush();
}
} //end broadcast function
}//end Main class
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0,
(int)clientSocket.ReceiveBufferSize);
dataFromClient =
System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0,
dataFromClient.IndexOf("$"));
Console.WriteLine("From client - " + clNo + " : " +
dataFromClient);
rCount = Convert.ToString(requestCount);
2. C# Chat Client
Create the C# Chat Server and C# Chat Client are two separate C# projects and compile and
build the program. Open a DOS prompt and run the Server Program first and then run the
Client program .
In the Client program, Enter a Chat name and click " Connect to Server " button . Then you
can see the message in the Server program User "Joined Chat Room" . Similarly you can
connect more than one Clients at the same time and start chatting each other. If you plan to
run more than one client, it is better to copy the .exe file in separate folders and run from
the .exe file.
After compile and build the projects, open a DOS prompt and run the
Server Program first. Then you will get the message "Server started" in
Server side.
Next you start the Client program , then you can see the message from
Server . You can start more than one client at the same time and
communicate with the Server program. If you plan to run more than one
client, it is better to run the client program's .exe file to copy in separate
folders and run from .exe file.
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");
counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> " + "Client No:" +
Convert.ToString(counter) + " started!");
handleClinet client = new handleClinet();
client.startClient(clientSocket,
Convert.ToString(counter));
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0,
(int)clientSocket.ReceiveBufferSize);
dataFromClient =
System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0,
dataFromClient.IndexOf("$"));
Console.WriteLine(" >> " + "From client-" + clNo +
dataFromClient);
rCount = Convert.ToString(requestCount);
serverResponse = "Server to clinet(" + clNo + ") " +
rCount;
sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
}
}
}
}
}
Create C# Multithreaded Server Socket Program and C# Multi Threaded Client Socket
Program in two separate C# projects .
After compile and build the projects, open a DOS prompt and run the Server Program
first.Then you will get the message "Server started" in Server side.
Next you start the Client program , then you can see the message from Server . You can start
more than one client at the same time and communicate with the Server program. If you plan
to run more than one client, it is better to run the client program's .exe file to copy in separate
folders and run from .exe file.
clientSocket.Connect("127.0.0.1", 8888);
When the Client gets connected to the C# Server , the Server makes a
separate thread for Client's communication . So we can connect more than
one client and communicate at the same time.
Create a new C# Windows based application and put the following source
code in the Project.
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new
System.Net.Sockets.TcpClient();
NetworkStream serverStream;
public Form1()
{
InitializeComponent();
}
Create C# Multi Threaded Server Socket Program and C# Multithreaded Client Socket
Program in two separate C# projects .
After compile and build the projects, open a DOS prompt and run the Server Program
first.Then you will get the message "Server started" in Server side.
Next you start the Client program , then you can see the message from Server . You can start
more than one client at the same time and communicate with the Server program. If you plan
to run more than one client, it is better to run the client program's .exe file to copy in separate
folders and run from .exe file.
1. C# Chat Server
2. C# Chat Client
In the Client program, Enter a Chat name and click " Connect to Server
" button . Then you can see the message in the Server program User "
Joined Chat Room " . Similarly you can connect more than one Clients at
the same time and start chatting each other. If you plan to run more than
one client, it is better to copy the .exe file in separate folders and run
from the .exe file.
The following C# source code shows how to send an email from a Gmail
address using SMTP server. The Gmail SMTP server name is
smtp.gmail.com and the port using send mail is 587 and also using
NetworkCredential for password based authentication.
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
mail.From = new
MailAddress("[email protected]");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new
System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
You have to provide the necessary information like your gmail username and password etc.