當前位置:首頁 » 雲伺服器 » 伺服器怎麼能使軟體用多線程跑

伺服器怎麼能使軟體用多線程跑

發布時間: 2023-09-13 07:44:56

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

用C#實現多線程TCP協議的伺服器端程序:
// <summary>
//毀帆/ Tcp客戶線程類(服務端),ThreadServerProcessor 線程產生的客戶連接,用該線程讀寫
/// </summary>
public class ThreadClientProcessor
{
//Tcp連接實例
private TcpClient tcpClient;
//消息框,本來想寫日誌用
private System.Windows.Forms.ListBox MessageList;
private string Password; //該連接登陸密碼
private string Cmd1Echo;
private string Cmd2Echo;
private bool ClientLogOn;//客戶是否登陸
private bool TcpClose;
public ThreadClientProcessor(){}
//構造函數,參數解釋:Tcp客戶,消息框,該服務密碼(password命令後的參數) ,命令回應串 1,2 ******************
public ThreadClientProcessor(TcpClient client , ListBox listBox,string LogonText ,string cmd1echo,string cmd2echo)
{
ClientList.Add(this); //把當前實例加入一個列表中,方便以後控制
this.tcpClient=client;
this.MessageList=listBox;
this.Password=LogonText;
this.Cmd1Echo=cmd1echo;
this.Cmd2Echo=cmd2echo;
this.ClientLogOn=false;
this.TcpClose=false;
}
public static char[] CmdSplit={' '}; //讀來的串由' ' 進行分離,命名+' '+參數
//public const string[] Cmd=new string[] { "password","cmd1","cmd2","echo","bye"};
//該函數由你自己寫,這個只是給一個例子,
//功能:命令處理器,給個命令串,返回該命令處理結果,把命令和處理結果放在一個文本文件里,便於系統升級
public string TcpCmd(string s)
{
string result;
try
{
string cmdarg=s.Trim();
string[] args=cmdarg.Split(CmdSplit);
string cmd=args[0].ToLower();
switch (cmd )
{
case "password" :
if (args.Length>1)
{
ClientLogOn= Password.Equals(args[1].Trim());
result=ClientLogOn? "登陸成功":"密碼不正確,未登陸";
}
else result= "登陸時候,沒有輸入密碼";
break;
case "cmd1":
result=ClientLogOn?this.Cmd1Echo:"該命令無權執行,請先登陸";
break;
case "cmd2":
result=ClientLogOn?this.Cmd2Echo:"該命令無權執行,請先登陸";
break;
case "echo":
result=string.Format("伺服器回纖行雹應:\n {0}",s);
break;
case "bye":
this.TcpClose=true;
result="DisConnected";
break;
default:
result="不可識別的命令";
break;
}
}
catch
{
result="解析命令發生錯誤,你輸入的是狗屁命令,TMD *^* ";
}
return result;
} //end cmd
//定帶讓義一個線程,該線程對應的函數是 void start()(不是Start())********************************
//一下程序主要是操作該線程
public System.Threading.Thread tcpClientThread;
//啟動客戶連接線程 *************************************************************
public void Start()
{
tcpClientThread=new Thread(new ThreadStart(start));
tcpClientThread.Priority=ThreadPriority.BelowNormal;
tcpClientThread.Start();
}
//斷開該當前實例連接,終止線程 **************************************************************
public void Abort()
{
if (this.tcpClientThread!=null)
{
//tcpClientThread.Interrupt();
tcpClientThread.Abort();
//一定要等一會兒,以為後邊tcpClient.Close()時候,會影響NetWorkStream的操作
Thread.Sleep(TimeSpan.FromMilliseconds(100));
tcpClient.Close();
}
}
//靜態列表,包含了每個連接實例(在構造實例時候使用了 ArrayList.Add( object))
private static System.Collections.ArrayList ClientList=new ArrayList();
//斷開所有的Tcp客戶連接,靜態方法*************************************************************
public static void AbortAllClient()
{
for(int j=0 ;j< ClientList.Count;j++)
{
//從實例列表中取一個對象,轉化為ThreadClientProcessor對象
ThreadClientProcessor o=(ThreadClientProcessor ) ClientList[j];
//調用ThreadClientProcessor 對象的停止方法
o.Abort();
}
//清除連接列表
ClientList.Clear();
}
//讀寫連接的函數,用於線程//*******************************************************************
private void start()
{
byte[] buf=new byte[1024*1024]; //預先定義1MB的緩沖
int Len=0; //流的實際長度
NetworkStream networkStream=tcpClient.GetStream(); //建立讀寫Tcp的流
try
{
byte[] p=Encoding.UTF8.GetBytes(" 歡迎光臨,請輸入密碼" );
//向Tcp連接寫 歡迎消息
if (!this.ClientLogOn )
networkStream.Write(p,0,p.Length);
//開始循環讀寫tcp流
while (!TcpClose)
{
//如果當前線程是在其它狀態,(等待掛起,等待終止.....)就結束該循環
if (Thread.CurrentThread.ThreadState!=ThreadState.Running)
break;
//判斷Tcp流是否有可讀的東西
if ( networkStream.DataAvailable)
{
//從流中讀取緩沖位元組數組
Len=networkStream.Read(buf,0,buf.Length);
//轉化緩沖數組為串
string cmd=Encoding.UTF8.GetString(buf,0,Len);
this.MessageList.Items.Add("客戶機:"+cmd);
//處理該緩沖的串(分析命令),分析結果為res串
string res=TcpCmd(cmd);
//把命令的返回結果res 轉化為位元組數組
byte[] result=Encoding.UTF8.GetBytes(res);
//發送結果緩沖數組給客戶端
networkStream.Write(result,0,result.Length);
this.MessageList.Items.Add("伺服器回應:"+res);
}
else
{
//Thread.Sleep(TimeSpan.FromMilliseconds(200d));
//this.MessageList.Items.Add("客戶機無命令");
//如果當前Tcp連接空閑,客戶端沒有寫入,則當前線程停止200毫秒
Thread.Sleep(TimeSpan.FromMilliseconds(200d));
}
}

java程序完成伺服器和客戶端的SOCKET通訊,要求伺服器使用多線程接收和處理多個客戶端訪問請求

1. 客戶端程序
import java.io.*;
import java.net.*;
public class TalkClient {
public static void main(String args[]) {
try{
Socket socket=new Socket("127.0.0.1",4700);
//向本機的4700埠發出客戶請求
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
//由系統標准輸入設備構造BufferedReader對象
PrintWriter os=new PrintWriter(socket.getOutputStream());
//由Socket對象得到輸出流,並構造PrintWriter對象
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket對象得到輸入流,並構造相應的BufferedReader對象
String readline;
readline=sin.readLine(); //從系統標准輸入讀入一字元串
while(!readline.equals("bye")){
//若從標准輸入讀入的字元串為 "bye"則停止循環
os.println(readline);
//將從系統標准輸入讀入的字元串輸出到Server
os.flush();
//刷新輸出流,使Server馬上收到該字元串
System.out.println("Client:"+readline);
//在系統標准輸出上列印讀入的字元串
System.out.println("Server:"+is.readLine());
//從Server讀入一字元串,並列印到標准輸出上
readline=sin.readLine(); //從系統標准輸入讀入一字元串
} //繼續循環
os.close(); //關閉Socket輸出流
is.close(); //關閉Socket輸入流
socket.close(); //關閉Socket
}catch(Exception e) {
System.out.println("Error"+e); //出錯,則列印出錯信息
}
}
}
2. 伺服器端程序
import java.io.*;
import java.net.*;
import java.applet.Applet;
public class TalkServer{
public static void main(String args[]) {
try{
ServerSocket server=null;
try{
server=new ServerSocket(4700);
//創建一個ServerSocket在埠4700監聽客戶請求
}catch(Exception e) {
System.out.println("can not listen to:"+e);
//出錯,列印出錯信息
}
Socket socket=null;
try{
socket=server.accept();
//使用accept()阻塞等待客戶請求,有客戶
//請求到來則產生一個Socket對象,並繼續執行
}catch(Exception e) {
System.out.println("Error."+e);
//出錯,列印出錯信息
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket對象得到輸入流,並構造相應的BufferedReader對象
PrintWriter os=newPrintWriter(socket.getOutputStream());
//由Socket對象得到輸出流,並構造PrintWriter對象
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
//由系統標准輸入設備構造BufferedReader對象
System.out.println("Client:"+is.readLine());
//在標准輸出上列印從客戶端讀入的字元串
line=sin.readLine();
//從標准輸入讀入一字元串
while(!line.equals("bye")){
//如果該字元串為 "bye",則停止循環
os.println(line);
//向客戶端輸出該字元串
os.flush();
//刷新輸出流,使Client馬上收到該字元串
System.out.println("Server:"+line);
//在系統標准輸出上列印讀入的字元串
System.out.println("Client:"+is.readLine());
//從Client讀入一字元串,並列印到標准輸出上
line=sin.readLine();
//從系統標准輸入讀入一字元串
} //繼續循環
os.close(); //關閉Socket輸出流
is.close(); //關閉Socket輸入流
socket.close(); //關閉Socket
server.close(); //關閉ServerSocket
}catch(Exception e){
System.out.println("Error:"+e);
//出錯,列印出錯信息
}
}}

❸ 關於多線程伺服器

你說你綁定和監聽都好了吧
那麼服務就可以這樣寫
while(bWorking)
{
SOCKET sAccept = accept(sListen, NULL, NULL);
if(sAccept != SOCKET_ERROR)
CreateThread(NULL, 0, ThreadProc, (LPVOID)&sAccept, 0, NULL);
}
相應的線程函數可以這樣:
DWORD WINAPI ThreadProc(LPVOID lpPara)
{
SOCKET sComm = *((SOCKET*)lpPara);
//從這里開始讀寫這個socket就可以了
}
這樣就實現了accept一個連接就創建一個線程。

❹ 網路出現連接超時怎麼解決

網路連接超時就是在程序默認的等待時間內沒有得到伺服器的響應。

可能原因及解決辦法如下:

1、網路斷開,不過經常顯示無法連接或是網路阻塞,導致你不能在程序默認等待時間內得到回復數據包 ;

2、網路不穩定,網路無法完整傳送伺服器信息 ;

3、系統:系統資源過低,無法為程序提供足夠的資源處理伺服器信息;

4、查看網路連接是否正常,可以給網路運營商打電話,要求查詢線路,也可以將同網路內的其他人的網速限制一下;

5、路由器、貓、網卡等設備故障,也會引起網路卡,建議先重啟,如果不能解決,建議更換;

6、電腦網卡驅動程序需要重新安裝一下,再重新聯網;

7、全盤查殺病毒後重新聯網。

(4)伺服器怎麼能使軟體用多線程跑擴展閱讀

網路連接超時常見解決辦法:

1、檢查網線,更換介面;

2、在早上上網人數少的時候注冊;

3、使用加速軟體,如果要下載東西盡量在沒有網路使用需求情況下載;

4、殺毒;

5、如果使用網卡,可以更換一下網卡。

熱點內容
怎樣登陸ftp 發布:2025-02-02 07:44:44 瀏覽:631
瘋狂點擊腳本 發布:2025-02-02 07:38:10 瀏覽:71
pss演算法 發布:2025-02-02 07:30:55 瀏覽:747
發信息腳本 發布:2025-02-02 07:03:07 瀏覽:741
l2l3緩存 發布:2025-02-02 06:56:47 瀏覽:524
為什麼安卓下不了蟲蟲助手 發布:2025-02-02 06:46:47 瀏覽:45
ftp伺服器ui 發布:2025-02-02 06:24:15 瀏覽:103
wifi有多少種密碼 發布:2025-02-02 06:22:06 瀏覽:587
app賬號和密碼忘了怎麼辦啊 發布:2025-02-02 06:21:58 瀏覽:107
map訪問 發布:2025-02-02 06:09:07 瀏覽:825