建立線程時,將使用採用委託作為其唯一參數的建構函式建立Thread 類的新執行個體。但線程在調用 Start方法前不會開始執行。調用後,將從由委託引用的方法的第一行開始執行。如下例所示:
Thread thread=new Thread(new ThreadStart(ThreadProc));
thread.Start();
在以下的源碼中我們使用了ManualResetEvent 來允許線程通過發訊號互相通訊。通常,此通訊涉及一個線程在其他線程進行之前必須完成的任務。當線程開始一個活動(此活動必須在其他線程進行之前完成)時,它調用 Reset 將 ManualResetEvent 設定為非終止狀態。此線程可被視為控制 ManualResetEvent。調用 ManualResetEvent 上的 WaitOne 的線程將阻塞,並等待訊號。當控制線程完成活動時,它調用 Set 以發出等待線程可以繼續進行的訊號。並釋放所有等待線程。一旦它被終止,將保持終止狀態,直到它被手動重設。即對 WaitOne 的調用將立即返回。可以通過將布爾值傳遞給建構函式來控制 ManualResetEvent 的初始狀態,如果初始狀態處於終止狀態,為 true;否則為 false。
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 聊天_socket
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.StatusBar statusBar1;
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.RichTextBox rtbReceive;
private System.Windows.Forms.RichTextBox rtbSend;
private System.Windows.Forms.TextBox txtServer;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Button btnListen;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.Button btnStop;
private IPAddress hostIPAddress=IPAddress.Parse("127.0.0.1");
private IPEndPoint Server;
private Socket listeningSocket;
private Socket handler;
private Socket mySocket;
private static ManualResetEvent Done=new ManualResetEvent(false);
private const int BufferSize=256;
private byte[] buffer=new byte[BufferSize];
string port;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.rtbReceive = new System.Windows.Forms.RichTextBox();
this.rtbSend = new System.Windows.Forms.RichTextBox();
this.txtServer = new System.Windows.Forms.TextBox();
this.txtPort = new System.Windows.Forms.TextBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.btnListen = new System.Windows.Forms.Button();
this.btnSend = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
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.SuspendLayout();
//
// rtbReceive
//
this.rtbReceive.Location = new System.Drawing.Point(80, 56);
this.rtbReceive.Name = "rtbReceive";
this.rtbReceive.Size = new System.Drawing.Size(264, 96);
this.rtbReceive.TabIndex = 0;
this.rtbReceive.Text = "";
//
// rtbSend
//
this.rtbSend.Location = new System.Drawing.Point(80, 152);
this.rtbSend.Name = "rtbSend";
this.rtbSend.Size = new System.Drawing.Size(264, 96);
this.rtbSend.TabIndex = 1;
this.rtbSend.Text = "";
//
// txtServer
//
this.txtServer.Location = new System.Drawing.Point(72, 16);
this.txtServer.Name = "txtServer";
this.txtServer.TabIndex = 2;
this.txtServer.Text = "127.0.0.1";
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(288, 16);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(48, 21);
this.txtPort.TabIndex = 3;
this.txtPort.Text = "19811";
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 287);
this.statusBar1.Name = "statusBar1";
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(360, 22);
this.statusBar1.TabIndex = 4;
this.statusBar1.Text = "statusBar1";
//
// btnListen
//
this.btnListen.Location = new System.Drawing.Point(32, 256);
this.btnListen.Name = "btnListen";
this.btnListen.TabIndex = 5;
this.btnListen.Text = "開始監聽";
this.btnListen.Click += new System.EventHandler(this.btnListen_Click);
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(144, 256);
this.btnSend.Name = "btnSend";
this.btnSend.TabIndex = 6;
this.btnSend.Text = "發送資訊";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(256, 256);
this.btnStop.Name = "btnStop";
this.btnStop.TabIndex = 7;
this.btnStop.Text = "停止監聽";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 8;
this.label1.Text = "伺服器:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(216, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 9;
this.label2.Text = "監聽連接埠:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 64);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 23);
this.label3.TabIndex = 10;
this.label3.Text = "接收資訊:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 152);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 23);
this.label4.TabIndex = 11;
this.label4.Text = "發送資訊:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 309);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.btnListen);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtServer);
this.Controls.Add(this.rtbSend);
this.Controls.Add(this.rtbReceive);
this.Name = "Form1";
this.Text = "聊天程式-伺服器";
this.TopMost = true;
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnListen_Click(object sender, System.EventArgs e)
{
try
{
hostIPAddress=IPAddress.Parse(txtServer.Text);
port=txtPort.Text;
}
catch{MessageBox.Show("請輸入正確的IP地址格式");}
try
{ //通過組合服務的主機 IP 位址和連接埠號碼,IPEndPoint 類形成到服務的連接點。
Server=new IPEndPoint(hostIPAddress,Int32.Parse(port));
// Create a socket object to establish a connection with the server
listeningSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
listeningSocket.Bind(Server); //綁定該主機連接埠
listeningSocket.Listen(50); //監聽連接埠,等待用戶端串連請求。50是隊列中最多可容納的等待接受的傳入串連數
statusBar1.Text="主機"+txtServer.Text+"連接埠"+txtPort.Text+"開始監聽.....";
//Accept 以同步方式從偵聽通訊端的串連請求隊列中提取第一個掛起的串連請求,然後建立並返回新的 Socket。
//mySocket=listeningSocket.Accept();
//一個進程可以建立一個或多個線程以執行與該進程關聯的部分程式碼。使用 ThreadStart 委託指定由線程執行的程式碼。
Thread thread=new Thread(new ThreadStart(ThreadProc));
thread.Start();
}
catch(Exception ee){statusBar1.Text=ee.Message;}
}
private void ThreadProc()
{
//if(mySocket.Connected)
//{
//statusBar1.Text="與客戶建立串連.";
while(true)
{
/*Byte[] ByteRecv=new Byte[256];
mySocket.Receive(ByteRecv,ByteRecv.Length,0);
string strRecv=Encoding.BigEndianUnicode.GetString(ByteRecv);
rtbReceive.AppendText(strRecv+"\r\n");*/
Done.Reset(); //將狀態設為非終止
listeningSocket.BeginAccept(new AsyncCallback(AcceptCallBack),listeningSocket);//開始一個非同步作業來接受一個傳入的串連嘗試
Done.WaitOne(); //阻塞當前線程,直到當前線程收到訊號。
}
//}
}
private void AcceptCallBack(IAsyncResult ar)//ar表示非同步作業的狀態。
{
Done.Set();//設為終止
mySocket=(Socket)ar.AsyncState; //擷取狀態
handler=mySocket.EndAccept(ar); //非同步接受傳入的串連嘗試,並建立新的 Socket 來處理遠程主機通訊,擷取結果
try
{
byte[] byteData=Encoding.BigEndianUnicode.GetBytes("準備完畢,可以通話"+"\r\n");
//調用SendCallBack非同步發送資料,
handler.BeginSend(byteData,0,byteData.Length,0,new AsyncCallback(SendCallBack),handler);
}
catch(Exception ee){MessageBox.Show(ee.Message);}
Thread thread=new Thread(new ThreadStart(ThreadRev));
thread.Start();
}
private void SendCallBack(IAsyncResult ar)
{
try
{
handler=(Socket)ar.AsyncState; //擷取狀態
int bytesSent=handler.EndSend(ar);//結束掛起的非同步發送,返迴向 Socket 發送的位元組數
}
catch{}
}
private void ThreadRev()
{
handler.BeginReceive(buffer,0,BufferSize,0,new AsyncCallback(ReadCallBack),handler);
}
private void ReadCallBack(IAsyncResult ar)
{
int bytesRead=handler.EndReceive(ar); //結束掛起的非同步讀取,返回接收到的位元組數。
StringBuilder sb=new StringBuilder(); //接收資料的可變字元字串,在通過追加、移除、替換或插入字元而建立它後可以對它進行修改。
sb.Append(Encoding.BigEndianUnicode.GetString(buffer,0,bytesRead));//追加字串
string content=sb.ToString(); //轉換為字串
sb.Remove(0,content.Length); //清除sb內容
rtbReceive.AppendText(content+"\r\n");
handler.BeginReceive(buffer,0,BufferSize,0,new AsyncCallback(ReadCallBack),handler);
}
private void btnStop_Click(object sender, System.EventArgs e)
{
try
{
listeningSocket.Close();
statusBar1.Text="主機"+txtServer.Text+"連接埠"+txtPort.Text+"監聽停止";
}
catch{MessageBox.Show("監聽尚未開始,關閉無效");}
}
private void btnSend_Click(object sender, System.EventArgs e)
{
try
{
string strSend ="Server--->"+rtbSend.Text+"\r\n";
Byte[] ByteSend = Encoding.BigEndianUnicode.GetBytes(strSend);
handler.BeginSend(ByteSend,0,ByteSend.Length,0,new AsyncCallback(SendCallBack),handler);
}
catch{MessageBox.Show("串連尚未建立,無法發送.");}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
listeningSocket.Close();//在視窗關閉之前關閉Scoket串連並釋放所有關聯的資源。
}
catch{}
}
}
}