When using Socket to connect to the server, the simplest way is to directly use the IP address and port, but the connect Method in the Socket class does not provide this method, instead, the SocketAddress class is used to pass the Server IP address and port to the connect method. Although this method is obviously troublesome, it will bring us another benefit, that is, the reuse of network addresses.
The reuse of network addresses involves two aspects:
1. By creating a SocketAddress object, you can use this SocketAddress object when connecting to the same server multiple times.
2. Two methods are provided in the Socket class: getRemoteSocketAddress and getLocalSocketAddress. The network address of the server and the local machine can be obtained through these two methods. The obtained network address can be used after the corresponding Socket object is closed. The declaration of these two methods is as follows:
Public SocketAddress getRemoteSocketAddress ()
Public SocketAddress getLocalSocketAddress ()
Whether the IP address and port are used directly when the Socket class is used to connect to the server, or SocketAddress is used, both methods return the network address in the form of SocketAddress. When the Socket object is not connected, the two methods return null, but note that only when the Socket object is not connected, the two methods return null, when the Socket object that has been connected successfully is closed, you can still use these two methods to obtain the corresponding network address.
Although SocketAddress is mentioned many times above, SocketAddress is just an abstract class. Besides a default constructor, all other methods are abstract. Therefore, we must use the subclass of SocketAddress to create the SocketAddress object. In JDK1.4, J only provides the implementation class of the IP Address: java.net. InetSocketAddress. This class is inherited from SocketAddress. We can use the following method to create a SocketAddress object.
SocketAddress socketAddress = new InetSocketAddress (host, ip );
The following code demonstrates how to share a network address through SocketAddress:
Package mynet;
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 Name: www.ptpress.com.cn
Server IP Address: 219.238.168.74
Server port: 80
Local IP Address: 192.168.18.253
Local Port 4250
If the value of the local port is different after 4-10 is run for multiple times. This is because socket2 does not use bind to bind a local port during connection, and the local port is randomly selected by the system from 1024 to 65,535. Therefore, the local port is not necessarily the same during each program running.