Socket的經常使用方法。

構造方法:java

1.使用構造方法 public ServerSocket(int port) 和public ServerSocket(int port,int backlog)windows

建立ServerSocket對象,則客戶端能夠使用服務器任意的IP鏈接到ServerSocket對象中。服務器

2. 使用public ServeSocket(int port,int backlog,InetAddress bindAddr)構造方法中的參數bindAddr建立ServerSocket對象後,socket

客戶端想要鏈接到服務端,則客戶端Socket的構造方法的參數要寫上與ServerSocket構造方法的參數bindAddr相同的IP地址,測試

否則就會出現異常。spa

綁定指定的Socket地址:.net

public void bind(SocketAddress endpoint)方法的主要做用是將ServerSocket綁定到特定的Socket地址(IP地址和端口號),code

使用這個地址與客戶端進行通訊。若是地址爲null,則系統將挑選一個臨時的端口和一個有效的本地地址來綁定套接字。server

SocketAddress自己是一個抽象類,表明Socket地址;而InetAdress類表明IP地址。對象

InetAddress構造方法:

1.public InetSocketAddress(int port)的做用是建立套接字地址,其中IP地址爲通配符地址,端口號爲指定值。有效端口0~65535之間;

端口號傳入0表明bind操做中隨機挑選空閒的端口。

2:public InetSocketAddress(String hostname,int port的做用是根據主機名和端口號建立套接字地址。

3:public InetSocketAddress(InetAddress addr,int port)的做用根據IP地址和端口號建立套接字地址。

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()獲取bending的SocketAddress對象,返回此Socket綁定的端點的地址,若是還沒有綁定,則返回null.

getLocalPort()方法用來獲取Socket綁定到本地的端口。

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與InetAddress本質的區別是SocketAddress不基於任何協議。

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()和getHostString()方法的區別

getHostName()方法的做用是獲取主機名。注意,若是地址是字面IP地址建立的,則此方法可能觸發名稱服務反向查找,也就是DNS服務經過IP找到域名。

getHostString()方法的做用是返回住居名或地址的字符串形式,若是它沒有主機名,則返回IP地址。這樣作的好處是不嘗試返回查找。

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());
    }
}

測試結果:

相關文章
相關標籤/搜索