C#实现套接字发送接收数据
程序员文章站
2022-07-03 22:23:34
本文实例为大家分享了c#实现套接字发送接收数据的具体代码,供大家参考,具体内容如下服务端namespace testserver{ public partial class serverform...
本文实例为大家分享了c#实现套接字发送接收数据的具体代码,供大家参考,具体内容如下
服务端
namespace testserver { public partial class serverform : form { socket socketsend; // 将远程连接的客户端的ip地址和socket存入集合 dictionary<string, socket> dicsocket = new dictionary<string, socket>(); public serverform() { initializecomponent(); } private void btnstart_click(object sender, eventargs e) { try { // 当点击开始监听后,在服务器创建一个负责监听ip地址和端口号的socket socket socketwatch = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); ipaddress ip = ipaddress.any;//;ipaddress.parse(txtserver.text) ipendpoint point = new ipendpoint(ip, convert.toint32(txtport.text)); // 监听 socketwatch.bind(point); showmsg("监听成功"); socketwatch.listen(10); thread th = new thread(listen); th.isbackground = true; th.start(socketwatch); } catch { } } void listen(object o) { // 等待客户端连接 并创建一个负责通信的socket socket socketwatch = o as socket; while (true) { socketsend = socketwatch.accept(); dicsocket.add(socketsend.remoteendpoint.tostring(),socketsend); cbousers.items.add(socketsend.remoteendpoint.tostring()); showmsg(socketsend.remoteendpoint.tostring() + " : " + "连接成功"); thread th = new thread(recive); th.isbackground = true; th.start(socketsend); } } void recive(object o) { socketsend = o as socket; while (true) { // 客户端连接成功后,服务器应接受客户端发来的消息 byte[] buffer = new byte[1024 * 1024 * 2]; // 实际接收到的有效字节数 int r = socketsend.receive(buffer); if (r == 0) { break; } string str = encoding.utf8.getstring(buffer, 0, r); showmsg(socketsend.remoteendpoint + " : " + str); } } void showmsg(string str) { txtlog.appendtext(str + "\r\n"); } private void form1_load(object sender, eventargs e) { control.checkforillegalcrossthreadcalls = false; } private void btnsend_click(object sender, eventargs e) { string str = txtmsg.text; byte[] buffer = encoding.utf8.getbytes(str); string ip = cbousers.selecteditem.tostring(); dicsocket[ip].send(buffer); //socketsend.send(buffer); txtmsg.text = ""; } private void btnsenduser_click(object sender, eventargs e) { string json = jsonconvert.serializeobject(userlist()); byte[] buffer = encoding.utf8.getbytes(json); socketsend.send(buffer); } private list<user> userlist() { list<user> userlist = new list<user>(); user user = null; string sql = "select * from user_table"; sqlcommand cmd = sqlutils.connectsql(sql); sqldatareader sdr = cmd.executereader(); while (sdr.read()) { user = new user(sdr.getint32(0), sdr.getstring(1), sdr.getstring(2), sdr.getint32(3)); userlist.add(user); } return userlist; } } }
客户端
namespace testclient { public partial class clientform : form { socket socketsend; public clientform() { initializecomponent(); } private void btnstart_click(object sender, eventargs e) { try { // 创建负责通信的socket socketsend = new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp); ipaddress ip = ipaddress.parse(txtserver.text); ipendpoint point = new ipendpoint(ip,convert.toint32(txtport.text)); socketsend.connect(point); showmsg("连接成功"); thread th = new thread(recive); th.isbackground = true; th.start(); } catch { } } void recive() { while (true) { // 客户端连接成功后,服务器应接受客户端发来的消息 byte[] buffer = new byte[1024 * 1024 * 2]; // 实际接收到的有效字节数 int r = socketsend.receive(buffer); if (r == 0) { break; } string str = encoding.utf8.getstring(buffer, 0, r); showmsg(socketsend.remoteendpoint + " : " + str); } } void showmsg(string str) { txtlog.appendtext(str + "\r\n"); } private void form1_load(object sender, eventargs e) { control.checkforillegalcrossthreadcalls = false; } private void btnsend_click(object sender, eventargs e) { string str = txtmsg.text.trim(); byte[] buffer = encoding.utf8.getbytes(str); socketsend.send(buffer); txtmsg.text = ""; } } }
效果
发送json数据
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 腾讯视频如何编辑在追记录?腾讯视频编辑在追记录教程
下一篇: 390个qq个性昵称或签名