C#实现套接字发送接收数据
本文实例为大家分享了C#实现套接字发送接收数据的具体代码,供大家参考,具体内容如下
服务端
【C#实现套接字发送接收数据】
文章图片
namespace TestServer{public partial class ServerForm : Form{Socket socketSend; // 将远程连接的客户端的IP地址和socket存入集合Dictionary dicSocket = new Dictionary(); public ServerForm(){InitializeComponent(); }private void btnStart_Click(object sender, EventArgs e){try{// 当点击开始监听后,在服务器创建一个负责监听IP地址和端口号的socketSocket 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){// 等待客户端连接 并创建一个负责通信的SocketSocket 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 ListUserList(){List userList = new List (); 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{// 创建负责通信的socketsocketSend = 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数据
文章图片
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM
- pytorch|使用pytorch从头实现多层LSTM