标签:
3.1 构造ServerSocket
ServerSocket的构造方法如下:
1 ServerSocket() 2 //Creates an unbound server socket. 3 4 ServerSocket(int port) 5 //Creates a server socket, bound to the specified port. 6 7 ServerSocket(int port, int backlog) 8 //Creates a server socket and binds it to the specified local port number, with the 9 //specified backlog. 10 11 ServerSocket(int port, int backlog, InetAddress bindAddr) 12 //Create a server with the specified port, listen backlog, and local IP address to 13 //bind to.
参数port指定服务器需要绑定的端口(监听端口),参数backlog指定客户连接请求队列的长度,参数bindAddr指定服务器需要绑定的IP地址。
参数port设为0时,由系统自动分配端口。
当队列中的连接请求达到了队列的最大容量时,服务器进程所在的主机会拒绝新的连接请求。当服务器通过ServerSocket.accept()方法从队列取出连接请求时,队列腾出空位,新的连接请求加入队列。
对于客户机进程,如果他发出的连接请求被加入到服务器队列,则说明连接成功,客户机Socket正常返回,否则抛出ConnectionException异常。
标签:
原文地址:http://www.cnblogs.com/wuchaodzxx/p/5517677.html