This is the start of the server code using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; namespace csharpserver / <summary> / Summary description for Form1. / </summary> public class Form1 : System.Windows.Forms.Form private System.Windows.Forms.ListBox conlist; private System.Windows.Forms.ListBox namelist; private System.Windows.Forms.Button exitbutton; / <summary> / Required designer variable. / </summary> private System.ComponentModel.Container components = null; public Form1() Required for Windows Form Designer support InitializeComponent(); TODO: Add any constructor code after InitializeComponent call / <summary> / Clean up any resources being used. / </summary> protected override void Dispose( bool disposing ) if( disposing )
if (components!= null) components.dispose(); base.dispose( disposing ); #region Windows Form Designer generated code / <summary> / Required method for Designer support - do not modify / the contents of this method with the code editor. / </summary> Thread listenthread; Thread[] clithread; int newcon; Socket[] ssock; Socket tcplist; private void InitializeComponent() this.conlist = new System.Windows.Forms.ListBox(); this.namelist = new System.Windows.Forms.ListBox(); this.exitbutton = new System.Windows.Forms.Button(); this.suspendlayout(); conlist this.conlist.items.addrange(new object[] "empty", 24); 24); 160); (152, 208); "empty"); this.conlist.location = new System.Drawing.Point(24, this.conlist.name = "conlist"; this.conlist.size = new System.Drawing.Size(160, 160); this.conlist.tabindex = 0; namelist this.namelist.location = new System.Drawing.Point(232, this.namelist.name = "namelist"; this.namelist.size = new System.Drawing.Size(176, this.namelist.tabindex = 1; exitbutton this.exitbutton.location = new System.Drawing.Point
this.exitbutton.name = "exitbutton"; this.exitbutton.size = new System.Drawing.Size(136, 40); this.exitbutton.tabindex = 2; this.exitbutton.text = "Exit"; this.exitbutton.click += new System.EventHandler (this.exitbutton_click); Form1 this.autoscalebasesize = new System.Drawing.Size(5, 13); this.clientsize = new System.Drawing.Size(440, 266); this.controls.addrange(new System.Windows.Forms.Control[] this.exitbutton, this.namelist, _Closing); this.conlist); this.name = "Form1"; this.text = "Form1"; this.load += new System.EventHandler(this.Form1_Load); this.closing += new CancelEventHandler(this.Form1 this.resumelayout(false); #endregion / <summary> / The main entry point for the application. / </summary> [STAThread] static void Main() Application.Run(new Form1()); private void Form1_Load(object sender, EventArgs e) int i; conlist.items.clear(); namelist.items.clear(); clithread = new Thread[50]; ssock = new Socket[50]; for (i=0;i<50;i++) conlist.items.add("no Connection"); namelist.items.add("");
(listenproc)); listenthread = new Thread(new ThreadStart listenthread.start(); private void Form1_Closing(object sender, CancelEventArgs e) int i; byte[] bytes; string byemessage; byemessage="bye@"; bytes=encoding.ascii.getbytes(byemessage); for (i=0;i<50;i++) if (namelist.items[i].tostring()!= "") ssock[i].send(bytes); clithread[i].abort(); tcplist.close(); listenthread.abort(); public void cliproc() int clientno; clientno = newcon; listenthread.resume(); string data,newdata; int bytesrec,dataptr; byte[] bytes; string towho,fromwho; string tempstring,message; int firstat,lendata,curlen,i; fromwho = namelist.items[clientno].tostring() + "@"; recvnext:
while(ssock[clientno].available == 0); bytesrec = ssock[clientno].receive(bytes); data = Encoding.ASCII.GetString(bytes); firstat = data.indexof("@"); tempstring = data.substring(0,firstat); lendata = Int32.Parse(tempstring); curlen = bytesrec; while (curlen<lendata) bytesrec = ssock[clientno].receive(bytes); data += Encoding.ASCII.GetString(bytes); curlen += bytesrec; dataptr = data.indexof("@",firstat+1); towho = data.substring(firstat+1,dataptr-firstat-1); if (towho=="bye") namelist.items.insert(clientno,""); namelist.items.removeat(clientno+1); conlist.items.insert(clientno,"no Connection"); conlist.items.removeat(clientno+1); data = "names@"; (); for (i=0;i<50;i++) if (namelist.items[i].tostring()!= "") data += namelist.items[i].tostring data += "@"; for (i=0;i<50;i++) if (namelist.items[i].tostring()!= "") ssock[i].send (Encoding.ASCII.GetBytes(data));
clithread[clientno].abort(); if (towho!= "bye") message = data.substring(dataptr+1,lendatadataptr-1); i = 0; while (namelist.items[i].tostring()!= towho) i++; newdata = fromwho + message; bytes = Encoding.ASCII.GetBytes(newdata); ssock[i].send(bytes); goto recvnext; public void listenproc() IPHostEntry iphostinfo = Dns.Resolve(Dns.GetHostName ()); IPAddress ipaddress = iphostinfo.addresslist[0]; IPEndPoint localendpoint = new IPEndPoint(ipAddress, 13); tcplist = new Socket (AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); keeplistening: tcplist.blocking=true; tcplist.bind(localendpoint); tcplist.listen(10); Connection") newcon=0; while( conlist.items[newcon].tostring()!= "No
; newcon++; ssock[newcon]=tcplist.accept(); conlist.items.remove(newcon); conlist.items.insert(newcon,((ipendpoint)ssock [newcon].remoteendpoint).address.tostring()); byte[] bytes; int bytesrec; string data; bytesrec = ssock[newcon].receive(bytes); data = Encoding.ASCII.GetString(bytes,0,bytesrec); namelist.items.remove(newcon); namelist.items.insert(newcon,data); int i; data = "names@"; for (i=0;i<50;i++) if (namelist.items[i].tostring()!= "") data += namelist.items[i].tostring()+"@"; bytes = Encoding.ASCII.GetBytes(data); for (i=0;i<50;i++) if (namelist.items[i].tostring()!= "") ssock[i].send(bytes); (cliproc)); clithread[newcon] = new Thread(new ThreadStart clithread[newcon].start(); listenthread.suspend(); goto keeplistening; private void exitbutton_click(object sender, System.EventArgs e)
int i; byte[] bytes; string byemessage; byemessage="bye@"; bytes=encoding.ascii.getbytes(byemessage); for (i=0;i<50;i++) if (namelist.items[i].tostring()!= "") ssock[i].send(bytes); clithread[i].abort(); tcplist.close(); listenthread.abort(); Application.Exit(); This is where the client code begins using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; namespace csharpclient
/ <summary> / Summary description for Form1. / </summary> public class Form1 : System.Windows.Forms.Form / <summary> / Required designer variable. / </summary> private System.ComponentModel.Container components = null; public Form1() Required for Windows Form Designer support InitializeComponent(); TODO: Add any constructor code after InitializeComponent call / <summary> / Clean up any resources being used. / </summary> protected override void Dispose( bool disposing ) if( disposing ) if (components!= null) components.dispose(); base.dispose( disposing ); #region Windows Form Designer generated code / <summary> / Required method for Designer support - do not modify / the contents of this method with the code editor. / </summary> / Socket csock; Thread recvthread; IPEndPoint endpoint; private System.Windows.Forms.TextBox yourname; private System.Windows.Forms.Button cbut; private System.Windows.Forms.TextBox sendbox; private System.Windows.Forms.Button sendbut;
private System.Windows.Forms.TextBox recbox; private System.Windows.Forms.ListBox theirnames; 40); private void InitializeComponent() this.yourname = new System.Windows.Forms.TextBox(); this.cbut = new System.Windows.Forms.Button(); this.sendbox = new System.Windows.Forms.TextBox(); this.sendbut = new System.Windows.Forms.Button(); this.recbox = new System.Windows.Forms.TextBox(); this.theirnames = new System.Windows.Forms.ListBox(); this.suspendlayout(); yourname this.yourname.location = new System.Drawing.Point(40, this.yourname.name = "yourname"; this.yourname.size = new System.Drawing.Size(248, 20); this.yourname.tabindex = 0; this.yourname.text = "bruce"; cbut this.cbut.location = new System.Drawing.Point(328, 32); this.cbut.name = "cbut"; this.cbut.size = new System.Drawing.Size(152, 48); this.cbut.tabindex = 1; this.cbut.text = "button1"; this.cbut.click += new System.EventHandler (this.cbut_click); sendbox this.sendbox.location = new System.Drawing.Point(24, 176); this.sendbox.multiline = true; this.sendbox.name = "sendbox"; this.sendbox.size = new System.Drawing.Size(512, 184); this.sendbox.tabindex = 2; this.sendbox.text = "textbox2"; sendbut this.sendbut.location = new System.Drawing.Point(32, 368); this.sendbut.name = "sendbut"; this.sendbut.size = new System.Drawing.Size(496, 32); this.sendbut.tabindex = 3; this.sendbut.text = "button2"; this.sendbut.click += new System.EventHandler (this.sendbut_click);
416); (32, 80); 56); recbox this.recbox.acceptsreturn = true; this.recbox.location = new System.Drawing.Point(32, this.recbox.multiline = true; this.recbox.name = "recbox"; this.recbox.size = new System.Drawing.Size(488, 184); this.recbox.tabindex = 4; this.recbox.text = "textbox3"; theirnames this.theirnames.location = new System.Drawing.Point this.theirnames.name = "theirnames"; this.theirnames.size = new System.Drawing.Size(272, this.theirnames.tabindex = 5; Form1 this.autoscalebasesize = new System.Drawing.Size(5, 13); this.clientsize = new System.Drawing.Size(568, 629); this.controls.addrange(new System.Windows.Forms.Control[] #endregion this.theirnames, this.recbox, this.sendbut, this.sendbox, this.cbut, this.yourname); this.name = "Form1"; this.text = "Form1"; this.load += new System.EventHandler(this.Form1_Load); this.resumelayout(false); / <summary> / The main entry point for the application. / </summary> [STAThread] static void Main()
Application.Run(new Form1()); private void Form1_Load(object sender, System.EventArgs e) cbut.text = "Connect"; yourname.text = "bruce"; recbox.text = ""; sendbox.text = ""; sendbut.text = "Send"; theirnames.items.clear(); endpoint = new IPEndPoint(Dns.Resolve ("Newdig1").AddressList[0],13); public void recvproc() int recbytes,tpoint,npoint; byte[] bytes; string data,tempstring,crstring; int atptr; byte[] cr; cr = new byte[2]; cr[0] = 10; cr[1] = 13; cr = Encoding.ASCII.GetBytes("\n"); crstring = Encoding.ASCII.GetString(cr); recvloop: while (csock.available==0); recbytes = csock.receive(bytes); data = Encoding.ASCII.GetString(bytes); atptr = data.indexof("@"); tempstring = data.substring(0,atptr); MessageBox.Show(data); if (tempstring == "names") tpoint = 6; theirnames.items.clear(); while (tpoint<recbytes)
- tpoint); endnameget: npoint = 1; npoint = data.indexof("@",tpoint+1); if (npoint == -1) tpoint = recbytes; goto recvloop; tempstring = data.substring(tpoint,npoint theirnames.items.add(tempstring); tpoint=npoint+1; goto recvloop; if (tempstring == "bye") MessageBox.Show("The Server has Left"); csock.shutdown(socketshutdown.both); csock.close(); theirnames.items.clear(); cbut.text = "Connect"; recvthread.abort(); if (tempstring!="bye" tempstring!="names") atptr = data.indexof("@"); tempstring = data.substring(0,atptr); "+tempstring+"\r\n"); recbox.appendtext("received from : tempstring = data.substring(atptr+1,recbytesatptr-1); recbox.appendtext(tempstring+"\r\n\r\n"); goto recvloop; private void cbut_click(object sender, System.EventArgs e) int recbytes; byte[] bytes; string data;
if (yourname.text == "") MessageBox.Show("You Must Specify a Username"); goto endcbutsub; if (cbut.text == "Connect") cbut.text = "Disconnect"; csock = new Socket (AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); csock.connect(endpoint); bytes = Encoding.ASCII.GetBytes(yourname.Text); csock.send(bytes); recvthread = new Thread(new ThreadStart (recvproc)); recvthread.isbackground = true; recvthread.start(); goto endcbutsub; if (cbut.text == "Disconnect") endcbutsub: data = "6@bye@"; bytes = Encoding.ASCII.GetBytes(data); csock.send(bytes); csock.shutdown(socketshutdown.both); cbut.text = "Connect"; recvthread.abort(); theirnames.items.clear(); ; MessageBox.Show("LOgged In"); e) private void sendbut_click(object sender, System.EventArgs string towho,lendata,message,data; int lenint; byte[] bytes; To"); if (theirnames.selectedindex == -1) MessageBox.Show("You Must Select a User to Send
goto endsend; towho = theirnames.items [theirnames.selectedindex].tostring()+"@"; message = towho+sendbox.text; lenint = message.length; if (lenint<7) lenint = lenint+2; goto nextstep; if (lenint<96 ) lenint = lenint+3; goto nextstep; if (lenint<995) lenint = lenint + 4; goto nextstep; nextstep: lendata = lenint.tostring()+"@"; data = lendata+message; MessageBox.Show(data); bytes = Encoding.ASCII.GetBytes(data); csock.send(bytes); endsend: ;