Socket Description:
The socket is called a socket, which describes the IP address and port. Hosts on the internet typically run multiple service software while providing several services, each of which opens a socket and is bound to a port, with different ports corresponding to different services. The sockets and ServerSocket classes are located in the java.net package. ServerSocket is used for server, socket is used when establishing network connection. When the connection succeeds, both ends of the application produce a socket instance that completes the required session by manipulating the instance.
Socket Common methods:
-int Getlocalport () Gets the port number that is used locally
-inetaddress getlocaladdress () Gets the local address of the socket binding
-int Getport () Gets the port number used by the remote
-inetaddress.getinetaddress () Gets the remote address of the socket binding
InetAddress Common methods:
-string Getcanonicalhostname () Gets the fully qualified domain name of this IP address.
-string gethostaddress () returns the IP address string
1 //get local address and port number:2 Public voidTestsocket ()throwsexception{3Socket socket=NewSocket ("localhost", 8088);4InetAddress add=socket.getlocaladdress ();5 System.out.println (Add.getcanonicalhostname ());6 System.out.println (Add.gethostname ());7 System.out.println (Add.getlocalport ());8}
1 //get the remote address and port number:2 Public Static voidTestsocket ()throwsexception{3Socket socket=NewSocket ("localhost", 8088);4InetAddress inetadd=socket.getinetaddress ();5 System.out.println (Inetadd.getcanonicalhostname ());6 System.out.println (Inetadd.gethostname ());7 System.out.println (Socket.getlocalport ());8}
Get network input stream and network output stream
Get input stream and output stream through socket, mainly use two methods
-inputstream getInputStream () returns the input stream for this socket
-outstream Getoutputstream () returns the output stream for this socket
Public Static voidTestsocket ()throwsException {Socket Socket=NewSocket ("localhost", 8088); OutputStream out=Socket.getoutputstream (); OutputStreamWriter OSW=NewOutputStreamWriter (out, "UTF-8"); PrintWriter PW=NewPrintWriter (OSW,true); Pw.println ("Content of the output");//Send contentInputStream in=Socket.getinputstream (); InputStreamReader ISR=NewInputStreamReader (In, "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); Br.readline ();//Read content to}
Note: When you are finished using the socket, close the socket to release the system resources
-void Close () closes the socket and also indicates that the obtained input and output stream has been closed.
The difference between TCP and UDP
TCP (transmission Control Protocol, transmission Protocol) is a connection-based protocol, which means that a reliable connection must be established with each other before the data is formally sent and received. The chat tool uses the TCP protocol
UDP (User data Protocol, Subscriber Datagram Protocol) is the protocol that corresponds to TCP. It is a non-connected protocol that does not establish a connection with the other, but sends the packet over directly! Game multi-use UDP protocol
Java Network Programming TCP communication