summary
The reason why Socket socket communication library is encapsulated is that it is mainly because directly using sockets for network communication programming is relatively complicated, especially for beginners. In fact, Microsoft has provided TCP and UDP communication advanced encapsulation classes since .net 2.0 as follows:
TcpListener TcpClient UdpClient
Microsoft has provided an asynchronous communication interface based on Task tasks since .net 4.0. When using the socket encapsulation library directly, many socket details cannot be controlled by themselves. This article aims to provide a socket encapsulation for reference. The article shows that some of the TCP communication library is encapsulated, and UDP packaging can also be used by the following:
CusTcpListener CusTcpClient
Code
using System; using ; using ; using ; namespace NetDemo { // Network operation related classes public class InternetProHelper { // Check whether the set port number is correct and return the correct port number. The invalid port number returns -1 public static int GetNetPort(string NetPort) { //Declare the correct port number returned int resPort = -1; //Detection of port number try { // If the incoming port number is empty, an exception will be thrown if (NetPort == "") { throw new Exception("The port number cannot be empty!"); } //Detection of port range if ((Convert.ToInt32(NetPort) < ) || (Convert.ToInt32(NetPort) > )) { throw new Exception("The port number range is invalid!"); } // Assign value to port number resPort = Convert.ToInt32(NetPort); } catch (Exception ex) { string errMessage = ; } return resPort; } public static IPAddress StringToIPAddress(string NetIP) { // Convert IP address in string form to IPAddress object return (NetIP); } public static string LocalHostName { // Get the computer name of the machine get { return (); } } public static string LANIP { // Get the local area network IP of the machine get { //Get the IP list of the machine. The first item in the IP list is the LAN IP, and the second item is the WAN IP IPAddress[] IPaddrList = (()).AddressList; //If the native IP list is empty, return an empty string if ( < 1) { return ""; } //Return to the local area network IP of the machine return IPaddrList[0].ToString(); } } public static string GetClientIP(Socket clientSocket) { // Get the IP address of the remote client IPEndPoint client = (IPEndPoint);//NetSocket object of client return (); } public static IPEndPoint CreateIPEndPoint(string NetIP, int NetPort) { // Create an IPEndPoint object IPAddress ipAddress = StringToIPAddress(NetIP); return new IPEndPoint(ipAddress, NetPort); } public static TcpListener CreateTcpListener() { //Create an automatically allocated network node IPAddress ipAddress = ; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 0); return new TcpListener(localEndPoint); } public static TcpListener CreateTcpListener(string NetIP, int NetPort) { //Create a network node IPAddress ipAddress = StringToIPAddress(NetIP); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, NetPort); return new TcpListener(localEndPoint); } public static Socket CreateTcpSocket() { // Create a Socket object based on TCP protocol return new Socket(, , ); } public static Socket CreateUdpSocket() { // Create a Socket object based on UDP protocol return new Socket(, , ); } public static IPEndPoint GetLocalPoint(TcpListener tcpListener) { // Get the local endpoint of the TcpListener object return (IPEndPoint); } public static string GetLocalPoint_IP(TcpListener tcpListener) { // Get the IP address of the local endpoint of the TcpListener object IPEndPoint localEndPoint = (IPEndPoint); return (); } public static int GetLocalPoint_Port(TcpListener tcpListener) { // Get the port number of the local endpoint of the TcpListener object IPEndPoint localEndPoint = (IPEndPoint); return ; } public static IPEndPoint GetLocalPoint(Socket NetSocket) { // Get the local endpoint of the Socket object return (IPEndPoint); } public static string GetLocalPoint_IP(Socket NetSocket) { // Get the IP address of the local endpoint of the Socket object IPEndPoint localEndPoint = (IPEndPoint); return (); } public static int GetLocalPoint_Port(Socket NetSocket) { // Get the port number of the local endpoint of the Socket object IPEndPoint localEndPoint = (IPEndPoint); return ; } public static void BindEndPoint(Socket NetSocket, IPEndPoint endPoint) { // Bind the endpoint if (!) { (endPoint); } } public static void BindEndPoint(Socket NetSocket, string NetIP, int NetPort) { //Create endpoint IPEndPoint endPoint = CreateIPEndPoint(NetIP, NetPort); //Bind endpoint if (!) { (endPoint); } } public static void StartListen(Socket NetSocket, int NetPort) { //Create a local endpoint IPEndPoint localPoint = CreateIPEndPoint(, NetPort); //Bind to local endpoint BindEndPoint(NetSocket, localPoint); //Start monitoring (200); } public static void StartListen(Socket NetSocket, int NetPort, int maxConnection) { //Create a local endpoint IPEndPoint localPoint = CreateIPEndPoint(, NetPort); //Bind to local endpoint BindEndPoint(NetSocket, localPoint); //Start monitoring (maxConnection); } public static void StartListen(Socket NetSocket, string NetIP, int NetPort, int maxConnection) { //Bind to local endpoint BindEndPoint(NetSocket, NetIP, NetPort); //Start monitoring (maxConnection); } public static bool Connect(Socket NetSocket, string NetIP, int NetPort) { // Connect to a server based on TCP protocol. Return true after successful connection, otherwise return false try { //Connect the server (NetIP, NetPort); //Detection of connection status return (-1, ); } catch (SocketException ex) { throw new Exception(); } } // Send messages to the specified Socket object in a synchronous manner public static void SendMsg(Socket NetSocket, byte[] dataStr) { //Send a message (dataStr, , ); } // Send messages to specified Socket objects in a synchronous manner using UTF8 encoding format public static void SendMsg(Socket NetSocket, string dataStr) { //Convert string message to character array byte[] NetBuf = .(dataStr); //Send a message (NetBuf, , ); } // Receive messages in synchronous manner public static void ReceiveMsg(Socket NetSocket, byte[] NetBuf) { (NetBuf); } // Receive messages in synchronous manner public static string ReceiveMsg(Socket NetSocket) { //Define the receive buffer byte[] NetBuf = new byte[10000]; //Receive data and obtain the number of bytes received int RecvNum = (NetBuf); //Define temporary buffer byte[] DataBuf = new byte[RecvNum]; //Write the received data to the temporary buffer (NetBuf, 0, DataBuf, 0, RecvNum); //Train DataBuf return (DataBuf); } // Close Socket object based on Tcp protocol public static void Close(Socket NetSocket) { try { //Socket objects are prohibited from receiving and sending data (); } catch (SocketException ex) { throw ex; } finally { //Close Socket object (); } } } }
Summarize
That’s all for this article. I hope it can help you and I hope you can pay more attention to more of my content!