Differences between TCP and UDP
TCP is connected, one-to-one, provides reliable delivery, full duplex communication, byte stream oriented UDP is connectionless, best effort delivered, message-oriented
TCP’s three handshakes and four waves
Three-way handshake
The client’s TCP program sends a connection request message to the server after the server to accept connections to, if accept connections, and will send confirmation message to the client, and for the allocation of resources, the client after receiving the confirmation message to the server, sends a confirmation message to the server again and it will be connected to the allocation of resources
Four times to wave
When the client to close the TCP connection request, send the server connection is closed the request, the server receives the request, send confirmation to the client, the client receives on the server side confirmed, will shut down the TCP connection to the server, client when the server not to send data to the client, will be to the client connection release request, After receiving the request, the client sends an acknowledgement to the server. After passing 2MSL, the client enters to connect to the closing request
TCP programming based on Socket
The four steps of the server-side program call ServerSocket(int port) to create a ServerSocket and bind it to the specified interface. The accept() call is used to listen for client requests and listens for connection requests. If the client requests a connection, it accepts the connection, returns a Socket object and calls the getInputStream() and getOutputStream() objects of the Socket() to get output and input streams. Start receiving and sending of network data Close ServerSocket object and Socket object: Client access accept, close communication Socket
UDP programming
Procedure 1 DatagramSocket and DatagramPacket 2 establish the sending end, the receiving end 3 establish the packet 4 call the Socket send, receive method 5 close the Socket sending end and the receiving end are two independent running programs