1 Attachment(s)
SimpleChat - Asyncronous Server/Client
Here's what I called 'SimpleChat' as an example application for (very simple) asyncronous/thread programming.
Complete project is attached. If you're looking for ways to learn from it maybe you could start by letting people set their own names :)
Server:
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SimpleChatServer
{
class Program
{
public static ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
public static TcpListener rawServer = null;
public static Dictionary<string, TcpClient> handles = new Dictionary<string, TcpClient>();
static void Main(string[] args)
{
#region Start Server
try
{
Console.WriteLine("TCP Server: Attempt Connection...");
rawServer = new TcpListener(IPAddress.Parse("127.0.0.1"), 5020);
Console.WriteLine("TCP Server: Started");
}
catch (Exception e)
{
Console.WriteLine("TCP Server [ERROR]: " + e.Message);
Console.Read();
Environment.Exit(1);
}
#endregion
#region Accept Clients
while (true)
{
rawServer.Start();
tcpClientConnected.Reset();
Console.WriteLine("TCP Server: Waiting...");
rawServer.BeginAcceptTcpClient(new AsyncCallback(acceptTcpClientCallback), rawServer);
tcpClientConnected.WaitOne();
}
#endregion
}
public static void acceptTcpClientCallback(IAsyncResult ar)
{
TcpListener listener = (TcpListener)ar.AsyncState;
TcpClient cli = rawServer.EndAcceptTcpClient(ar);
Thread acceptThread = new Thread(new ParameterizedThreadStart(clientStart));
acceptThread.Start(cli);
tcpClientConnected.Set();
}
public static void sendToAll(string msg)
{
StreamWriter oSw;
foreach (TcpClient oCli in handles.Values)
{
oSw = new StreamWriter(oCli.GetStream());
oSw.WriteLine(msg);
oSw.Flush();
}
}
public static void sendToAll(string msg, string skipMe)
{
StreamWriter oSw;
foreach (KeyValuePair<string, TcpClient> kvp in handles)
{
if (kvp.Key == skipMe) continue;
oSw = new StreamWriter(kvp.Value.GetStream());
oSw.WriteLine(msg);
oSw.Flush();
}
}
public static void clientStart(object clien)
{
TcpClient cli = (TcpClient)clien;
StreamReader sr = new StreamReader(cli.GetStream());
StreamWriter sw = new StreamWriter(cli.GetStream());
try
{
IPEndPoint ipe = (IPEndPoint)cli.Client.RemoteEndPoint;
Console.WriteLine("New Client: " + ipe.Address + ":" + ipe.Port);
// Let's set a name
string name = "Guest " + handles.Count;
sendToAll("> " + name + " connected.", name);
handles.Add(name, cli);
while (true)
{
string read = sr.ReadLine();
sendToAll(name + ": " + read, name);
}
}
catch (Exception e)
{
Console.WriteLine("> Exception: " + e.Message);
Console.WriteLine("> Exception Source: " + e.Source);
foreach (string str in e.Data.Values)
Console.WriteLine(str);
}
}
}
}
Client:
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SimpleChat
{
class Program
{
static StreamReader sr;
static StreamWriter sw;
static TcpClient cli;
static void Main(string[] args)
{
Console.WriteLine("Welcome to SimpleChat");
cli = new TcpClient();
cli.Connect(IPAddress.Parse("127.0.0.1"), 5020);
sr = new StreamReader(cli.GetStream());
sw = new StreamWriter(cli.GetStream());
Thread readingThr = new Thread(new ThreadStart(recThread));
readingThr.Start();
while (true)
{
string toSend = Console.ReadLine();
sw.WriteLine(toSend);
sw.Flush();
Console.WriteLine("You: " + toSend);
}
}
static void recThread()
{
while (true)
{
string read = sr.ReadLine();
Console.WriteLine(read);
}
}
}
}