The simplest way to connect a server with a socket is to use the IP and port directly, but the Connect method in the socket class does not provide this way, but instead uses the SocketAddress class to pass the server's IP and port to the Connect method. While this is a seemingly troublesome approach, it brings us another benefit: The reuse of network addresses .
The reuse of so-called network addresses is represented in two ways:
1. By creating a SocketAddress object, you can use this SocketAddress object when connecting to the same server multiple times.
2. There are two methods available in the Socket class: Getremotesocketaddress and Getlocalsocketaddress, which can be used to obtain the network address of the server and the local computer. and the resulting network address in the corresponding socket object is closed after the subsequent can be used. The following are the declarations of these two methods:
Public socketaddress getremotesocketaddress () public socketaddress getlocalsocketaddress ()
Both methods return the network address in socketaddress form, regardless of whether the socket class is used to connect to the server using the IP and port directly, or if socketaddress is used. Both methods return NULL when the socket object is not connected, but it is important to note that both methods return NULL when the socket object is not connected, and that both methods can be used to obtain the corresponding network address when the socket object that has been successfully connected is closed.
Although the above mentioned socketaddress, but SocketAddress is an abstract class, except that there is a default constructor method, the other methods are abstract, so, We must use socketaddress subclasses to build socketaddress objects. In JDK1.4, J only provides us with an implementation class for IP network addresses: java.net.InetSocketAddress. This class is inherited from SocketAddress, and we can build the SocketAddress object by the following method.
SocketAddress socketaddress = new Inetsocketaddress (host, IP);
The following code demonstrates how to share a network address through SocketAddress:
Packagemynet;
Import java.net.*;
public class Mysocketaddress { Public static void Main (string[] args){ Try{ Socket SOCKET1 = new socket ("www.ptpress.com.cn", 80); socketaddress socketaddress = socket1.getremotesocketaddress (); Socket1.close (); Socket Socket2 = new socket (); //Socket2.bind (New Inetsocketaddress ("192.168.18.252", 0)); Socket2.connect (socketaddress); Socket2.close (); inetsocketaddress InetSocketAddress1 = (inetsocketaddress) socketaddress; System.out.println ("Server domain name:" + inetsocketaddress1.getaddress (). GetHostName ()); System.out.println ("Server IP:" + inetsocketaddress1.getaddress (). gethostaddress ()); System.out.println ("Server port:" + inetsocketaddress1.getport ()); inetsocketaddress inetSocketAddress2 = (inetsocketaddress) Socket2. getlocalsocketaddress (); System.out.println ("Local IP:" + inetsocketaddress2.getaddress (). Getlocalhost (). gethostaddress ()); System.out.println ("Local port:" + inetsocketaddress2.getport ()); } catch (Exception e){ System.out.println (E.getmessage ()); } } }
Output Result:
Server domain: www.ptpress.com.cn
Server ip:219.238.168.74
Server Port: 80
Local ip:192.168.18.253
Local Port: 4250
If you run routine 4-10 multiple times, the value of the local port may be different each time. This is because the Socket2 does not use bind to bind the local port at the time of the connection, and the local port is randomly selected by the system from 1024 to 65,535, so the local port is not necessarily the same every time the program is run.
Java network programming from beginner to proficient (15): Why use socketaddress to manage network addresses