13.2 Network Programming Technology
The introduction of the basic knowledge of network programming, the initial establishment of the concept of network programming, but the actual learning network programming must also use a programming language for code implementation, the following describes the network programming code implementation.
13.2.1 Network Programming steps
According to the previous basic knowledge, whether using TCP or UDP network communication, network programming is composed of client and server side. Of course, the B/s structure of the programming only need to implement server-side can. Therefore, the following introduction of network programming steps, the C/s structure is based on the introduction.
Description: This step implementation is not language independent, that is to say, this step applies to various language implementations, not limited to the Java language.
13.2.1.1 Client Network Programming steps
Client refers to the network programming in the first connection of the program, the client general implementation of the program interface and basic logic implementation, in the actual client programming, regardless of the client complex or simple, as well as the way the client implementation, client programming is mainly implemented by three steps:
1. Establish network connection
The first step in client network programming is to establish a network connection. When establishing a network connection, you need to specify the IP address and port number of the server to which you are connected, and after the completion, a virtual connection will be formed, and subsequent operations will enable the exchange of data through that connection.
2. Exchange data
After the connection is established, the data can be exchanged through this connection. Exchange data in strict accordance with the request response model, the client sends a request data to the server, the server feedback a response data to the client, if the client does not send a request, the server side does not respond.
Depending on your logical needs, you can exchange data multiple times, but you must still follow the request response model.
3. Turn off network connection
After the data exchange completes, closes the network connection, releases the program occupation the port, the memory and so on system resources, ends the network programming.
The most basic steps are generally these three steps, in the actual implementation, step 2 will be duplicated, in the code organization, because the network programming is more time-consuming operation, so generally open a dedicated site for network communication.
13.2.1.2 server-side network programming steps
Server-side (servers) refers to the process of passively waiting for connection in network programming, and the core functions of server-side to realize program's core logic and data storage. The server-side programming steps are different from the client, and are implemented in four steps, followed by:
1. Listening port
The server side belongs to a passive wait connection, so the server-side startup does not need to initiate a connection, but only listens to a fixed port on the local computer.
This port is the server-side open to the client port, the server-side program running the local computer's IP address is the server-side program IP address.
2. Get connection
When the client connects to the server side, the server can get a connection that contains the client's information, such as the client IP address, and so on, and the server-side and the client also exchange data through the connection.
Typically in server-side programming, when a connection is obtained, a dedicated thread is opened to handle the connection, each of which is implemented by a separate thread.
3. Exchange data
The server side is exchanging data through the connection obtained. The server-side data exchange step is to receive the data sent by the client first, then the logic processing, and then the processing of the results of the data sent to the client. In short, it is to receive and resend, which is different from the client's data exchange sequence.
In fact, the server-side access to the connection and the client connection is the same, but the data exchange steps are different.
Of course, server-side data exchange can also be done several times.
After the data exchange completes, close the connection to the client.