This is a point-to-point chat tool, is a good example of learning socket + ThreadPool. In the implementation can also learn the method delegate, event processing, window programming and so on concepts, is a more comprehensive and easy to learn examples. The code is as follows:
Using System;
Using System.IO;
Using System.Text;
Using System.Threading;
Using System.Net;
Using System.Net.Sockets;
Using System.Drawing;
Using System.Windows.Forms;
Class APP
{
Entry Point
public static void Main (string[] args)
{
If the args parse in known way then run the application
if (Parseargs (args))
{
Create a custom Talker object
Talker talker = new Talker (endPoint, client);
Pass the object reference to a new form object
Talkform form = new Talkform (talker);
Start the talker "talking"
Talker. Start ();
Run The applications message pump
Application.Run (form);
}
}
Parsed Argument Storage
private static IPEndPoint EndPoint;
private static bool client;
Parse Command Line arguments
private static bool Parseargs (string[] args)
{
Try
{
if (args. Length = = 0)
{
Client = false;
EndPoint = new IPEndPoint (Ipaddress.any, 5150);
return true;
}
Switch (Char.ToUpper (args[0] [1])
{
Case ' L ':
int port = 5150;
if (args. Length > 1)
{
Port = Convert.ToInt32 (args[1]);
}
EndPoint = new IPEndPoint (Ipaddress.any, Port);
Client = false;
break;
Case ' C ':
Port = 5150;
String address = "127.0.0.1";
Client = true;
if (args. Length > 1)
{
Address = args[1];
Port = Convert.ToInt32 (args[2]);
}
EndPoint = new IPEndPoint (dns.resolve address). Addresslist[0], port);
EndPoint = new IPEndPoint (dns.gethostentry address). addresslist[0], port);
break;
Default:
Showusage ();
return false;
}
}
Catch
{
Showusage ();
return false;
}
return true;
}
Show sample Usage
private static void Showusage ()
{
MessageBox.Show ("wintalk [switch] [parameters ...]" + "/l [Port]--listens on a port." default:5150 "+
"/C [address] [port]--connects to a address and port." +
"Example Server-" +
"WINTALK/L" +
"Example Client-" +
"Wintalk/c servermachine 5150", "WinTalk Usage");
}
}
UI Class for the sample
Class Talkform:form
{
Public Talkform (talker talker)
{
Associate with the Talker object
this. Talker = talker;
Talker. Notifications + = new
Talker.notificationcallback (handletalkernotifications);
Create a UI elements
Splitter talksplitter = new Splitter ();
Panel talkpanel = new Panel ();
Receivetext = new TextBox ();
SendText = new TextBox ();
We'll support up to 64k data in our text box controls
Receivetext.maxlength = Sendtext.maxlength = 65536;
StatusText = new Label ();
Initialize UI Elements
Receivetext.dock = Dockstyle.top;
Receivetext.multiline = true;
Receivetext.scrollbars = Scrollbars.both;
Receivetext.size = new Size (506, 192);
Receivetext.tabindex = 1;
Receivetext.text = "";
Receivetext.wordwrap = false;
Receivetext.readonly = true;
Talkpanel.anchor = (Anchorstyles.top | Anchorstyles.bottom
| Anchorstyles.left | Anchorstyles.right);
TalkPanel.Controls.AddRange (new control[] {sendtext,
Talksplitter,
Receivetext});
Talkpanel.size = new Size (506, 371);
Talkpanel.tabindex = 0;
Talksplitter.dock = Dockstyle.top;
Talksplitter.location = new Point (0, 192);
Talksplitter.size = new Size (506, 6);
Talksplitter.tabindex = 2;
Talksplitter.tabstop = false;
Statustext.dock = Dockstyle.bottom;
Statustext.location = new Point (0, 377);
Statustext.size = new Size (507, 15);
Statustext.tabindex = 1;
Statustext.text = "Status:";
Sendtext.dock = DockStyle.Fill;
Sendtext.location = new Point (0, 198);
Sendtext.multiline = true;
Sendtext.scrollbars = Scrollbars.both;
Sendtext.size