本文实例为大家分享了c#实现套接字发送接收数据的具体代码,供大家参考,具体内容如下
namespace testrver{ public partial class rverform : form { socket socketnd; // 将远程连接的客户端的ip地址和socket存入集合 漳河工程管理局 dictionary<string, socket> dicsocket = new dictionary<string, socket>(); public rverform() { initializecomponent(); } private void btnstart_click(object nder, eventargs e) { try { // 当点击开始监听后,在服务器创建一个负责监听ip地址和端口号的socket socket socketwatch = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); ipaddress ip = ipaddress.any;//;ipaddress.par(txtrver.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) { socketnd = socketwatch.accept(); dicsocket.add(socketnd.remoteendpoint.tostring(),socketnd); cbours.items.add(socketnd.remoteendpoint.tostring()); showmsg(socketnd.remoteendpoint.tostring() + " : " + "连接成功"); thread th = new thread(recive); th.isbackground = true; th.start(socketnd); } } void recive(object o) { socketnd = o as socket; while (true) { // 客户端连接成功后,服务器应接受客户端发来的消息 byte[] buffer = new byte[1024 * 1024 * 2]; // 实际接收到的有效字节数 int r = socketnd.receive(buffer); if (r == 0) { break; } 担当作文 string str = encoding.utf8.getstring(buffer, 0, r); showmsg(socketnd.remoteendpoint + " : " + str); } } void showmsg(string str) { txtlog.appendtext(str + "\r\n"); } private void form1_load(object nder, eventargs e) { control.checkforillegalcrossthreadcalls = fal; } private void btnnd_click(object nder, eventargs e) { string str = txtmsg.text; byte[] buffer = encoding.utf8.getbytes(str); string ip = cbours.lecteditem.tostring(); dicsocket[ip].nd(buffer); //socketnd.nd(buffer); txtmsg.text = ""; } private void btnndur_click(object nder, eventargs e) { string json = jsonconvert.rializeobject(urlist()); byte[] buffer = encoding.utf8.getbytes(json); socketnd.nd(buffer); } private list<ur> urlist() { list<ur> urlist = new list<ur>(); ur ur = null; string sql = "lect * from ur_table"; sqlcommand cmd = sqlutils.connectsql(sql); sqldatareader sdr = cmd.executereader(); while (sdr.read()) { ur = new ur(sdr.getint32(0), sdr.getstring(1), sdr.getstring(2), sdr.getint32(3)); urlist.add(ur); } r江州司马eturn urlist; } }}
namespace testclient{ public partial class clientform : form { socket socketnd; public clientform() { initializecomponent(); } private void btnstart_click(object nder, eventargs e) { try { // 创建负责通信的socket socketnd = new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp); ipaddress ip = ipaddress.par(txtrver.text); ipendpoint point = new ipendpoint(ip,convert.toint32(txtport.text)); socketnd.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 = socketnd.receive(buffer); if (r == 0) { break; } string str = encoding.utf8.getstring(buffer, 0, r); showmsg(socketnd.remoteendpoint + " : " + str); } } void showmsg(string str) { txtlog.appendtext(str + "\r\n"); } private void form1_load(object nder, eventargs e) { control.checkforillegalcrossthreadcalls = fal; } private void btnnd_click(object nder, eventargs e) { string str = txtmsg.text.trim(); byte[]惠州香溪堡 buffer = encoding.utf8.getbytes(str); socketnd.nd(buffer); txtmsg.text = ""; } }}
效果
发送json数据
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-03 23:01:22,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/0fbf2f9e52b0432634776c54c75331c4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#实现套接字发送接收数据.doc
本文 PDF 下载地址:C#实现套接字发送接收数据.pdf
留言与评论(共有 0 条评论) |