Socket SOC = new Socket();
soc。 ConnectToServer();
Creating a connection is such a piece of code, in which the parameters need to specify the protocol (TCP/UDP), IP and port, and then the client can send messages using Socket. Send () method. After the server receives it, it can receive the socket of the client by using TcpListener. AcceptTcpClient () method, and also call send () method to feed back to the client.
The existence of so-called UDP packet loss or accuracy error is only relative, a simple conceptual understanding. In fact, UDP is the best choice in the stable network environment of local area network, which is faster and more convenient to program than TCP. As for the so-called handshake, it is only for the detection of the network. Is it really necessary to use it in LAN, unless you are pursuing some fashionable terms and technologies? If you want to design a handshake, it should be an application layer protocol and you need to make it yourself. In fact, QQ uses UDP protocol.
As for the port number, it is impossible for a machine to open two identical listening ports at the same time, but one port number is enough for external connection. For example, if you open port number 2512 (the default port of Oracle database) to handle monitoring, other machines can communicate with you as long as they are connected to this port number. Or Oracle, for example, an Oracle server can establish connections with multiple clients, but the port number is also 25 12, which is not contradictory, because the server only listens to one port, but can receive all client connections. You got it?