Java Network Programming from entry to mastery (17): Socket class getter and setter methods (1)

Source: Internet
Author: User

In Java classes, getter and setter methods account for a large proportion. The getter and setter methods are used to obtain and set the Java class attribute values. For example, the getName and setName methods are used to set the value of the name attribute. If a property only has the getter method, the property is read-only. If only the setter method is used, the property is only written. There are also many such attributes in the Socket class to obtain Socket-related information and set the status of the Socket object.

1. getter method used to obtain information

We can obtain three types of information from the Socket object.

1. Server Information

For the client, there are only two server information: IP address and port. The Socket class provides three methods for us to obtain the two information.

(1) public InetAddress getInetAddress ()

This method returns an InetAddress object. This object can be used to obtain the server's IP address, domain name, and other information.


Socket socket = new Socket ("www.ptpress.com.cn", 80 );
System. out. println (socket. getInetAddress (). getHostAddress ());
System. out. println (socket. getInetAddress (). getHostName ());

 


(2) public int getPort ()

This method can obtain the server port number in integer form.


Socket socket = new Socket ("www.ptpress.com.cn", 80 );
System. out. println (socket. getInetAddress (). getPort ());


(3) public SocketAddress getRemoteSocketAddress ()

This method combines the getInetAddress and getPort methods. You can use this method to obtain the IP address and port number of the server at the same time. However, this method returns a SocketAddress object, which can only be used as a parameter of the connect Method to connect to the server. To obtain the IP address and port number of the server, you must convert SocketAddress to its subclass InetSocketAddress.


Socket socket = new Socket ("www.ptpress.com.cn", 80 );
System. out. println (InetSocketAddress) socket. getRemoteSocketAddress (). getHostName ());
System. out. println (InetSocketAddress) socket. getRemoteSocketAddress (). getPort ());

Note: The preceding three methods can be called after the Socket object is called. The information they obtain is still valid after the Socket object is closed. If you directly use an IP address to connect to the server, the returned values of getHostName and getHostAddress are the same.


2. Local Information

Like the server information, the local information also has two local IP addresses and the bound local port numbers. This information can also be obtained through three methods.

(1) public InetAddress getLocalAddress ()

This method returns the InetAddress object of the local machine. This method can be used to obtain the IP address and machine name of the local machine. When the local machine is bound with multiple IP addresses, the IP address used by the Socket object to connect to the server will be returned. If the local machine uses ADSL to access the Internet and uses a Socket object to connect to an IP address or domain name on the Internet (for example, www.ptpress.com.cn), getLocalAddress will return the IP address temporarily bound to the "ADSL connection; therefore, we can get the temporary IP address of ADSL through getLocalAddress.


Socket socket = new Socket ();
Socket. connect (new InetSocketAddress ("www.ptpress.com.cn", 80 ));
System. out. println (socket. getLocalAddress (). getHostAddress ());
System. out. println (socket. getLocalAddress (). getHostName ());


3. input and output streams for data transmission

The input and output streams are used multiple times in the previous section. Here we will give a brief review.

(1) public InputStream getInputStream () throws IOException

Used to obtain the input stream for reading data from the server. Its stream is the original source. For ease of operation, we often use InputStreamReader and BufferedReader to read string data from the server.


Socket socket = new Socket ("www.ptpress.com.cn", 80 );
InputStream inputStream = socket. getInputStream ();
InputStreamReader inputStreamReader = new InputStreamReader (inputStream );
BufferedReader bufferedReader = new BufferedReader (inputStreamReader );
System. out. println (bufferedReader. readLine ());


(2) public OutputStream getOutputStream () throws IOException

Used to obtain the output stream that sends data to the server. The output stream can write string data to the server through OutputStreamWriter and BufferedWriter.


Socket socket = new Socket ("www.ptpress.com.cn", 80 );
OutputStream outputStream = socket. getOutputStream ();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter (outputStream );
BufferedWriter bufferedWriter = new BufferedWriter (outputStreamWriter );
BufferedWriter. write ("hello ");
BufferedWriter. flush ();


Note: after using the write method of OutputStream to output data, you must use the flush method to refresh the output buffer to send the data in the output buffer. If you want to output strings, both OutputStreamWriter and BufferedWriter can be used. both of their write methods can directly use strings as parameters to output data. This is different from the corresponding InputStreamReader and BufferedReader. Only BufferedReader has the readLine method. Therefore, you must use BufferedReader to directly read string data.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.