Socket Client

    技术2022-06-23  74

    Chapter9 Socket client

    Socket 会被client和server用到,它的构造函数如下:1 public Socket(String host, int port) throws UnknownHostException, IOException; 会尝试建立一个connect

    2 public Socket(InetAddress host, int port) throws IOException, 和上面那个差不多,不过不会扔出UnknownHostException,这个异常会在InetAddress建立过程中出现。

    3 public Socket(String host, int port, InetAddress interface, int localPort) throws IOException, UnknownHostException 从指定的localPort上连接一个Host,会尝试建立连接。

    4 public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException 和上面那个类似。

    5 public Socket( ) 制造一个还没有连接Host的socket,如果以后要连接,这个调用 connect(SocketAddress)来实现。

    6 protected Socket(SocketImpl impl)

    7 public Socket(Proxy proxy) // Java 1.5 这个构造函数的意思就是,从本机上发出的连接,经过一个Proxy,然后再到达指定的Host。 下面是一个用法: SocetAddress proxyAddress = new InetSocketAddress("myproxy.example.com", 1080);

     Proxy proxy = new Proxy(Proxy.Type.SOCKS,  proxyAddress)

     Socket s = new Socket(proxy);

     SocketAddress remote = new InetSocketAddress("login.ibiblio.org", 25);

     s.connect(remote);//通过代理发起连接 如果不用这种方法,代理也可以设置。设置 socksProxyHost and socksProxyPort system properties, 不过这个设置会对所有的Socket起作用。

    8 Socket 用一个SocketImpl 字段,其中SocketImpl 主要用来实现 native code.

    9 public InetAddress getInetAddress( )  用来得到Socket所连接的 remote host. (相似的方法有:int getPort())

    10 public int getLocalPort( )  一个Socket分为两端,remote host 端的host是已知的,而local host 是操作系统分配的。当local传输数据时,会把自己的port信息附带的发给remote host,这样  remote就

    知道如何回复local了。

    11  public InetAddress getLocalAddress( ) 在有多个网卡时可能会用到。

    12 public InputStream getInputStream( ) throws IOException 从remote host读数据

    13 public OutputStream getOutputStream( ) throws IOException 向remote host写数据

    14 Setting Socket Options Socket Options 包括好几个,他们是: TCP_NODELAY (Setting TCP_NODELAY to true ensures that packets are sent as quickly as possible regardless of their size,即可以不buffer)

     SO_BINDADDR

     SO_TIMEOUT (当socket在read的时候它可能会block, 如果设置timeout, 则过了这个时间就会抛出InterruptedIOException)

     SO_LINGER (在socket关闭前,剩余数据的传输时间)

     SO_SNDBUF (Java 1.2 and later)

     SO_RCVBUF (Java 1.2 and later)

     SO_KEEPALIVE (Java 1.3 and later)

     OOBINLINE (Java 1.4 and later)

    15 Class of Service public int getTrafficClass( ) throws SocketException public void setTrafficClass(int trafficClass) throws SocketException 只有以下四种Service  0x02: Low cost

     0x04: High reliability

     0x08: Maximum throughput

     0x10: Minimum delay

    16 Socket Exceptions Most methods of the Socket class are declared to throw IOException or its subclass. Such as  public class SocketException extends IOException public class BindException extends SocketException public class ConnectException extends SocketException public class NoRouteToHostException extends SocketException

     这些异常类的说明如下: A BindException is thrown if you try to construct a Socket or ServerSocket object on a local port that is in use or that you do not have sufficient privileges to use. A  

     ConnectException is thrown when a connection is refused at the remote host, which usually happens because the host is busy or no process is listening on that port.   

     Finally, a NoRouteToHostException indicates that the connection has timed out

    17 Socket Addresses 代表远端的一个地址,包括InetAddress 和 port, 主要用在 Socket.connect(SocketAddress a) 中、


    最新回复(0)