本文实例为大家分享了c#基于socket实现多人聊天功能的具体代码,供大家参考,具体内容如下
服务器负责接受所有客户端发来的消息,和将接受到的问题群发到其他用户。
代码:
using system;using system.collections.generic;using system.net;using system.net.sockets;using system.text;using system.threading;namespace chatroomrvice{ class rvice { socket socketvice ; list<socket> urlist;//用户组 public rvice() { socketvice = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); urlist = new list<socket>(); } public void start() { socketvice.bind(new ipendpoint(ipaddress.any,5566)); socketvice.listen(10); console.writeline("服务器启动成功"); //开启接受连接,用多线程 thread accthread = new thread(accept); accthread.isbackground = true; accthread.start(); } private void accept() { //接受连接 socket clientsocket = socketvice.accept(); urlist.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)+"退出"); urlist.remove(client); thread.currentthread.abort();//退出时掐死线程,不然递归反弹 } recemessage(client); //凸透镜成像公式使用递归 } /// <summary> /// 广播信息 /// </summary> /// <param name="ustr">传入收到的传输的内容</param> /// <param name="obj">传送信息的客户</param> private void broadcast(string urstr,object obj) { socket clientnd = obj as socket; //当前发送信息的客户 foreach (socket client in urlist) { if (client != clientnd)//将信息广播给其他用户 { client.nd(encoding.default.getbytes(iptoaddr庆典花篮贺词ess(clientnd)+":"+urstr)); } } } //转换出连来客户的ip地址 priva台湾简介te 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 chatroomrvice{ class program { static void main(string[] args) { rvice ss = new rvice(); 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 nd(string str) { clientsocket.nd(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.nd(str); str = console.readline(); } console.readline(); } }}
可以正常对话,测试一下。假装和自己对话
目前还没有解决沾包问题
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 23:51:44,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/742534c3cd4774f2bb6c17a77147c59e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#基于Socket实现多人聊天功能.doc
本文 PDF 下载地址:C#基于Socket实现多人聊天功能.pdf
留言与评论(共有 0 条评论) |