Socket的使用(简单测试)
服务端:
public class SocketTest { private string _ip = string.Empty; private int _port = 0; private Socket _socket = null; private byte[] buffer = new byte[1024 * 1024 * 2]; /// <summary> /// 构造函数 /// </summary> /// <param name="ip">监听的IP</param> /// <param name="port">监听的端口</param> public SocketTest(string ip, int port) { this._ip = ip; this._port = port; } public SocketTest(int port) { this._ip = "0.0.0.0"; this._port = port; } public void StartListen() { try { //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(int.MaxValue); writeLog.AddLogCanDelete("监听" + _socket.LocalEndPoint.ToString() + "消息成功"); //6.0 开始监听 Thread thread = new Thread(ListenClientConnect); thread.Start(); } catch (Exception ex) { } } /// <summary> /// 监听客户端连接 /// </summary> private void ListenClientConnect() { try { while (true) { //Socket创建的新连接 Socket clientSocket = _socket.Accept(); clientSocket.Send(Encoding.UTF8.GetBytes("服务端发送消息:")); Thread thread = new Thread(ReceiveMessage); thread.Start(clientSocket); } } catch (Exception) { } } /// <summary> /// 接收客户端消息 /// </summary> /// <param name="socket">来自客户端的socket</param> private void ReceiveMessage(object socket) { Socket clientSocket = (Socket)socket; while (true) { try { //获取从客户端发来的数据 int length = clientSocket.Receive(buffer); writeLog.AddLogCanDelete("接收客户端" + clientSocket.RemoteEndPoint.ToString() + ",消息" + Encoding.UTF8.GetString(buffer, 0, length)); } catch (Exception ex) { writeLog.AddLogCanDelete(ex.Message); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; } } } }
客户端:
public class SocketTest { private string _ip = string.Empty; private int _port = 0; private Socket _socket = null; private byte[] buffer = new byte[1024 * 1024 * 2]; /// <summary> /// 构造函数 /// </summary> /// <param name="ip">连接服务器的IP</param> /// <param name="port">连接服务器的端口</param> public SocketTest(string ip, int port) { this._ip = ip; this._port = port; } public SocketTest(int port) { this._ip = "127.0.0.1"; this._port = port; } /// <summary> /// 开启服务,连接服务端 /// </summary> public void StartClient() { try { //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.Connect(endPoint); Console.WriteLine("连接服务器成功"); //5.0 接收数据 //int length = _socket.Receive(buffer); //Console.WriteLine("接收服务器{0},消息:{1}", _socket.RemoteEndPoint.ToString(), Encoding.UTF8.GetString(buffer, 0, length)); //6.0 像服务器发送消息 for (int i = 0; i < 10; i++) { Thread.Sleep(200); string sendMessage = string.Format("客户端发送的消息,当前时间{0}", DateTime.Now.ToString()); _socket.Send(Encoding.UTF8.GetBytes(sendMessage)); //Console.WriteLine("像服务发送的消息:{0}", sendMessage); } } catch (Exception ex) { _socket.Shutdown(SocketShutdown.Both); _socket.Close(); Console.WriteLine(ex.Message); } Console.WriteLine("发送消息结束"); } }
一 、Socket概念
Socket并不是一种协议,而是为了方便使用TCP和UDP而抽象出来的一层,位于应用层和传输控制层之间的一组接口。
当两台主机通信时,必须通过Socket连接,Socket则利用TCP/IP协议建立TCP连接,TCP连接则更依赖于底层的IP协议,Socket是控制层传输协议。
二、网络通信的三要素
1. IP地址(网络上主机设备的唯一标识)
2.端口号(定位程序)。有效端口:0~65535,其中0~1024由系统使用,开发中一般使用1024以上的端口。
3.传输协议(用什么样的方式进行交互)。常见协议:TCP(面向连接,提供可靠的服务),UDP(无连接,传输速度快)
三、Socket通信流程
四、C#中Socket的简单实用步骤
1.服务端监听某个端口
2.客户端向服务端地址和端口发起Socket请求
3.服务器接收连接请求后创建Socket连接,并维护这个连接队列
4.客户端和服务端就建立起了双工通信,客户端与服务端就可以实现彼此发送消息。
转载于:https://blog.csdn.net/qq_33022911/article/details/82432778