# wsock.tcp.server 库模块帮助文档

## wsock.tcp.server 成员列表 <a id="wsock.tcp.server" href="#wsock.tcp.server">&#x23;</a>

TCP服务端

### wsock.tcp.server.closeById(serverId) <a id="wsock.tcp.server.closeById" href="#wsock.tcp.server.closeById">&#x23;</a>
参数指定wsock.tcp.server对象的ID属性  
关闭指定ID的服务器

### wsock.tcp.server.getFreePort(ip,...) <a id="wsock.tcp.server.getFreePort" href="#wsock.tcp.server.getFreePort">&#x23;</a>
获取空闲 TCP 服务端口，  
IP参数需要与实际创建服务器使用的 IP 参数完全一致，  
可选在参数中使用数值数组或任意个数值参数指定优先测试的端口号，  
如果没有找到参数指定的空闲凋口,则自动分配空闲端口，  
动态分配空闲端口为49152到65535之间的值，  
XP系统则为 1025到5000之间的值，  
命令行输入 netsh int ipv4 show dynamicport tcp 看动态端口范围，  
安装 Hyper-V 会导致动态起始端口变为 1024 导致常用端口冲突

### wsock.tcp.server.isFreePort(port,ip) <a id="wsock.tcp.server.isFreePort" href="#wsock.tcp.server.isFreePort">&#x23;</a>
检查参数指定的端口号是否空闲  
如果不写IP，则默认设为"0.0.0.0"

## WsockTcpServerObject 成员列表 <a id="WsockTcpServerObject" href="#WsockTcpServerObject">&#x23;</a>

### WsockTcpServerObject._serverAddress <a id="WsockTcpServerObject._serverAddress" href="#WsockTcpServerObject._serverAddress">&#x23;</a>
服务端监听地址  

[返回对象:sockaddrInObject](#sockaddrInObject)

### WsockTcpServerObject.beforeClose() <a id="WsockTcpServerObject.beforeClose" href="#WsockTcpServerObject.beforeClose">&#x23;</a>

```aardio
WsockTcpServerObject.beforeClose = function(){
	/*服务器关闭前调用此函数*/
}
```

### WsockTcpServerObject.bind <a id="WsockTcpServerObject.bind" href="#WsockTcpServerObject.bind">&#x23;</a>
绑定 IP 端口。  
此函数适用于创建 UDP 服务端。  
创建 UDP 客户端一般不必要调用此函数。

### WsockTcpServerObject.bind(IP,port) <a id="WsockTcpServerObject.bind" href="#WsockTcpServerObject.bind">&#x23;</a>
绑定 IP 端口。  
如果在 wsock.tcp.server 构造参数 @1 为 false 则可以在这里指定。  
如果不指定 @IP 则默认设为"0.0.0.0"。  

参数 @port 可指定数值或数组，指定数组则尝试连接数组中的所有端口。  

指定单个端口时成功返回 true，端口参数为数组时返回**连接成功的实际端口**。  
失败返回 null,错误信息,错误代码。  
相同的本地 IP 与端口重复绑定会返回 10022（_WSAEINVAL） 错误。  
绑定成功后不能再重复调用此函数。

### WsockTcpServerObject.close() <a id="WsockTcpServerObject.close" href="#WsockTcpServerObject.close">&#x23;</a>
关闭 TCP 服务端

### WsockTcpServerObject.forever(回调函数) <a id="WsockTcpServerObject.forever" href="#WsockTcpServerObject.forever">&#x23;</a>

```aardio
WsockTcpServerObject.forever(  
	function(acceptSocket){  
		var request = wsock.tcp.client(,acceptSocket)  
	}  
)
```

### WsockTcpServerObject.getLocalIp() <a id="WsockTcpServerObject.getLocalIp" href="#WsockTcpServerObject.getLocalIp">&#x23;</a>
返回当前绑定的本地 IP,端口号

### WsockTcpServerObject.getLocalPort() <a id="WsockTcpServerObject.getLocalPort" href="#WsockTcpServerObject.getLocalPort">&#x23;</a>
返回当前绑定的本地端口号

### WsockTcpServerObject.id <a id="WsockTcpServerObject.id" href="#WsockTcpServerObject.id">&#x23;</a>
服务器唯一ID,字符串

### WsockTcpServerObject.listen(请求队列大小) <a id="WsockTcpServerObject.listen" href="#WsockTcpServerObject.listen">&#x23;</a>
监听绑定的 IP 端口

## wsock.tcp 成员列表 <a id="wsock.tcp" href="#wsock.tcp">&#x23;</a>

### wsock.tcp.server() <a id="wsock.tcp.server" href="#wsock.tcp.server">&#x23;</a>
[返回对象:WsockTcpServerObject](#WsockTcpServerObject)

### wsock.tcp.server(IP,端口,请求队列大小) <a id="wsock.tcp.server" href="#wsock.tcp.server">&#x23;</a>
创建并启动 TCP 服务端，所有参数可选,  

如果不写 IP，则默认设为"0.0.0.0"也即监听本机所有IP（访问此服务端不限制 IP）。  
限制仅本机可以访问建议写 127.0.0.1  

端口为 0 或省略则自动查找 1025 以后的空闲端口  
注意0-1023为系统通用服务保留端口,  
1024-49151 为用户服务端口,其中大约 %9 已由 IANA 注册分配  
49152-65535 为私有或动态分配的临时端口

### wsock.tcp.server(false) <a id="wsock.tcp.server" href="#wsock.tcp.server">&#x23;</a>
仅创建服务端。  
后面可以调用 bind 访法绑定 IP 与端口，调用 listen 方法启动监听。
