Java network programming from getting started to mastering (+): Socket class getter and Setter methods (1)

Source: Internet
Author: User

In Java classes, getter and setter methods account for a significant proportion. Because there are no keywords in Java that define attributes, getter and setter methods are used to get and set property values for Java classes, such as the GetName and SetName methods used to set the value of the Name property. If a property has only getter methods, then this property is read-only, and if only the setter method, then this property is write-only. There are also many such properties in the socket class to obtain information about the socket and to set the state of the socket object.

First, used to obtain information. Getter Method

We can get 3 kinds of information from the socket object.

1. Server information

For clients, the server has only two information: IP and port. The socket class provides us with 3 ways to get both of these information.

(1) Public inetaddress getinetaddress ()

This method returns a InetAddress object. Through this object, you can get the server's IP, 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 get the port number of the server 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, which can be used to obtain both the IP and port number of the server. However, this method returns a SocketAddress object that can be used only as a parameter of the Connect method to connect to the server, and to obtain the server's IP and port number, The socketaddress must be converted 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 above 3 methods can be called after a call to the socket object is closed. The information they get is still valid after the socket object is closed. If you use IP to connect to the server directly, the return value of GetHostName and Gethostaddress is the same, and both are the IP of the server.

2. Native information

As with server information, there are also two native messages: local IP and the local port number of the binding. This information can also be obtained through 3 methods.

(1) Public inetaddress getlocaladdress ()

This method returns the InetAddress object for this machine. The IP and machine name of this machine can be obtained by this method. When the computer is bound to multiple IPs, which IP is used by the socket object to connect to the server, which IP is returned. If the computer is using ADSL, and the socket object is connected to an IP or domain name on the Internet (such as www.ptpress.com.cn), Getlocaladdress will return the IP temporarily bound by the ADSL connection; We can get the temporary IP of ADSL via 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 ());

(2) public int getlocalport ()

This method allows you to get a port number of the native that the socket object is bound to, or a random number from 1024 to 65,535 if the port number is not bound. Therefore, using this method may not get the same port number each time.

Socket socket = new socket ();
If you use the following bind method for port binding, the Getlocalport method returns 100
Socket.bind (New Inetsocketaddress ("127.0.0.1", 100));
Socket.connect (New Inetsocketaddress ("www.ptpress.com.cn" 80));
System.out.println (Socket.getlocalport ())

(3) Public socketaddress getlocalsocketaddress ()

This method is similar to the Getremotesocketaddress method, and it also gets the port number that the local IP and socket objects are bound to. If you want to get the local IP and port number, you must convert the return value of this method to the Inetsocketaddress object.

Socket socket = new Socket ("www.ptpress.com.cn", 80);
System.out.println (((inetsocketaddress) socket.getlocalsocketaddress ()). GetHostName ());
System.out.println (((inetsocketaddress) socket.getlocalsocketaddress ()). Getport ());

3. Input and output streams for transmitting data

The input and output streams have been used many times in the previous chapters. Let's take a brief look at it here.

(1) Public InputStream getInputStream () throws IOException

Used to obtain an input stream that reads data from the server. The flow to which it is made is the most primitive source. For ease of operation, we often use inputstreamreader and BufferedReader to read string data passed 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 an 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 you use the Write method of OutputStream to output data, you must flush the output buffer with the Flush method to send the data in the output buffer. If you want to output strings, you can use both OutputStreamWriter and BufferedWriter, and their write methods can output data directly using strings as parameters. This differs from the corresponding InputStreamReader and BufferedReader, where only BufferedReader has a ReadLine method, so you must use BufferedReader to directly read the string data.

Java network programming from getting started to mastering (+): Socket class getter and Setter methods (1)

Related Article

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.