A Chat Client/Server Program for C#

Here's some code for a chat server, and an accompanying client program.

The client:


using System.IO;
using System.Net;
using System;
using System.Threading;
using N = System.Net;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;

class TalkUser {
   
   static Form talk;
   static N.Sockets.TcpClient TC;

   [DllImport("kernel32.dll")]
   private static extern void ExitProcess(int a);
   
   public static void Main() {
       talk = new Form();
       talk.Text = "TalkUser - The OFFICIAL TalkServ Client";
       talk.Closing += new CancelEventHandler(talk_Closing);
       talk.Controls.Add(new TextBox());
       talk.Controls[0].Dock = DockStyle.Fill;
       talk.Controls.Add(new TextBox());
       talk.Controls[1].Dock = DockStyle.Bottom;
       ((TextBox)talk.Controls[0]).Multiline = true;
       ((TextBox)talk.Controls[1]).Multiline = true;
       talk.WindowState = FormWindowState.Maximized;
       talk.Show();
       ((TextBox)talk.Controls[1]).KeyUp += new KeyEventHandler(key_up);
       TC = new N.Sockets.TcpClient();
       TC.Connect("IP OF A SERVER HERE",4296);
       Thread t = new Thread(new ThreadStart(run));
       t.Start();
       while(true) {
           Application.DoEvents();
       }
   }
   
   private static void talk_Closing(object s, CancelEventArgs e) {
       e.Cancel = false;
       Application.Exit();
       ExitProcess(0);
   }
   
   private static void key_up(object s,KeyEventArgs e) {
       TextBox TB = (TextBox)s;
       if(TB.Lines.Length>1) {
           StreamWriter SW = new StreamWriter(TC.GetStream());
           SW.WriteLine(TB.Text);
           SW.Flush();
           TB.Text = "";
           TB.Lines = null;
       }
   }
   
   private static void run() {
       StreamReader SR = new StreamReader(TC.GetStream());
       while(true) {
           Application.DoEvents();
           TextBox TB = (TextBox)talk.Controls[0];
           TB.AppendText(SR.ReadLine()+"\r\n");
           TB.SelectionStart = TB.Text.Length;
       }
   }
}

And the server:

using System.IO;
using System.Net;
using System;
using System.Threading;
using N = System.Net;
using System.Collections;

class TalkServ {
   
   System.Net.Sockets.TcpListener server;
   public static Hashtable handles;
   public static Hashtable handleByConnect;
   
   public static void Main() {
       TalkServ TS = new TalkServ();
   }

   public TalkServ() {
       handles = new Hashtable(100);
       handleByConnect = new Hashtable(100);
       server = new System.Net.Sockets.TcpListener(4296);
       while(true) {
           server.Start();
           if(server.Pending()) {
               N.Sockets.TcpClient connection = server.AcceptTcpClient();
               Console.WriteLine("Connection made");
               BackForth BF = new BackForth(connection);            
           }
       }
   }
   
   public static void SendToAll(string name,string msg) {
       StreamWriter SW;
       ArrayList ToRemove = new ArrayList(0);
       N.Sockets.TcpClient[] tc = new N.Sockets.TcpClient[TalkServ.handles.Count];
       TalkServ.handles.Values.CopyTo(tc,0);
       for(int i=0;i<tc.Length;i++) {
           try {
           if(msg.Trim()==""||tc[i]==null)
               continue;
           SW = new StreamWriter(tc[i].GetStream());
           SW.WriteLine(name + ": " + msg);
           SW.Flush();
           SW = null;
           } catch(Exception e44) { e44 = e44;
               string g = (string) TalkServ.handleByConnect[tc[i]];
               TalkServ.SendSysMsg("** " + g + " ** HAS LEFT US.");
               TalkServ.handles.Remove(g);
               TalkServ.handleByConnect.Remove(tc[i]);
           }
       }
   }

   public static void SendSysMsg(string msg) {
       StreamWriter SW;
       ArrayList ToRemove = new ArrayList(0);
       N.Sockets.TcpClient[] tc = new N.Sockets.TcpClient[TalkServ.handles.Count];
       TalkServ.handles.Values.CopyTo(tc,0);
       for(int i=0;i<tc.Length;i++) {
           try {
           if(msg.Trim()==""||tc[i]==null)
               continue;
           SW = new StreamWriter(tc[i].GetStream());
           SW.WriteLine(msg);
           SW.Flush();
           SW = null;
           } catch(Exception e44) { e44 = e44;
               TalkServ.handles.Remove(TalkServ.handleByConnect[tc[i]]);
               TalkServ.handleByConnect.Remove(tc[i]);
           }
       }
   }
}//end of class TalkServ

class BackForth {
   N.Sockets.TcpClient client;
   System.IO.StreamReader SR;
   System.IO.StreamWriter SW;
   string handle;
   
   public BackForth(System.Net.Sockets.TcpClient c) {
       client = c;
       Thread t = new Thread(new ThreadStart(init));
       t.Start();
   }
   
   private string GetHandle() {
       SW.WriteLine("What is your handle? ");
       SW.Flush();
       return SR.ReadLine();
   }
   
   private void run() {
       try {
       string l = "";
       while(true) {
           l = SR.ReadLine();
           TalkServ.SendToAll(handle,l);
       }
       } catch(Exception e44) { Console.WriteLine(e44); }
   }
   
   private void init() {
       SR = new System.IO.StreamReader(client.GetStream());
       SW = new System.IO.StreamWriter(client.GetStream());
       SW.WriteLine("WELCOME TO TalkServ! Be Nice!");
       //SW.WriteLine("What is your handle? ");
       //SW.Flush();
       handle = GetHandle();
       while(TalkServ.handles.Contains(handle)) {
           SW.WriteLine("ERR - Handle already exists!");
           handle = GetHandle();
       }
       TalkServ.handles.Add(handle,client);
       TalkServ.handleByConnect.Add(client,handle);
       TalkServ.SendSysMsg("** " + handle + " ** HAS JOINED US.");
       SW.WriteLine("Now Talking.....\r\n-------------------------------");
       SW.Flush();
       Thread t = new Thread(new ThreadStart(run));
       t.Start();
   }
}

You might also like...

Comments

Michael H

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry