socketchannel,SocketFactory、DefaultSocketFactory、ServerSocketFactory、DefaultServ

 2023-10-18 阅读 31 评论 0

摘要:  SocketFactory是一個抽象類(抽象類不能被實例化,但可以有構造函數)。   描述:This class creates sockets./此類用于創建sockets public abstract class SocketFactory {private static SocketFactory theFactory;  //返回環境默認的sock

  SocketFactory是一個抽象類(抽象類不能被實例化,但可以有構造函數)。

  描述:This class creates sockets./此類用于創建sockets

public abstract class SocketFactory {private static SocketFactory theFactory;
  //返回環境默認的socket工廠的副本。
public static SocketFactory getDefault() {synchronized (SocketFactory.class) {if (theFactory == null) {theFactory = new DefaultSocketFactory();}}return theFactory;}
  //創建一個無連接的socket(拋出SocketException("Unconnected sockets not implemented")異常)
public Socket createSocket() throws IOException {UnsupportedOperationException localUnsupportedOperationException = new UnsupportedOperationException();SocketException localSocketException = new SocketException("Unconnected sockets not implemented");localSocketException.initCause(localUnsupportedOperationException);throw localSocketException;}//創建一個套接字并將其連接到指定地址的指定端口號。public abstract Socket createSocket(String host, int port) throws IOException, UnknownHostException; //創建套接字并將其連接到指定遠程主機的指定遠程端口號。public abstract Socket createSocket(String host, int port, InetAddress localHost, int localPort)throws IOException, UnknownHostException;//創建一個套接字并將其連接到指定地址的指定端口號。public abstract Socket createSocket(InetAddress address, int port) throws IOException;//創建套接字并將其連接到指定遠程地址上的指定遠程端口。public abstract Socket createSocket(InetAddress address, int port, InetAddress localAdress,int localPort) throws IOException; }

  DefaultSocketFactory是SocketFactory的子類。socket創建的默認工廠類

class DefaultSocketFactory extends SocketFactory {public Socket createSocket() {return new Socket();}public Socket createSocket(String paramString, int paramInt) throws IOException, UnknownHostException {return new Socket(paramString, paramInt);}public Socket createSocket(InetAddress paramInetAddress, int paramInt) throws IOException {return new Socket(paramInetAddress, paramInt);}public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)throws IOException, UnknownHostException {return new Socket(paramString, paramInt1, paramInetAddress, paramInt2);}public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2,int paramInt2) throws IOException {return new Socket(paramInetAddress1, paramInt1, paramInetAddress2, paramInt2);}
}

?

  ServerSocketFactory是一個抽象類(抽象類不能被實例化,但可以有構造函數)。

  描述:This class creates server sockets./此類用于創建服務器套間字

 

public abstract class ServerSocketFactory {private static ServerSocketFactory theFactory;//返回環境默認的socket工廠副本
    public static ServerSocketFactory getDefault() {synchronized (ServerSocketFactory.class) {if (theFactory == null) {theFactory = new DefaultServerSocketFactory();}}return theFactory;}//返回一個未綁定的服務器套間字
    public ServerSocket createServerSocket() throws IOException {throw new SocketException("Unbound server sockets not implemented");}//返回一個綁定到指定端口的服務器套間字
    public abstract ServerSocket createServerSocket(int port) throws IOException;//返回一個綁定到指定端口的服務器套間字,并指定backlog參數
    public abstract ServerSocket createServerSocket(int port, int backlog) throws IOException;//返回一個綁定到指定端口的服務間套間字,并指定backlog和ip地址
    public abstract ServerSocket createServerSocket(int port, int backlog, InetAddress adress)throws IOException;
}                

  DefaultServerSocketFactory是ServerSocketFactory的子類。ServerSocket創建的默認工廠類。

class DefaultServerSocketFactory extends ServerSocketFactory {public ServerSocket createServerSocket() throws IOException {return new ServerSocket();}public ServerSocket createServerSocket(int paramInt) throws IOException {return new ServerSocket(paramInt);}public ServerSocket createServerSocket(int paramInt1, int paramInt2) throws IOException {return new ServerSocket(paramInt1, paramInt2);}public ServerSocket createServerSocket(int paramInt1, int paramInt2, InetAddress paramInetAddress)throws IOException {return new ServerSocket(paramInt1, paramInt2, paramInetAddress);}
}

  簡單測試

class TestFactory extends SocketFactory {@Overridepublic Socket createSocket(String paramString, int paramInt) throws IOException, UnknownHostException {return null;}@Overridepublic Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)throws IOException, UnknownHostException {return null;}@Overridepublic Socket createSocket(InetAddress paramInetAddress, int paramInt) throws IOException {return null;}@Overridepublic Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2,int paramInt2) throws IOException {return null;}public static void main(String[] args) throws UnknownHostException, IOException {TestFactory factory = new TestFactory();Socket socket1 = factory.createSocket("host", 1);System.out.println(socket1 == null);SocketFactory factory1 =  SocketFactory.getDefault();Socket socket2 = factory.createSocket("host", 1);System.out.println(socket2 == null);}
}

返回結果:

socketchannel、true

false

 

?

轉載于:https://www.cnblogs.com/maxudong/p/8385018.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/150097.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息