下 :客户机
namespace clientWindowsApplication8
public class Form1 : System.Windows.Forms.Form
{
private Socket serverSocket,clintSocket;
private const int dataSize=1024;
private byte[] data=new byte[dataSize];
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ListBox listBoxState;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBoxName;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBoxPort;
private System.Windows.Forms.RichTextBox richTextBoxSend;
private System.Windows.Forms.Button buttonRequest;
private System.Windows.Forms.Button buttonSend;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.RichTextBox richTextBoxReceive;

private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
this.textBoxName.Text=Dns.GetHostName();
this.textBoxPort.Text="6788";
this.listBoxState.Items.Clear();
this.richTextBoxReceive.Text="";
this.richTextBoxSend.Text=""; } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing )
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void buttonRequest_Click(object sender, System.EventArgs e)
{
IPAddress myIP;
IPEndPoint iep;
try
{
IPHostEntry myHost=new IPHostEntry();
myHost=Dns.GetHostByName(this.textBoxName.Text);
myIP=IPAddress.Parse(myHost.AddressList[0].ToString());
iep=new IPEndPoint (myIP,Int32.Parse(this.textBoxPort.Text));
}
catch
{
MessageBox.Show("输入的服务名或端口号格式不正确,请重新输入!");
return;
}
serverSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
serverSocket.BeginConnect(iep,new AsyncCallback(ConnectServer),clintSocket);
}
private void ConnectServer(IAsyncResult ar)
{
clintSocket=(Socket)ar.AsyncState;
try
{
clintSocket.EndConnect(ar);
this.listBoxState.Items.Add("与服务器"+clintSocket.RemoteEndPoint.ToString()+"建立连接.");
clintSocket.BeginReceive(data,0,dataSize,SocketFlags.None,new AsyncCallback(ReceiveData),clintSocket);
}
catch(Exception Ex)
{
MessageBox.Show("与服务器连接失败" + Ex.Message);
}
}
private void ReceiveData(IAsyncResult ar)
{
try
{
Socket Server=(Socket)ar.AsyncState;
int receiveDataLength=Server.EndReceive(ar);
string str=System.Text.Encoding.Unicode.GetString(data,0,receiveDataLength);
this.richTextBoxReceive.Text=str;
}
catch
{ }
} private void buttonSend_Click(object sender, System.EventArgs e)
{
try
{
byte[] message=System.Text.Encoding.Unicode.GetBytes(this.richTextBoxSend.Text);
this.richTextBoxSend.Clear();
clintSocket.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),clintSocket);
}
catch
{
               MessageBox.Show("尚未与服务器建立连接,发送失败");
}
}
private void SendData(IAsyncResult ar)
{
          Socket socket=(Socket)ar.AsyncState;
          int send=socket.EndSend(ar);
       socket.BeginReceive(data,0,dataSize,SocketFlags.None,new AsyncCallback(ReceiveData),socket);
} private void buttonStop_Click(object sender, System.EventArgs e)
{
try
{
clintSocket.Close();
this.listBoxState.Items.Add("与服务器断开连接!");
}
catch
{
MessageBox.Show("连接尚未开始,断开无效!");
}
}
}
}