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 SocketServer
{
public class mainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.StatusBar statusBar;
private System.ComponentModel.Container components = null;
private IPAddress myIP;
private IPEndPoint MyServer;
private Socket sock;
private Socket handler;
private System.Windows.Forms.RichTextBox txtSendMsg;
private System.Windows.Forms.RichTextBox txtMsg;
private static ManualResetEvent Done = new ManualResetEvent(false);
public mainForm()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.txtPort = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnSend = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.statusBar = new System.Windows.Forms.StatusBar();
this.txtSendMsg = new System.Windows.Forms.RichTextBox();
this.txtMsg = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(24, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 0;
this.label1.Text = "服务器:";
this.label2.Location = new System.Drawing.Point(24, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 1;
this.label2.Text = "监听端口:";
this.label3.Location = new System.Drawing.Point(24, 232);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 2;
this.label3.Text = "发送信息:";
this.label4.Location = new System.Drawing.Point(24, 144);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 3;
this.label4.Text = "接收信息:";
this.txtIP.Location = new System.Drawing.Point(96, 24);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(120, 21);
this.txtIP.TabIndex = 4;
this.txtIP.Text = "";
this.txtPort.Location = new System.Drawing.Point(96, 80);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(120, 21);
this.txtPort.TabIndex = 5;
this.txtPort.Text = "";
this.btnStart.Location = new System.Drawing.Point(280, 24);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 8;
this.btnStart.Text = "开始监听";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
this.btnSend.Location = new System.Drawing.Point(280, 64);
this.btnSend.Name = "btnSend";
this.btnSend.TabIndex = 9;
this.btnSend.Text = "发送信息";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.btnStop.Location = new System.Drawing.Point(280, 104);
this.btnStop.Name = "btnStop";
this.btnStop.TabIndex = 10;
this.btnStop.Text = "停止监听";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
this.statusBar.Location = new System.Drawing.Point(0, 312);
this.statusBar.Name = "statusBar";
this.statusBar.Size = new System.Drawing.Size(392, 22);
this.statusBar.TabIndex = 11;
this.txtSendMsg.Location = new System.Drawing.Point(96, 224);
this.txtSendMsg.Name = "txtSendMsg";
this.txtSendMsg.Size = new System.Drawing.Size(280, 72);
this.txtSendMsg.TabIndex = 12;
this.txtSendMsg.Text = "";
this.txtMsg.Location = new System.Drawing.Point(96, 144);
this.txtMsg.Name = "txtMsg";
this.txtMsg.Size = new System.Drawing.Size(280, 72);
this.txtMsg.TabIndex = 13;
this.txtMsg.Text = "";
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 334);
this.Controls.Add(this.txtMsg);
this.Controls.Add(this.txtSendMsg);
this.Controls.Add(this.statusBar);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "mainForm";
this.Text = "聊天程序服务器端";
this.Closed += new System.EventHandler(this.mainForm_Closed);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new mainForm());
}
private void btnStart_Click(object sender, System.EventArgs e)
{
if (txtIP.Text == "")
{
MessageBox.Show("对不起,IP地址不能为空@!", "警告");
txtIP.Focus();
return;
}
if (txtPort.Text == "")
{
MessageBox.Show("对不起,端口不能为空!", "警告");
txtPort.Focus();
return;
}
try
{
myIP = IPAddress.Parse(txtIP.Text.Trim());
}
catch
{
MessageBox.Show("你输入的IP地址格式不正确,请重新输入!", "警告");
}
try
{
MyServer = new IPEndPoint(myIP, Int32.Parse(txtPort.Text.Trim()));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
statusBar.Text = "主机: " + txtIP.Text + " 端口: " + txtPort.Text + "开始监听......";
Thread thread = new Thread(new ThreadStart(StartServer));
thread.Start();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void StartServer()
{
try
{
while (true)
{
Done.Reset();
sock.BeginAccept(new AsyncCallback(AcceptCallBack), sock);
Done.WaitOne();
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void AcceptCallBack(IAsyncResult ar)
{
Done.Set();
Socket listener = (Socket)ar.AsyncState;
handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
statusBar.Text = "与客户端建立连接...";
try
{
byte[] byteData = System.Text.Encoding.BigEndianUnicode.GetBytes("准备完毕,可以通话!" + "\n\r");
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
Thread thread = new Thread(new ThreadStart(rec));
thread.Start();
}
private void SendCallback(IAsyncResult ar)
{
try
{
handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void rec()
{
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket tt = state.workSocket;
int bytesRead = handler.EndReceive(ar);
state.sb.Append(System.Text.Encoding.BigEndianUnicode.GetString(state.buffer, 0, bytesRead));
string content = state.sb.ToString();
state.sb.Remove(0, content.Length);
txtMsg.AppendText(content + "\r\n");
tt.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void btnStop_Click(object sender, System.EventArgs e)
{
try
{
sock.Close();
statusBar.Text = "与客户端断开连接...";
}
catch
{
MessageBox.Show("连接尚未建立,断开无效!", "警告");
}
}
private void btnSend_Click(object sender, System.EventArgs e)
{
try
{
byte[] byteData = System.Text.Encoding.BigEndianUnicode.GetBytes(txtSendMsg.Text + "\n\r");
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void mainForm_Closed(object sender, System.EventArgs e)
{
try
{
sock.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
Application.Exit();
}
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
public StateObject()
{
}
}
}
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 SocketClient
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.ListBox listReceiveMsg;
private System.Windows.Forms.RichTextBox txtSendMsg;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.Button btnExit;
private System.ComponentModel.Container components = null;
private IPEndPoint MyServer;
private Socket sock;
private static ManualResetEvent ConnectDone = new ManualResetEvent(false);
private static ManualResetEvent SendDone = new ManualResetEvent(false);
private System.Windows.Forms.StatusBar statusBar1;
private IPAddress myIP;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.txtPort = new System.Windows.Forms.TextBox();
this.listReceiveMsg = new System.Windows.Forms.ListBox();
this.txtSendMsg = new System.Windows.Forms.RichTextBox();
this.btnConnect = new System.Windows.Forms.Button();
this.btnSend = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 0;
this.label1.Text = "服务器:";
this.label2.Location = new System.Drawing.Point(24, 96);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 1;
this.label2.Text = "请求端口:";
this.label3.Location = new System.Drawing.Point(24, 136);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 2;
this.label3.Text = "接收信息:";
this.label4.Location = new System.Drawing.Point(24, 208);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 23);
this.label4.TabIndex = 3;
this.label4.Text = "发送信息:";
this.txtIP.Location = new System.Drawing.Point(112, 32);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(128, 21);
this.txtIP.TabIndex = 4;
this.txtIP.Text = "";
this.txtPort.Location = new System.Drawing.Point(112, 88);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(128, 21);
this.txtPort.TabIndex = 5;
this.txtPort.Text = "";
this.listReceiveMsg.ItemHeight = 12;
this.listReceiveMsg.Location = new System.Drawing.Point(112, 128);
this.listReceiveMsg.Name = "listReceiveMsg";
this.listReceiveMsg.Size = new System.Drawing.Size(312, 76);
this.listReceiveMsg.TabIndex = 6;
this.txtSendMsg.Location = new System.Drawing.Point(112, 208);
this.txtSendMsg.Name = "txtSendMsg";
this.txtSendMsg.Size = new System.Drawing.Size(312, 96);
this.txtSendMsg.TabIndex = 7;
this.txtSendMsg.Text = "";
this.btnConnect.Location = new System.Drawing.Point(312, 24);
this.btnConnect.Name = "btnConnect";
this.btnConnect.TabIndex = 8;
this.btnConnect.Text = "请求连接";
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
this.btnSend.Location = new System.Drawing.Point(312, 64);
this.btnSend.Name = "btnSend";
this.btnSend.TabIndex = 9;
this.btnSend.Text = "发送消息";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.btnExit.Location = new System.Drawing.Point(312, 96);
this.btnExit.Name = "btnExit";
this.btnExit.TabIndex = 10;
this.btnExit.Text = "关闭连接";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
this.statusBar1.Location = new System.Drawing.Point(0, 312);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(440, 22);
this.statusBar1.TabIndex = 11;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(440, 334);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.txtSendMsg);
this.Controls.Add(this.listReceiveMsg);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "客户端";
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
if (txtIP.Text == "")
{
MessageBox.Show("请输入IP地址", "警告");
txtIP.Focus();
return;
}
if (txtPort.Text == "")
{
MessageBox.Show("请输入端口号", "警告");
txtPort.Focus();
return;
}
try
{
myIP = IPAddress.Parse(txtIP.Text.Trim());
}
catch
{
MessageBox.Show("你输入的IP地址格式不正确,请重新输入", "警告");
}
try
{
MyServer = new IPEndPoint(myIP, Int32.Parse(txtPort.Text.Trim()));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.BeginConnect(MyServer, new AsyncCallback(ConnectCallBack), sock);
ConnectDone.WaitOne();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void ConnectCallBack(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
try
{
byte[] byteData = System.Text.Encoding.BigEndianUnicode.GetBytes("准备完毕,可以通话!" + "\n\r");
sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallBack), sock);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
statusBar1.Text = "与主机 : " + txtIP.Text + "端口: " + txtPort.Text + "建立连接!";
Thread thread = new Thread(new ThreadStart(targett));
thread.Start();
ConnectDone.Set();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void SendCallBack(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
SendDone.Set();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void targett()
{
try
{
StateObject state = new StateObject();
state.workSocket = sock;
sock.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallBack), state);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void ReceiveCallBack(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
int byteData = client.EndReceive(ar);
state.sb.Append(System.Text.Encoding.BigEndianUnicode.GetString(state.buffer, 0, byteData));
string msg = state.sb.ToString();
state.sb.Remove(0, msg.Length);
listReceiveMsg.Items.Add(msg);
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallBack), state);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
try
{
sock.Close();
statusBar1.Text = "与服务器断开连接";
}
catch
{
MessageBox.Show("连接尚未建立,断开无效!", "警告");
}
}
private void btnSend_Click(object sender, System.EventArgs e)
{
try
{
byte[] byteData = System.Text.Encoding.BigEndianUnicode.GetBytes(txtSendMsg.Text);
sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallBack), sock);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void Form1_Closed(object sender, System.EventArgs e)
{
sock.Close();
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
public StateObject()
{
}
}
}