C#基于Socket实现多人聊天功能
程序员文章站
2022-06-04 07:49:28
本文实例为大家分享了c#基于socket实现多人聊天功能的具体代码,供大家参考,具体内容如下服务器服务器负责接受所有客户端发来的消息,和将接受到的问题群发到其他用户。代码:using system;u...
本文实例为大家分享了c#基于socket实现多人聊天功能的具体代码,供大家参考,具体内容如下
服务器
服务器负责接受所有客户端发来的消息,和将接受到的问题群发到其他用户。
代码:
using system; using system.collections.generic; using system.net; using system.net.sockets; using system.text; using system.threading; namespace chatroomservice { class service { socket socketsevice ; list<socket> userlist;//用户组 public service() { socketsevice = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); userlist = new list<socket>(); } public void start() { socketsevice.bind(new ipendpoint(ipaddress.any,5566)); socketsevice.listen(10); console.writeline("服务器启动成功"); //开启接受连接,用多线程 thread accthread = new thread(accept); accthread.isbackground = true; accthread.start(); } private void accept() { //接受连接 socket clientsocket = socketsevice.accept(); userlist.add(clientsocket); //打印已经连接ip地址 console.writeline(iptoaddress(clientsocket)+"连接进来了"); // thread recvthread = new thread(recemessage); recvthread.isbackground = true; recvthread.start(clientsocket); accept();//递归 } //接收客户端信息 private void recemessage(object obj) { socket client = obj as socket; byte[] strbyte = new byte[1024 * 1024];//设定接受字符的长度 string str = ""; try { int len = client.receive(strbyte);//接受用户发送的内容 str = encoding.default.getstring(strbyte, 0, len); broadcast(str,client);//广播给用户 console.writeline(str); } catch (exception e) { console.writeline(iptoaddress(client)+"退出"); userlist.remove(client); thread.currentthread.abort();//退出时掐死线程,不然递归反弹 } recemessage(client); //使用递归 } /// <summary> /// 广播信息 /// </summary> /// <param name="usestr">传入收到的传输的内容</param> /// <param name="obj">传送信息的客户</param> private void broadcast(string userstr,object obj) { socket clientsend = obj as socket; //当前发送信息的客户 foreach (socket client in userlist) { if (client != clientsend)//将信息广播给其他用户 { client.send(encoding.default.getbytes(iptoaddress(clientsend)+":"+userstr)); } } } //转换出连来客户的ip地址 private string iptoaddress(socket soket) { return (soket.remoteendpoint as ipendpoint).address.tostring(); } } }
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace chatroomservice { class program { static void main(string[] args) { service ss = new service(); ss.start(); console.readline(); } } }
客户端
客户端的功能开始十分简单,可以发送信息给服务器。也可以接收服务器转发过来其他客户端的信息。
using system; using system.collections.generic; using system.linq; using system.net; using system.net.sockets; using system.text; using system.threading; using system.threading.tasks; namespace chatroom { class clientroom { socket clientsocket; public clientroom() { clientsocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);//初始化服务器 } /// <summary> /// 连接服务器 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> public void connected(string ip,int port) { clientsocket.connect(ip,port); console.writeline("连接成功"); // clientsocket.bind(new ipendpoint()); thread recvthread = new thread(recvmessage); recvthread.isbackground = true; recvthread.start(); } public void send(string str) { clientsocket.send(encoding.default.getbytes(str)); } /// <summary> /// 接受信息 /// </summary> private void recvmessage() { try { byte[] strbyte = new byte[500 * 1024]; int len = clientsocket.receive(strbyte); console.writeline(encoding.default.getstring(strbyte, 0, len)); } catch (exception e) //服务器关闭 { console.writeline("服务器关闭"); thread.currentthread.abort();//关闭时切断进程 } recvmessage(); } } }
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace chatroom { class program { static void main(string[] args) { clientroom client = new clientroom(); client.connected("127.0.0.1", 5566); string str = console.readline(); while (!str.equals("q")) { client.send(str); str = console.readline(); } console.readline(); } } }
可以正常对话,测试一下。假装和自己对话
目前还没有解决沾包问题
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。