Socket for server(ServerSocket)

    技术2022-05-19  21

    1 The constructor public ServerSocket(int port) throws BindException, IOException public ServerSocket(int port, int queueLength) throws BindException, IOException public ServerSocket(int port, int queueLength, InetAddress bindAddress) throws IOException 这些构造函数从参数上都能看出如何用,其中一些需要注意。如果 port设置成0, 则系统会随机的分配一个端口号;

    queueLength的设置如果超过OS的最大值,则自动设为OS的最大值。

    2 public ServerSocket( ) throws IOException  主要用来设置ServerSocketr选项,然后再连接。它可以随后bind,如下: public void bind(SocketAddress endpoint) throws IOException // Java 1.4 public void bind(SocketAddress endpoint, int queueLength) throws IOException 3 Socket accept() 会一直阻塞,直到有一个client请求连接,此方法返回一个Socket,client和server就可以交流了。这个交流过程的时间是

    不确定的,所以一般服务器需要用多个进程来处理client的请求。

    4 public void close( ) throws IOException


    最新回复(0)