1. Two main issues in network programming
One is how to accurately locate one or more hosts on the network, and the other is how to reliably and efficiently transmit data after finding the host.
In the TCP/IP protocol, the IP layer is mainly responsible for the location of the network host and the routing of data transmission. The IP address can uniquely determine a host on the Internet.
The TCP layer provides a reliable (tcp) or non-reliable (UDP) data transmission mechanism for application, which is the main object of network programming, and generally does not need to care about how the IP layer processes data.
The most popular network programming model at present is the client/server (C/S) structure. That is, one of the communication parties acts as a server to wait for the customer to submit a request and respond. The customer applies to the server when the service is needed. The server is generally always running as a daemon, listening to the network port. Once a customer requests it, it will start a service process to respond to the customer, and at the same time continue to listen to the service port itself so that later customers can also get service in a timely manner.
2. Two types of transmission protocols: TCP/UDP
TCP is the abbreviation of Transfer Control Protocol, a connection-oriented protocol that ensures reliable transmission. Transmission through TCP protocol results in an order of error-free data streams. A connection must be established between the two pairs of sockets of the sender and the receiver in order to communicate on the basis of the TCP protocol. When one socket (usually a server socket) is waiting to establish a connection, the other socket can require a connection. Once these two sockets are connected, they can perform two-way data transmission, and both parties can perform sending or receiving operations.
UDP is the abbreviation of User Datagram Protocol. It is a connectionless protocol. Each datagram is an independent information, including a complete source or destination address. It is transmitted to the destination on the network by any possible path. Therefore, whether it can reach the destination, the time to reach the destination, and the correctness of the content cannot be guaranteed.
Compare:
UDP:
1. Each datagram gives complete address information, so there is no need to establish a connection between the sender and the receiver.
2. There is a size limit when DP transmits data, and each transmitted datagram must be limited to 64KB.
3. DP is an unreliable protocol, and the datagrams sent by the sender do not necessarily arrive at the receiver in the same order.
TCP:
1. To connect to the connection protocol, a connection must be established before data transmission between sockets, so connection time is required in TCP.
2. CP transmission data size limit. Once the connection is established, the sockets of both parties can transmit large data in a unified format.
3.CP is a reliable protocol that ensures that the receiver completely correctly obtains all the data sent by the sender.
application:
1. TCP has extremely strong vitality in network communication. For example, remote connection (Telnet) and file transfer (FTP) require data of varying lengths to be reliably transmitted. However, reliable transmission comes at a cost. Verification of the correctness of data content will inevitably occupy the computer's processing time and network bandwidth. Therefore, the efficiency of TCP transmission is not as high as that of UDP.
2. UDP is simple to operate and requires only less supervision, so it is usually used in client/server applications in distributed systems with high reliability in LANs. For example, video conferencing systems do not require the absolute correctness of audio and video data, as long as the consistency is guaranteed, it is obviously more reasonable to use UDP in this case.
3. Socket-based Java network programming
1. Is it a Socket
Two programs on the network realize data exchange through a two-way communication connection. One end of this two-way link is called a Socket. Socket is usually used to connect between customers and service providers. Socket is a very popular programming interface of the TCP/IP protocol. A Socket is uniquely determined by an IP address and a port number.
However, the types of protocols supported by Socket are not only TCP/IP, so there is no necessary connection between the two. In the Java environment, Socket programming mainly refers to network programming based on the TCP/IP protocol.
2. The process of ocket communication
The Server side Listen (listens) whether there is a connection request on a certain port. The Client side issues a Connect request to the Server side, and the Server side sends an Accept message back to the Client side. A connection is established. Both the Server and Client side can communicate with each other through Send, Write and other methods.
For a fully functional Socket, it must include the following basic structure, and its working process includes the following four basic steps:
(1) Create Socket;
(2) Open the input/outflow connected to the Socket;
(3) Read/write the Socket according to a certain protocol;
(4) Close Socket.
3. Build Socket
Create Socket
Java provides two classes Socket and ServerSocket in the package java.net, which are used to represent the client and server for bidirectional connection. These are two very well-packaged classes and are very easy to use. The construction method is as follows:
Socket(InetAddress address, int port);
Socket(InetAddress address, int port, boolean stream);
Socket(String host, int prot);
Socket(String host, int prot, boolean stream);
Socket(SocketImpl impl)
Socket(String host, int port, InetAddress localAddr, int localPort)
Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
ServerSocket(int port);
ServerSocket(int port, int backlog);
ServerSocket(int port, int backlog, InetAddress bindAddr)
The address, host and port are the IP address, host name and port number of the other party in the bidirectional connection respectively. The stream indicates whether the socket is a stream socket or a datagram socket. LocalPort indicates the port number of the local host. LocalAddr and bindAddr are the address of the local machine (the host address of the ServerSocket). Impl is the parent class of the socket. It can be used to create serverSocket and create sockets. count represents the maximum number of connections that the server can support. For example:
Socket client = new Socket("127.0.0.1", 8888);
ServerSocket server = new ServerSocket(8888);
Note that you must be careful when selecting a port. Each port provides a specific service. Only by giving the correct port can the corresponding service be obtained. The port numbers from 0~1023 are reserved by the system. For example, the port number of the http service is 80, the port number of the telnet service is 21, and the port number of the ftp service is 23. Therefore, when we select the port number, it is best to choose a number greater than 1023 to prevent conflicts.
If an error occurs when creating a socket, an IOException will be generated and it must be processed in the program. So when creating a Socket or ServerSocket, it is necessary to catch or throw an exception.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.