Socket的常用方法。


構造方法:

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

創建ServerSocket對象,則客戶端可以使用服務器任意的IP連接到ServerSocket對象中。

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

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

不然就會出現異常。

綁定指定的Socket地址:

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

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

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

測試結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM