Network Programming for learning contents of textbook
• Network programming is the transfer of data between two or more than two devices, such as a computer.
What the programmer is doing is sending the data to the specified location, or receiving the specified data, which is the narrow network programming category.
• When sending and receiving data, most programming languages design specialized APIs to implement these functions, and programmers only need to invoke them.
Network overview
• Network programming is the exchange of data between two or more devices (Programs).
• Identify each device on the network:
• IP Address: Each computer after the network has a unique legitimate IP address, like mobile phone number, in the computer network, now named IP address is the IPV4 protocol, the Protocol stipulates that each IP address consists of 4 numbers between 0-255. This IP address may be dynamic or fixed.
• Domain name.
• One IP address can correspond to multiple domain names, and a domain name can only correspond to one IP address.
• DNS server: The data transmitted in the network, all the IP address as the address identification, so in the actual transfer of data before the need to convert the domain name to an IP address, the implementation of such a function of the server called a DNS server, that is, popular parlance is called Domain name resolution. When the DNS server is working properly, it is convenient to use an IP address or domain name to find a device in the computer network, such as a server computer. When DNS is not working properly, the device can only be accessed through an IP address.
C/S Structure: The structure of network programming is called the client/server structure, also called the client/server structure.
B/S structure: Using the browser as the client's structure is called the browser/server structure, also called the browser/server structure. b/s structure is actually a special C/s structure.
P2P Program: In-Peer program contains both client programs and server-side programs, is a special program.
• Port: Allows a computer to run multiple network programs at the same time. The port number must be between 0-65535, and each port is unique to one network program, and a network program can use multiple ports.
• Protocol (PROTOCOL): In the actual data exchange, in order to let the receiver understand the data, it is necessary to specify the format of the data, the format of the data is the protocol.
• How to write the protocol format: As long as the protocol format can generate a unique encoding, according to the code can only parse the contents of the sending data. It is also because of the different protocol format between the various network programs, so that the client program is a dedicated structure.
• There are two main ways of network communication in an existing network:
TCP (Transmission Control Protocol) mode: When network communication, the need to establish a dedicated virtual connection, then reliable data transmission, if the data sent failed, the client will automatically resend the data.
UDP (User Datagram Protocol) mode: in the network communication, do not need to establish a dedicated virtual connection, transmission is not very reliable, if the sending fails the client is not available.
• Network programming generally has two types of structure:
• Client/server structure, also called client/server structure, referred to as C/s structure. The advantages are rich expression, the disadvantage is the poor versatility, the maintenance of the pressure ratio is large.
• Browser/server structure, also called browser/server structure, referred to as B/s structure. The advantage is that the development is less stressful and does not require maintenance of the client. The disadvantage is the limitation is big, the expressive force is not strong, cannot carry on the system level operation and so on.
Network Programming Technology
• The programming of the client is mainly implemented in three steps:
• 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 establishment is completed, a virtual connection is formed, and subsequent operations can be exchanged through the connection.
• Exchange data: Once 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 the request, the server side will not respond. Depending on your logic needs, you can exchange data multiple times, but you still have to follow the request response model.
• Turn off network connectivity: After the data exchange is complete, close the network connection, release the system resources such as port, memory, etc., and end the network programming.
• Server-side network programming steps:
• Listening port: The server side is passive waiting for a connection, so after the server is started, you do not need to initiate a connection, but only need to listen to a fixed port on the local computer. This port is the server-side open to the client port, the server-side program runs the local computer's IP address is the server-side program IP address.
• Get Connected: When the client connects to the server side, the server can get a connection that contains the client's information, and the server side and the client also exchange data through the connection. Typically in server-side programming, when you get a connection, you need to turn on a dedicated thread to handle the connection, and each connection is implemented by a separate thread.
• Exchanging data: The server side is exchanging data through the connections obtained. The server-side data exchange step is to first receive the data sent by the client, then the logical processing, and then the processing of the resulting data sent to the client. This differs from the client's data exchange sequence. The server-side connection is the same as the client connection, except that the steps for data exchange are different. Of course, the server-side data exchange can also be done multiple times. After the data exchange is complete, the connection to the client is closed.
• Close the connection: when the server program shuts down, the server side needs to be shut down, and the port that the server listens to and the memory consumed can be freed by shutting down the server, enabling the connection to close.
• The code to implement server-side snooping is:
new ServerSocket(10000);
• Implementation of UDP programming, including client network programming and server-side network programming, mainly implemented by two classes, respectively:
Datagramsocket: Implement network connections, including client network connections and server-side network connections. The Datagramsocket implements the transmitter when the data is sent, and the role of the listener when the data is received. Analogous to a network connection in TCP, this class can be used either to implement client connections or to implement server-side connections.
Datagrampacket: Implements the encapsulation of data that is transmitted over the network, and objects of that class represent the data exchanged in the network. In UDP network programming, whether the data to be sent or the data to be received must be processed into an object of type Datagrampacket, which contains the address sent to, the port number sent to, and the content sent. Compared with the TCP network transmission, IO programming is not necessary in the network programming of UDP, and the structure is simpler than the TCP mode of network programming.
InetAddress class: The function of this class is to represent an IP address, and the IP address and domain name-related action methods are included inside the class.
TCP Programming: In the Java language, for TCP network programming provides good support, in the actual implementation, the Java.net.Socket class on behalf of the client connection, the Java.net.ServerSocket class represents the server-side connection. In network programming, the details of the underlying network communication have been implemented in a relatively high package, so when the programmer actually programming, only need to specify the IP address and port number to establish a connection. It is because of this high-level encapsulation, on the one hand simplifies the Java language Network programming difficulty, also makes the Java language network programming cannot penetrate to the network bottom, therefore uses the Java language to carry on the network low-level system programming is very difficult, but because the Java language network programming is relatively simple, Therefore, it has been widely used.
Java The implementation steps of the client in the language:
• Establish a connection:
Socket socket1 = new Socket(“192.168.1.103”,10000);Socket socket2 = new Socket(“www.sohu.com”,80);
• Data exchange:
OutputStream os = socket1.getOutputStream(); //获得输出流InputStream is = socket1.getInputStream(); //获得输入流
• Turn off the network connection:
socket1.close();
Java The implementation steps of the service side in the language:
• Listening Port:
new ServerSocket(10000);
• Get Connected:
socket = ss.accept();
• Data exchange:
socket = serverSocket.accept(); //接收客户端发送内容is = socket.getInputStream();
• Turn off the network connection:
ss.close();
UDP programming uses two main classes:
Datagramsocket: Implement network connections, including client network connections and server-side network connections. Although the UDP mode of network communication does not need to establish a dedicated network connection, but after all, it is necessary to send and receive data, Datagramsocket implementation is the transmitter when sending data, and the role of the listener when receiving data. Analogous to a network connection in TCP, this class can be used either to implement client connections or to implement server-side connections.
Datagrampacket: Implements data encapsulation for transport in the network. In UDP network programming, whether the data to be sent or the data to be received must be processed into an object of type Datagrampacket, which contains the address sent to, the port number sent to, and the content sent. In fact, the role of the Datagrampacket class is similar to the real letter, in the letter contains the address of the letter sent to the recipient, as well as the content sent, the post office only need to follow the address to pass. When receiving data, the received data must also be processed into an object of type Datagrampacket, which contains information such as the sender's address, port number, and the contents of the data. Compared with the TCP network transmission, IO programming is not necessary in the network programming of UDP, and the structure is simpler than the TCP mode of network programming.
Java The implementation steps of the client in the language:
• Establish a connection:
new DatagramSocket();
Or
new DatagramSocket(5000);
• Data exchange:
String s = "Hello"; String host = "127.0.0.1 ";int port =10001;Converts the sent content to a byte arrayByte[] B = s.getbytes ();Convert the server IP to the inetaddress object inetaddress Server = Inetaddress.getbyname (host);Constructs a sent packet object Datagrampacket SENDDP =New Datagrampacket (B,b.length,server,port);//Send data Ds.send (SENDDP); //construct buffer array byte[] data = new byte[]; Constructs a packet object Datagrampacket received = new Datagrampacket (data,data.length); Receive Data ds.receive (RECEIVEDP); //Output data content byte[] b = receivedp.getdata (); //Get buffer array int len = receivedp.getlength (); //Get valid data length string s = new string (b,0,len); System.out. println (s);
• Turn off the network connection:
ds.close();
Java The implementation steps of the service side in the language:
• Listening Port:
new DatagramSocket(10010);
• Data exchange: With client, port number required when sending
//获得客户端的IPInetAddress clientIP = receiveDp.getAddress();//获得客户端的端口号Int clientPort = receiveDp.getPort();
• Turn off the network connection:
ds.close();
Network protocol
• Network protocol refers to the provision of data formats for transmission over a network. After the network protocol is designed, in the network programming, it is necessary to do the corresponding coding in the program according to the well-designed protocol format.
• The processing that the client program needs to complete is:
•客户端发送协议格式的生成 •服务器端反馈数据格式的解析
• The processing required by the server-side program is:
•服务器端反馈协议格式的生成 •客户端发送协议格式的解析
代码统计:
代码托管:
学习进度条:
|
Lines of code (new/cumulative) |
Blog volume (Add/accumulate) |
Learning time (new/cumulative) |
Important growth |
| Goal |
3500 rows |
20 articles |
300 hours |
|
| First week |
120/120 |
1/1 |
14/14 |
|
| Second week |
340/460 |
1/2 |
14/28 |
|
| Third week |
200/660 |
1/3 |
14/42 |
|
| Week Four |
320/980 |
1/4 |
14/56 |
|
| Week Five |
280/1260 |
1/5 |
14/70 |
|
| Week Six |
478/1738 |
2/7 |
16/86 |
|
| Seventh Week |
425/2163 |
2/9 |
16/102 |
|
| Eighth Week |
859/3022 |
3/12 |
16/118 |
Learn to use Git managed code, using the WC statistic code line number, refactoring. |
| Nineth Week |
4482/7504 |
2/14 |
21/139 |
Using the Android development platform |
| Tenth Week |
1171/8675 |
2/16 |
20/159 |
Learn and Master network programming |
20145326 Java programming 10th Week of study summary