This is just a simple chat client to work with the Chat server discussed in earlier post. it is a winform application that contains two textboxes one for message input and other for historical broadcast messages.
public partial class Form1 : Form
{
StringBuilder history;
TcpClient client = new TcpClient();
NetworkStream clientStream;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
history = new StringBuilder();
try
{
client.Connect(serverEndPoint);
clientStream = client.GetStream();
lblStatus.Text = "Connected to Server";
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
catch { lblStatus.Text = "Unable to Connect to Server"; }
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
var msg = encoder.GetString(message, 0, bytesRead);
System.Diagnostics.Debug.WriteLine(msg);
if (string.IsNullOrWhiteSpace(msg))
SendMessage(msg);
else
{
history.AppendLine(msg);
tbHistory.SetPropertyThreadSafe(() => tbHistory.Text, history.ToString());
}
}
tcpClient.Close();
}
private void btnBroadcast_Click(object sender, EventArgs e)
{
if (clientStream != null && client != null && client.Connected)
{
SendMessage(tbMessage.Text);
tbMessage.Text = string.Empty;
}
}
private void SendMessage(string msg)
{
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(msg);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
private void btnExit_Click(object sender, EventArgs e)
{
if (clientStream != null && client != null && client.Connected)
{
clientStream.Close();
client.Close();
}
Application.Exit();
}
}
public partial class Form1 : Form
{
StringBuilder history;
TcpClient client = new TcpClient();
NetworkStream clientStream;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
history = new StringBuilder();
try
{
client.Connect(serverEndPoint);
clientStream = client.GetStream();
lblStatus.Text = "Connected to Server";
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
catch { lblStatus.Text = "Unable to Connect to Server"; }
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
var msg = encoder.GetString(message, 0, bytesRead);
System.Diagnostics.Debug.WriteLine(msg);
if (string.IsNullOrWhiteSpace(msg))
SendMessage(msg);
else
{
history.AppendLine(msg);
tbHistory.SetPropertyThreadSafe(() => tbHistory.Text, history.ToString());
}
}
tcpClient.Close();
}
private void btnBroadcast_Click(object sender, EventArgs e)
{
if (clientStream != null && client != null && client.Connected)
{
SendMessage(tbMessage.Text);
tbMessage.Text = string.Empty;
}
}
private void SendMessage(string msg)
{
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(msg);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
private void btnExit_Click(object sender, EventArgs e)
{
if (clientStream != null && client != null && client.Connected)
{
clientStream.Close();
client.Close();
}
Application.Exit();
}
}