乙個簡單的TCP客戶端 服務端會話程式 C

2021-04-27 14:34:09 字數 717 閱讀 8383

服務端:

1. new 乙個tcplistener listner

2.listner.start

3.while(true)

4. 獲取socket :socket s = listner.acceptsocket();

5. 獲取netstream:networkstream ns = new networkstream(s);

6.讀取netstream: streamreader sr = new streamreader(ns);

string command = sr.readline();

7.應答: byte res = encoding.utf8.getbytes("回答內容");

s.send(res);

客戶端1. new tcpclient client

2. 獲取流 :  networkstream stream = client.client.getstream();

streamreader reader = new streamreader(stream);

3. 請求內容:byte cmd = encoding.utf8.getbytes("請求內容");

4.傳送請求:stream.write(cmd, 0, cmd.length);

5.獲取回應(商定為空的時候退出):while (true)

6.關閉連線:client.close();

乙個簡單的服務端與客戶端TCP通訊

乙個簡單的服務端與客戶端tcp通訊 from socket import ip port 10.33.0.75 8000 定義建立連線的ip位址與ip埠 back log 5 設定連線池為2 buffer size 1024 設定每次傳送與接收的資料大小 tcp sever socket af in...

TCP客戶端服務端demo

服務端程式 include include include include include include include int main 列印握手成功的客戶端 struct sockaddr in servaddr socklen t nservlen sizeof servaddr getso...

簡單實現TCP服務端與客戶端

1 include2 include3 include4 include5 include6 include7 include8 include9 10 tcp服務端 簡單的網路聊天程式 11 1 建立socket 12 2 為socket繫結位址埠 13 3 開始監聽socket 告訴作業系統,開...