當前位置:編程學習大全網 - 編程語言 - 如何用C#實現多線程TCP協議的服務器端程序

如何用C#實現多線程TCP協議的服務器端程序

用C#實現多線程TCP協議的服務器端程序:

//& lt;總結& gt

/// Tcp客戶端線程類(server),ThreadServerProcessor線程生成的客戶端連接,用於讀寫。

///& lt;/summary & gt;

公共類ThreadClientProcessor

{

//Tcp連接實例

私有TcpClient tcpClient

//消息框,本來想寫日誌的。

私人系統。windows . forms . listbox message list;

私有字符串密碼;//此連接的登錄密碼

私有字符串cmd 1 echo;

私有字符串Cmd2Echo

私有bool ClientLogOn//客戶登錄了嗎?

私有bool TcpClose

公共ThreadClientProcessor(){}

//構造函數,參數解釋:Tcp客戶端,消息框,服務密碼(密碼命令後的參數),命令響應字符串1,2 ******************。

公共thread client processor(TCP client客戶端,ListBox listBox,string LogonText,string cmd1echo,string cmd2echo)

{

客戶列表。補充(這個);//將當前實例添加到列表中,以便以後控制。

this.tcpClient = client

這個。MessageList = listBox

這個。Password = LogonText

這個。cmd 1 echo = cmd 1 echo;

這個。cmd 2 echo = cmd 2 echo;

這個。ClientLogOn = false

這個。TcpClose = false

}

public static char[]CmdSplit = { ' ' };//讀取的字符串由“”分隔,並命名為+“”+參數。

//public const string[]Cmd = new string[]{ " password "," cmd1 "," cmd2 "," echo "," bye " };

//這個函數是自己寫的。這只是壹個例子。

//功能:命令處理器,給出命令字符串,返回命令的處理結果,將命令和處理結果放在文本文件中,方便系統升級。

公共字符串TcpCmd(字符串s)

{

字符串結果;

嘗試

{

string cmd arg = s . Trim();

string[] args=cmdarg。split(CmdSplit);

字符串cmd=args[0]。to lower();

開關(cmd)

{

大小寫“密碼”:

if (args。長度& gt1)

{

ClientLogOn=密碼。Equals(args[1]。trim());

result=ClientLogOn?“登錄成功”:“密碼不正確,未登錄”;

}

Else result= "登錄時未輸入密碼";

打破;

案例“cmd1”:

result=ClientLogOn?這個。Cmd1Echo:“此命令無權執行,請先登錄”;

打破;

案例“cmd2”:

result=ClientLogOn?這個。Cmd2Echo:“此命令無權執行,請先登錄”;

打破;

案例“回聲”:

結果=字符串。Format("服務器響應:\n {0} ",s);

打破;

案例“再見”:

這個。TcpClose = true

result= "斷開連接";

打破;

默認值:

Result= "無法識別的命令";

打破;

}

}

捕捉

{

Result= "解析命令出錯,妳輸入了壹個扯淡的命令,TMD * * ";

}

返回結果;

}//結束命令

//定義壹個線程,線程對應的函數是void start()(not start())* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *。

//接下來的程序主要是操作線程。

公共系統。threading . Thread tcpClientThread;

//啟動客戶連接線程* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *。

公共void開始()

{

tcpClientThread = new Thread(new ThreadStart(start));

tcpClientThread。優先級=線程優先級。低於正常水平;

tcpClientThread。start();

}

//斷開當前實例並終止線程* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *。

公共void中止()

{

if (this.tcpClientThread!=空)

{

//tcpClientThread。中斷();

tcpClientThread。abort();

//壹定要等壹段時間,以為後面的tcpClient。Close()會影響NetWorkStream的運行。

線程。睡眠(TimeSpan。from milliseconds(100));

tcpClient。close();

}

}

//靜態列表,包含每個連接實例(ArrayList。構造實例時使用了Add( object)

私有靜態系統。collections . ArrayList client list = new ArrayList();

//斷開所有Tcp客戶端,靜態方法為* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *。

公共靜態void AbortAllClient()

{

for(int j = 0;j & lt客戶列表。數數;j++)

{

//從實例列表中獲取壹個對象,並將其轉換為ThreadClientProcessor對象。

ThreadClientProcessor o =(ThreadClientProcessor)client list[j];

//調用ThreadClientProcessor對象的stop方法

o.abort();

}

//清除連接列表

客戶列表。clear();

}

//線程的讀寫連接函數/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *。

私有void開始()

{

byte[]buf = new byte[1024 * 1024];//1MB的預定義緩沖區

int Len = 0;//流的實際長度

network stream network stream = TCP client。GetStream();//建立壹個讀寫Tcp的流。

嘗試

{

byte[]p =編碼。UTF8.GetBytes("歡迎,請輸入您的密碼");

//向Tcp連接寫入歡迎消息。

如果(!這個。客戶端登錄)

網絡流。Write(p,0,p . Length);

//開始循環讀寫tcp流。

而(!TcpClose)

{

//如果當前線程處於其他狀態,(等待掛起,等待終止...)結束循環。

如果(線程。CurrentThread.ThreadState!=線程狀態。跑步)

打破;

//判斷Tcp流中是否有可讀的東西。

if ( networkStream。數據可用)

{

//從流中讀取緩沖區字節數組。

Len =網絡流。讀取(buf,0,buf。長度);

//將緩沖區數組轉換為字符串

字符串cmd =編碼。UTF8.GetString(buf,0,Len);

這個。message list . items . add(" client:"+cmd);

//處理緩沖的字符串(分析命令),分析結果為res字符串。

string RES = TcpCmd(cmd);

//將命令的返回結果res轉換成字節數組。

byte[]結果=編碼。UTF8 . GetBytes(RES);

//將結果緩沖區數組發送給客戶端。

網絡流。寫(結果,0,結果。長度);

這個。MessageList.Items.Add("服務器響應:"+RES);

}

其他

{

//線程。睡眠(TimeSpan。from毫秒(200d));

//這個。MessageList.Items.Add("客戶端沒有命令");

//如果當前Tcp連接空閑,客戶端沒有寫入,則當前線程停止200ms。

線程。睡眠(TimeSpan。from毫秒(200d));

}

}

  • 上一篇:救命啊!桌面沒了!
  • 下一篇:方形規劃
  • copyright 2024編程學習大全網