Common methods of Socket.

Construction method:

1. Constructors public ServerSocket (int port) and public ServerSocket (int port, int backlog)

Create a ServerSocket object, the client can connect to the server ServerSocket object using any IP.

2. Use public ServeSocket constructor parameter (int port, int backlog, InetAddress bindAddr) bindAddr create ServerSocket object,

The client wants to connect to the server, the client write parameters on the Socket constructor with parameters bindAddr ServerSocket constructor of the same IP address,

Otherwise, there will be an exception.

Socket binding specified address:

The main role of public void bind (SocketAddress endpoint) method is to Socket ServerSocket bound to specific address (IP address and port number),

Use this address to communicate with the client. If the address is null, then the system will select a temporary port and a valid local address to bind the socket.

SocketAddress itself is an abstract class that represents Socket address; InetAdress class represents the IP address.

InetAddress Constructor:

Role 1.public InetSocketAddress (int port) is to create a socket address where the IP address is the wildcard address, port number specified value. Valid port between 0 to 65535;

Incoming port number port 0 for bind operations randomly selected idle.

2: public InetSocketAddress (String hostname, int port role is to create a socket address based on the host name and port number.

3: public InetSocketAddress (InetAddress addr, int port) Creates a socket address the role of IP address and port number.

Server.java

package com.company.s8;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;

public class Server {
    public static void main(String[] args)  {
        try {
            ServerSocket serverSocket=new ServerSocket();
            serverSocket.bind(new InetSocketAddress(8888));
            System.out.println("server begin accept");
            serverSocket.accept();
            System.out.println("server end accept");
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("catch "+System.currentTimeMillis());
        }
    }
}

Client.java

package com.company.s8;

import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        try {
            System.out.println("client request begin");
            Socket socket=new Socket("localhost",8888);
            System.out.println("client request end");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

getLocalSocketAddress () Gets bending of SocketAddress objects, return address of the endpoint this Socket bound, if not yet binding, null is returned.

o getLocalPort () method is used to obtain Socket bind to the local ports.

package com.company.s12;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket();
        System.out.println("new ServerSocket 无参构造的端口是:"+serverSocket.getLocalPort());
        //windows再dos窗口ipconfig获取
        serverSocket.bind(new InetSocketAddress("192.168.0.101",8888));
        System.out.println("bind方法之后的端口是:"+serverSocket.getLocalPort());
        InetSocketAddress inetSocketAddress=(InetSocketAddress) serverSocket.getLocalSocketAddress();
        System.out.println("inetSocketAddress.getHostName="+inetSocketAddress.getHostName());
        System.out.println("inetSocketAddress.getHostString="+inetSocketAddress.getHostString());
        System.out.println("inetSocketAddress.getPort="+inetSocketAddress.getPort());
        serverSocket.close();
    }
}

 

SocketAddress difference is the nature of the InetAddress SocketAddress not based on any agreement.

Server.java

package com.company.s12_1;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket();
        InetAddress inetAddress=InetAddress.getByName("localhost");
        InetSocketAddress inetSocketAddress=new InetSocketAddress(inetAddress,8888);
        serverSocket.bind(inetSocketAddress);
        System.out.println("server begin");
        Socket socket=serverSocket.accept();
        System.out.println("server end");
        socket.close();
        serverSocket.close();
    }
}

Client.java

package com.company.s12_1;

import java.io.IOException;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("client begin");
        Socket socket=new Socket("localhost",8888);
        System.out.println("client end");
    }
}

GetHostName difference () and getHostString () method

Role getHostName () method is to obtain the hostname. Note that if the address is a literal IP address created, this method may trigger reverse lookup service name, which is the DNS domain name service to find over IP.

Action getHostString () method returns a string that residence name or address, if it is not the host name, IP address is returned. The benefit of this is not to try to return to find.

package com.company.s12_2;

import java.net.InetSocketAddress;

public class Server {
    public static void main(String[] args) {
        InetSocketAddress inetSocketAddress1=new InetSocketAddress("192.168.0.101",80);
        InetSocketAddress inetSocketAddress2=new InetSocketAddress("192.168.0.101",80);
        System.out.println(inetSocketAddress1.getHostName());
        System.out.println(inetSocketAddress2.getHostString());
    }
}

Test Results:

 

Guess you like

Origin www.cnblogs.com/guoyansi19900907/p/11117188.html