Hackneyed Java Network Programming TCP communication (must see)
Socket introduction:
Socket is called "socket" and describes IP address and port. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a socket and is bound to a port. Different ports correspond to different services. The socket and ServerSocket classes are located in Java Net package. ServerSocket is used for the server. Socket is used when establishing network connection. When the connection is successful, a socket instance will be generated at both ends of the application. Operate this instance to complete the required session.
Common socket methods:
-Int getlocalport() gets the port number used locally
-InetAddress getlocaladdress() gets the local address of the socket binding
-Int getport() gets the port number used by the remote end
-InetAddress. Getinetaddress() gets the remote address of the socket binding
Common methods of InetAddress:
-String getcanonicalhostname() gets the fully qualified domain name of this IP address.
-String gethostaddress() returns the IP address string
Get network input stream and network output stream
The following two methods are mainly used to obtain input stream and output stream through socket
-InputStream getinputstream() is used to return the input stream of this socket
-Outstream getoutputstream() is used to return the output stream of this socket
Note: after using the socket for communication, close the socket to release system resources
-Void close() closes the socket, which also means that the acquired input and output streams are closed.
Differences between TCP and UDP
TCP (transmission control protocol) is a connection based protocol, that is, before sending and receiving data formally, a reliable connection must be established with the other party. TCP protocol is used for chat tools
UDP (user data protocol) is a protocol corresponding to TCP. It is a non connection oriented protocol. It does not establish a connection with the other party, but directly sends data packets! Games use UDP protocol more
The above commonplace talk about Java Network Programming TCP communication (must see) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.