OSI model layering
The OSI model refers to the open System Interconnect Reference Model (interconnection Reference MODEL,OSI/RM) proposed by the International Organization for Standardization (ISO), which divides the network into seven tiers: physical, data link, network, transport, Session layer, Presentation layer, application layer
TCP/IP protocol
TCP/IP is a general term for a series of network communication protocols, of which the two most important protocols are TCP and IP. TCP is called the Transmission Control Protocol, and IP is called an internetwork.
In addition to the OSI model layering, there are layered TCP/IP models, and the network is divided into four layers, application layer, transport layer, network layer and network interface layer. TCP/IP model layering is a condensed version of the OSI model hierarchy
The corresponding structure of OSI model and TCP/IP network hierarchy and TCP/IP partial protocol family,
TCP Three-time handshake
First handshake: establishes the connection. The client sends the connection request message segment, the SYN position is 1,sequence number x, and then the client enters the Syn_send state, waiting for the server to confirm;
Second handshake: The server receives a SYN message segment. The server receives the SYN message segment of the client, needs to confirm this SYN segment, set acknowledgment number to x+1 (Sequence number+1), and send the SYN request information yourself, with the SYN position at 1 Sequence number is Y; the server will put all of the above information into a message segment (that is, the Syn+ack message segment), a concurrent send to the client, when the server entered the SYN_RECV state;
Third handshake: The client receives the server's Syn+ack message segment. Then the acknowledgment number is set to Y+1, send an ACK message to the server segment, after the message segment is sent, the client and server side both enter the established state, complete the TCP three handshake.
Three-time handshake diagram
JAVA Socket
Java uses ServerSocket (server side) and socket (client) for TCP protocol-based network communication, and the server-side pain over Accetp () method listens for client connections.
After the connection is established, the getInputStream and Getoutputstream of the socket object can be used for normal IO operation, and the communication between the two sides is similar to that of local communication.
The following is a simple example of server-side and client communication,
Server-side,
1 PackageNetwork;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 Importjava.io.IOException;6 ImportJava.io.InputStreamReader;7 ImportJava.io.OutputStreamWriter;8 ImportJava.io.PrintStream;9 ImportJava.net.ServerSocket;Ten ImportJava.net.Socket; One A Public classServer { - Public Static voidMain (string[] args)throwsIOException { - //Create a server socket for port 30000 theServerSocket SS =NewServerSocket (3500); - //The accept will block until the client socket is returned -Socket s =ss.accept (); -BufferedWriter WR =NewBufferedWriter (NewOutputStreamWriter (S.getoutputstream ())); +BufferedReader br =NewBufferedReader (NewInputStreamReader (S.getinputstream ())); - inti = 0; + while(true) { A //when you send a message, you need to add a newline character, or the client's ReadLine () will block atWr.write ("Hello, you receive a New year greeting from the server [" + i++ + "]\n"); - //use the Flush method to empty the buffer immediately so that the message is sent out immediately, or the message will not be sent before the buffer is full . - Wr.flush (); - -String line =br.readline (); -SYSTEM.OUT.PRINTLN ("Data from the client:" +Line ); in } - } to}
Client
1 PackageNetwork;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 Importjava.io.IOException;6 ImportJava.io.InputStreamReader;7 ImportJava.io.OutputStreamWriter;8 ImportJava.net.Socket;9 Importjava.net.UnknownHostException;Ten One Public classClient { A Public Static voidMain (string[] args)throwsUnknownhostexception, - IOException, interruptedexception { -Socket socket =NewSocket ("127.0.0.1", 3500); theBufferedWriter WR =NewBufferedWriter (NewOutputStreamWriter (Socket.getoutputstream ())); -BufferedReader br =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); - inti = 0; - while(true) { + //when you send a message, you need to add a newline character, or the server-side ReadLine () will block -Wr.write ("Hello, you receive a New year greeting from the client [" + i++ + "]\n"); + //use the Flush method to empty the buffer immediately so that the message is sent out immediately, or the message will not be sent before the buffer is full . A Wr.flush (); at -String line =br.readline (); -SYSTEM.OUT.PRINTLN ("Data from server:" +Line ); -Thread.Sleep (1000); - } - } in}
Start the server side in the cmd window, then open a new CMD window, start the client, you can see the server side and the client send and receive messages to each other, the execution results are as follows.
Java basic knowledge of network programming ——-TCP/IP protocol, socket communication, Server client communication demo