using system;
using system.collections.generic;
using system.text;
using system.net;
using system.net.sockets;
using system.threading;
namespace server
// 善後工作,釋放資源
}catch (exception e)
console.readline();}}
public class acceptsocketclass
public void startthread()
批資料流...", times + 1);
for (int i = 0; i < k; i++)
thread td = new thread(new parameterizedthreadstart(doaction));
td.start(times);
times++;}}
private void doaction(object t)
個執行緒", (int)t + 1);
thread.sleep(10000);
console.writeline("第個執行緒醒了", (int)t + 1);
// 處理客戶端請求,給客戶端回應
asciiencoding asen = new asciiencoding();
socket.send(asen.getbytes("the" + ((int)t + 1).tostring() + "th string was echoed by the server."));
console.writeline("\n第個執行緒已傳送回應資訊", (int)t + 1);
thread.currentthread.abort();//幹完就退}}
}
using system;
using system.collections.generic;
using system.text;
using system.io;
using system.net;
using system.net.sockets;
using system.threading;
namespace client
}catch (exception e)
console.readline();
}private static void constantsend()//收發在乙個執行緒才能保證埠不錯亂
個連線成功", t);
t++;
console.writeline("傳送乙個'hi'過去後睡2000ms\n");
// 得到客戶端的流
stream stm = tcpclnt.getstream();
asciiencoding asen = new asciiencoding();
str = "hi" + (t - 1).tostring();
byte ba = asen.getbytes(str);
"傳輸中.....");
stm.write(ba, 0, ba.length);
thread.sleep(2000);
// 接收從伺服器返回的資訊
byte bb = new byte[100];
int k = stm.read(bb, 0, 100);
// 輸出伺服器返回資訊
for (int i = 0; i < k; i++)
console.writeline();//空行區分下
// 關閉客戶端連線
tcpclnt.close();
}catch (exception e)}}
}
除錯posix多執行緒 socket網路通訊有感
1 捕捉大腦裡最微妙的東西。包括村上春,包括linux。並將其清晰化,這是一種非常重要的能力。2 能用語言,圖形將問題盡量清晰地描述出來 3 能編寫自我測試程式 並能強化測試目標,優化測試順序,描繪測試藍圖,好用。4 繪製藍圖 可以理解為連線,模組內部模組之間的關係,起一種鳥瞰俯視的作用 施工圖 容...
基於UDP的多執行緒網路通訊程式
分享一段基於udp的多執行緒網路點對點通訊程式。檔案一 ccomm.h ifndef ccomm h define ccomm h include include include include define socklen t int pragma comment lib,wsock32.lib ...
暑期java學習筆記(二) 多執行緒與網路通訊
一 多執行緒 繼承thread,收到單繼承的影響 常用runnable介面,實現run 方法,不能丟擲異常,沒有返回值 實現callable介面,重寫call 方法,可以丟擲異常,有返回值 join 合併兩個執行緒 eg 執行緒b 暫停本執行緒 sleep 靜態方法,休眠一段時間,不釋放鎖。1 用在...