socket实例
//服务器端
//服务器端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; namespace Server { public partial class Form1 : Form { Socket socketSend; public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } //监听 private void button1_Click(object sender, EventArgs e) { Socket socketWatch = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPAddress ip = IPAddress.Any; IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(this.txtPort.Text)); socketWatch.Bind(point); ShowMsg("监听成功"); //监听 socketWatch.Listen(10); //使用线程不断监听 Thread thread = new Thread(Listen); thread.IsBackground = true; thread.Start(socketWatch); } //发送信息 private void button2_Click(object sender, EventArgs e) { string msg = this.txtMsg.Text.Trim(); byte[] buffer = Encoding.UTF8.GetBytes(msg); socketSend.Send(buffer); } //监听客户端socket void Listen(object o) { Socket socketWatch = o as Socket; while (true) { //等待客户端的连接 并且创建一个负责通信的Socket socketSend = socketWatch.Accept(); ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功"); //接受客户端发送的信息 Thread thread = new Thread(Receive); thread.IsBackground = true; thread.Start(socketSend); } } void Receive(object o) { Socket socketSend = o as Socket; while (true) { byte[] buffer = new byte[1024 * 1024 * 2]; int r = socketSend.Receive(buffer); if (r == 0) { break; } string msg = Encoding.UTF8.GetString(buffer,0,r); ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + msg); } } void ShowMsg(string msg) { txtLog.AppendText(msg+"\r\n"); } } }
//客户端
//客户端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; namespace Client { public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } Socket socketSend; private void button1_Click(object sender, EventArgs e) { socketSend = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); //获取服务器ip IPAddress ip = IPAddress.Parse(txtIp.Text.Trim()); //获取端口号 IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text.Trim())); //连接服务器 socketSend.Connect(point); //开启一个线程不断接受服务器端发送过来的信息 Thread thread = new Thread(Receive); thread.IsBackground = true; thread.Start(); } /// <summary> /// 接受服务端发送的信息 /// </summary> void Receive() { while (true) { byte[] buffer = new byte[1024 * 1024 * 2]; int r = socketSend.Receive(buffer); if (r == 0) { break; } string msg = Encoding.UTF8.GetString(buffer, 0, r); ShowMsg(socketSend.RemoteEndPoint + ":" + msg); } } /// <summary> /// 客服端向服务器端发送信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { string msg = this.txtMsg.Text.Trim(); byte[] buffer = Encoding.UTF8.GetBytes(msg); socketSend.Send(buffer); } void ShowMsg(string msg) { txtLog.AppendText(msg + "\r\n"); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix