tcp開闢多個監聽埠,並且每個埠支援多個連線

2021-10-09 09:31:35 字數 2034 閱讀 1958

class tcpclass

catch (socketexception se)

));socketwatch.close();

return;

}// 設定監聽佇列的長度;

socketwatch.listen(10);

// 建立負責監聽的執行緒;

thread threadwatch = new thread(watchconnecting);

threadwatch.isbackground = true;

threadwatch.start(socketwatch);

api.tb.dispatcher.invoke(new action(

delegate

));console.writeline(api.tcp_port+"伺服器啟動監聽成功!");

}///

/// 監聽客戶端請求的方法;

///

void watchconnecting(object socketw)

));// 開始監聽客戶端連線請求,accept方法會阻斷當前的執行緒;

socket soc = socketw as socket;

socket sokconnection = soc.accept(); // 一旦監聽到乙個客戶端的請求,就返回乙個與該客戶端通訊的 套接字;

console.writeline(sokconnection.remoteendpoint.tostring());

api.tb.dispatcher.invoke(new action(

delegate

));// 將與客戶端連線的 套接字 物件新增到集合中;

dict.add(sokconnection.remoteendpoint.tostring(), sokconnection);

thread thr = new thread(recmsg);

thr.isbackground = true;

thr.start(sokconnection);

dictthread.add(sokconnection.remoteendpoint.tostring(), thr);  //  將新建的執行緒 新增 到執行緒的集合中去。}}

//接收資料

void recmsg(object sokconnectionparn)

else

));break;}}

catch (socketexception se)

));break;

}catch (exception e)

));break;}}

}public void deal_rec_data(byte tcp_rec_data,string str)

//存在,清空list

else}} 

}//音訊資料

else if (tcp_rec_data[8] == 2)}}

//停止錄音

else if (tcp_rec_data[8] == 3)} }

//結束錄音

else if (tcp_rec_data[8] == 4)

}rec_buf.remove(str);

dict.remove(str);

dictthread.remove(str);}}

}//生成音訊檔案

void write_pcm_file(listlist)

));filestream fs = file.openwrite(filepath); 

for(int i=1;i從第5位開始擷取3個位元組

//byte陣列中取int數值,適用於(低位在前,高位在後)的順序

public static int bytestoint(byte src, int offset)

/** 

* byte陣列中取int數值,適用於(低位在後,高位在前)的順序。和inttobytes2()配套使用

*/public static int bytestoint2(byte src, int offset)

}

python同時監聽多個埠 監聽多個埠

我在玩twisted,建立了乙個簡單的 伺服器 我想讓伺服器監聽多個埠 1025 65535 而不是單個埠。我該怎麼做?在 我的 from twisted.internet.protocol import protocol,serve ctory from twisted.internet impo...

wriesharek同時監聽多個埠

之前的文章 wireshark解析自定義的protobuf協議 當時只監聽了乙個埠,而如果遊戲同時有二個 socket 連線,比如乙個是閘道器另外乙個是其它的,怎麼辦呢?for i,port in ipairs tcp port do tcp port table add port,m meteor...

wriesharek同時監聽多個埠

之前的文章 wireshark解析自定義的protobuf協議 當時只監聽了乙個埠,而如果遊戲同時有二個 socket 連線,比如乙個是閘道器另外乙個是其它的,怎麼辦呢?for i,port in ipairs tcp port do tcp port table add port,m meteor...