TCP中的服務端與客戶端的實現

2021-06-18 11:28:56 字數 1388 閱讀 3141

tcp中首先要在服務端開啟監聽,這樣才可以從客戶端鏈結

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.net.sockets;

using system.collections;

using system.net;

namespace server

static void delmember(ipendpoint rep)//移除組

}

}static void senduserlist()

byte data = udpcomm.udpcomm.encodingascii(userlist);

foreach (keyvaluepairss in dic)//把使用者表發給每個使用者

}static void sendtomember(ipendpoint user,string message)//組類**資料(第乙個引數是誰發的,第二個是發的內容)

}foreach (keyvaluepaird in dic)//迴圈給每個人都傳送資訊

}static void main(string args)

else if (returndata.indexof("del") > -1)

else }}

}catch (exception e) }}

}

客戶端

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.net.sockets;

using system.net;

using system.threading;

namespace client

public void senddata()

while (true)

else if (senddata.indexof("quit") > -1)

data = udpcomm.udpcomm.encodingascii(senddata);//傳送資料

client.send(data, data.length);

}console.writeline("退出");

client.close();

}catch (exception e)

}public void receivedata()}}

}

上面就能夠實現乙個簡單的聊天程式

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

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

TCP客戶端與服務端通訊

客戶端 include include include include include in.h include include include include define maxline 1024 int main int argc,char ar 建立socketfd socketfd soc...

TCP客戶端服務端demo

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