Network communication and network communication protocols
Overview 1. Network Model
The OSI (Open System Interconnection) model summarizes the network System structure and divides the network into seven layers: application Layer, presentation layer, Session Layer, transmission layer, network layer, data link layer, and physical layer.
2. IP protocol
The Network Layer Protocol specifies the rules for determining and searching for computers on the Internet.
3. TCP protocol
A data transmission protocol in the transport layer. A connection is established through a three-way handshake before data transmission, and then data is sent. This protocol is suitable for situations with high data accuracy requirements, the transmission speed is slow because a connection needs to be established before data transmission.
4. UDP protocol
A data transmission protocol in the transmission layer. No connection is required before data transmission. This Protocol is applicable to situations where data accuracy is not high and data transmission is fast. Generally, chat information is transmitted through this protocol.
5. HTTP protocol
HTTP is an application layer protocol that provides interfaces for operating systems or network applications to access network services.
6. port
After the data arrives at the computer, an integer is assigned to each application to locate the target application. The value ranges from 0 to 65535. The integer is the port, the port represents an application on the computer to ensure that the data is accurate to the predefined program. A port cannot be occupied by multiple applications at the same time. After an application ends, the port will not be released immediately. There is a memory delay, which is usually very short. Ports 0-are occupied by system applications and other applications. Avoid using ports in this range during programming.
7. Socket
Socket is a tool for sending and receiving data. The sender sends data through the socket, and the receiver obtains data through the port specified by the socket listener.
8. data can only be sent in bytes regardless of the TCP or UDP protocol.
2. TCP Program Design
1. Close the input stream or output stream obtained through Socket will close the Socket.
2. The output stream obtained through Socket must be closed after the output is complete, otherwise the corresponding input stream at the other end will be blocked. When the output stream object is used to close the output stream and the Socket object is closed at the same time, the other end cannot obtain the corresponding Socket object. Therefore, the output stream can only be closed using the method shutdownOutput in the Socket.
3. General steps of the client:
Socket socket = new Socket (String host, int port); // create a client Socket to send and receive data. Specify the Server IP address and port OutputStream OS = socket. getOutputStream (); // get the output stream and send data to the server .......... OS. flush (); socket. shutdownOutput (); // close the output stream to prevent the server from blocking InputStream is = socket. getInputStream (); // get the input stream. The input stream contains the server feedback ............ socket. close (); // close the socket and close the input and output streams.
4. General steps of the server:
ServerSocket server = new ServerSocket (int port); // create a server Socket and specify the listening port socket = server. accept (); // obtain the Socket used to access the client, blocking the thread InputStream is = socket. getInputStream (); // get the input stream, which contains the data sent by the client ............. outputStream OS = socket. getOutputStream (); // gets the output stream and sends feedback to the client .............. OS. flush (); OS. shutdownOutput (); server. close ();
5. Demo Client
Package com. javase. networkCommunication. tcp. demo02; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. socket; import java.net. unknownHostException; public class ImgClient {public static void main (String [] args) throws UnknownHostException, IOException {Socket socket = new Socket ("192.168.146.1", 10007); FileInputStream is = New FileInputStream ("Files/1.jpg"); OutputStream OS = socket. getOutputStream (); byte [] buf = new byte [1024]; // read data to the buffer first, faster than reading data from the hard disk. int length = 0; while (length = is. read (buf ))! =-1) {OS. write (buf, 0, length);} OS. flush (); socket. shutdownOutput (); // If the output stream is not closed, the input stream corresponding to the server will block InputStream replyIs = socket. getInputStream (); // The thread byte [] buf01 = new byte [1024]; int length01 = replyIs. read (buf01); String reply = new String (buf01, 0, length01); System. out. println (reply); is. close (); socket. close ();}}
Server
Package com. javase. networkCommunication. tcp. demo02; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. serverSocket; import java.net. socket; import org. junit. test; public class ImgServer {@ Test public void test01 () throws IOException {ServerSocket serverSocket = new ServerSocket (10007); Socket socket = serverSocket. accept (); // Thread blocking, waiting for the request System. out. println ("hostAddress =" + socket. getInetAddress (). getHostAddress (); InputStream is = socket. getInputStream (); FileOutputStream OS = new FileOutputStream ("Files/2.jpg"); System. out. println (1); byte [] buf = new byte [1024]; int length = 0; System. out. println (2); int count = 3; while (length = is. read (buf ))! =-1) {OS. write (buf, 0, length); System. out. println (count ++);} OS. flush (); OS. close (); System. out. println ("Image Upload completed");/* PrintWriter out = new PrintWriter (socket. getOutputStream (), true); out. write ("success"); */OutputStream out = socket. getOutputStream (); out. write ("success ". getBytes (); out. flush (); socket. shutdownOutput (); System. out. println ("response data sent"); serverSocket. close ();}}
3. UDP programming 1. Data Processing Methods
UDP sends data in the form of data packets. The maximum value of each packet is 64 KB.
2. General steps for sending data:
DatagramSocket socket = new DatagramSocket (); // create a datagram socket for sending data // The DUP protocol uses data packets to send data in segments. Therefore, you need to establish a data packet, specify the destination IP address and port in the data packet: DatagramPacket packet = DatagramPacket (byte buf [], int offset, int length, InetAddress address, int port); socket. send (packet );
3. General steps for receiving data:
DatagramSocket socket = new DatagramSocket (int port); // creates a datagram socket for listening to a specified port. DatagramPacket packet = new DatagramPacket (byte buf [], int length); socket. receive (packet );