您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了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数据
感谢你能够认真阅读完这篇文章,希望小编分享的“C#如何实现套接字发送接收数据”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。