1.socket Introduction:
Sockets are often referred to as "sockets", which describe IP addresses and ports, and are a handle to a communication chain. Hosts on the internet typically run multiple service software, while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services.
Applications typically use sockets to make requests to the network or to answer network requests. The sockets and ServerSocket classes are located in the java.net package. Serverscoket is used for server, socket is used when establishing network connection. When the connection succeeds, a socket instance is generated on both ends of the application, manipulating the instance to complete the required session.
2. Get the address and port number:
The socket allows you to get the local address and port number, as well as the remote address and port number.
Server-side Source code:
1 Packagesocket;2 3 Importjava.net.InetAddress;4 ImportJava.net.ServerSocket;5 ImportJava.net.Socket;6 7 Public classServer {8 Public Static voidMain (string[] args)throwsException {9 //Create a serversocket and request a 8080 portTenServerSocket SS =NewServerSocket (8080); One //block the method until a socket is connected. The connection succeeds, resulting in the socket instance. ASocket s =ss.accept (); - intLocalPort = S.getlocalport ();//get Local Port -InetAddress Localadd = s.getlocaladdress ();//gets the local address IP of the socket binding theString hostName = Localadd.getcanonicalhostname ();//get the fully qualified domain name for this IP address -String hostaddress = localadd.gethostaddress ();//returns the IP address string (in text representation). -SYSTEM.OUT.PRINTLN ("Local port is:" +localport); -SYSTEM.OUT.PRINTLN ("Fully qualified domain name for the local IP address:" +hostName); +System.out.println ("The string for the local IP is:" +hostaddress); -System.out.println ("************* separator *****************"); + intPort = S.getport ();//get the port used by the remote side AInetAddress Inetadd = s.getinetaddress ();//get socket-bound remote IP atString inetname = Inetadd.getcanonicalhostname ();//get the fully qualified domain name for this IP address -String inetaddress = inetadd.gethostaddress ();//returns the IP address string (in text representation). -SYSTEM.OUT.PRINTLN ("Remote port is:" +port); -SYSTEM.OUT.PRINTLN ("Fully qualified domain name for remote IP address" +inetname); -System.out.println ("String for remote IP is" +inetaddress); - } in}
Customer-side code:
1 Packagesocket;2 3 Importjava.net.InetAddress;4 ImportJava.net.Socket;5 6 Public classClient {7 Public Static voidMain (string[] args)throwsException {8 //connect the host named "LocalHost" to the host 8080 port that is connected to. 9Socket s =NewSocket ("192.168.1.103", 8080);Ten intLocalPort = S.getlocalport ();//get Local Port OneInetAddress Localadd = s.getlocaladdress ();//gets the local address IP of the socket binding AString hostName = Localadd.getcanonicalhostname ();//get the fully qualified domain name for this IP address -String hostaddress = localadd.gethostaddress ();//returns the IP address string (in text representation). -SYSTEM.OUT.PRINTLN ("Local port is:" +localport); theSYSTEM.OUT.PRINTLN ("Fully qualified domain name for the local IP address:" +hostName); -System.out.println ("The string for the local IP is:" +hostaddress); -System.out.println ("************* separator *****************"); - intPort = S.getport ();//get the port used by the remote side +InetAddress Inetadd = s.getinetaddress ();//get socket-bound remote IP -String inetname = Inetadd.getcanonicalhostname ();//get the fully qualified domain name for this IP address +String inetaddress = inetadd.gethostaddress ();//returns the IP address string (in text representation). ASYSTEM.OUT.PRINTLN ("Remote port is:" +port); atSYSTEM.OUT.PRINTLN ("Fully qualified domain name for remote IP address" +inetname); -System.out.println ("String for remote IP is" +inetaddress); - } -}
Note: To open the server first, then open the client. You can use the browser instead of the client because the HTTP protocol is based on the TCP/IP protocol.
Write your own simple Web application Server (1)-tcp communication