网上搜的资料,在这里简单记录一下
用途:Socket可以实现软件与软件之间的通信
流程:创建Socket——创建网络端口——将Socket与端口绑定——设置最大连接数——监听连线
创建Socket
实例化一个Socket对象,使用IP4、字节流、TCP通讯
//1.0 实例化套接字(IP4寻找协议,流式协议,TCP协议) _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
创建网络端口
创建一个网络端口,包含IP地址和端口号等关键信息
//2.0 创建IP对象 IPAddress address = IPAddress.Parse(“192.168.1.100”); //3.0 创建网络端口,包括ip和端口 IPEndPoint endPoint = new IPEndPoint(address,50000);
将Socket与端口绑定
将Socket与端口绑定,指定socket服务器端的IP地址与监听端口
//4.0 绑定套接字 _socket.Bind(endPoint);
设置最大连接数
设置同时连接客户端的最大数目,
//5.0 设置最大连接数 _socket.Listen(10);
监听连线
监听是否有客户端连线用 .Accept()方法,该方法会一直等待所以使用一个新开的线程去执行它,否则程序会卡死
//6.0 开启新线程监听是否有连接 Thread th = new Thread(Listen); th.IsBackground = true; th.Start(_socket);
不断重复的监听是否有客户端连接,如果有则为其生成一个专门与该客户端通讯的socket
void Listen(object o) { Socket SocketWatch = o as Socket; while (true) { //等待客户端连接 SocketSend = SocketWatch.Accept(); ShowMsg(SocketSend.RemoteEndPoint.ToString() + "连接成功"); } }
在此之后,便可以使用该Socket实例的.Send和.Receive实现服务器端的数据的发送与接收
private void button2_Click(object sender, EventArgs e) { byte[] buffer = new byte[1024 * 1024 * 2]; buffer = Encoding.UTF8.GetBytes(textBox4.Text); SocketSend.Send(buffer); }
与发送不同的接收需要实时的接收数据,建议创建一个新线程不停的接收客户端发送的数据
void Recive(object o) { byte[] buffer = new byte[1024 * 1024 * 2]; Socket SocketRecvie = o as Socket; while(true) { //将socket获取到的字节数组存到buffer中,并将字节的长度给到r。 int r = SocketRecvie.Receive(buffer); if (r == 0) { break; } //将二进制编码buffer转化为字符串 string str = Encoding.UTF8.GetString(buffer, 0, r); //输出内容 ShowMsg(SocketRecvie.RemoteEndPoint.ToString() + ":" + str); } }
总结:大致可以这样写
public partial class Form1 : Form { private string _ip = "192.168.1.100"; private int _port = 50000; private Socket _socket = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //1.0 实例化套接字(IP4寻找协议,流式协议,TCP协议) _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //2.0 创建IP对象 IPAddress address = IPAddress.Parse(_ip); //3.0 创建网络端口,包括ip和端口 IPEndPoint endPoint = new IPEndPoint(address, _port); //4.0 绑定套接字 _socket.Bind(endPoint); //5.0 设置最大连接数 _socket.Listen(10); ShowMsg("监听成功"); //6.0 开启新线程监听是否有连接 Thread th = new Thread(Listen); th.IsBackground = true; th.Start(_socket); } /// <summary> /// 服务器等待客户端连接 /// </summary> /// <param name="o"></param> void Listen(object o) { Socket SocketWatch = o as Socket; while (true) { //等待客户端连接 SocketSend = SocketWatch.Accept(); ShowMsg(SocketSend.RemoteEndPoint.ToString() + "连接成功"); //开新线程,一直接受数据 Thread thRecive = new Thread(Recive); thRecive.IsBackground = true; thRecive.Start(SocketSend); } } /// <summary> /// 服务器接收数据 /// </summary> /// <param name="o"></param> void Recive(object o) { byte[] buffer = new byte[1024 * 1024 * 2]; Socket SocketRecvie = o as Socket; while(true) { //将socket获取到的字节数组存到buffer中,并将字节的长度给到r。 int r = SocketRecvie.Receive(buffer); if (r == 0) { break; } //将二进制编码buffer转化为字符串 string str = Encoding.UTF8.GetString(buffer, 0, r); //输出内容 ShowMsg(SocketRecvie.RemoteEndPoint.ToString() + ":" + str); } } void ShowMsg(string str) { textBox3.AppendText(str+"\r\n"); } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } /// <summary> /// 服务器端发送消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { byte[] buffer = new byte[1024 * 1024 * 2]; buffer = Encoding.UTF8.GetBytes(textBox4.Text); SocketSend.Send(buffer); } }
标签:socket,buffer,创建,void,端口,new,Socket,服务器端 From: https://www.cnblogs.com/ZXQ2/p/17130640.html