vbnet
复制代码
* 1.创建服务端 ****************************************
* 协议
Protocol := 'TCP4'
* 端口
Port := 5321
* 超时
Timeout := 1.0
* 打开TCP服务端
open_socket_accept (Port, ['protocol', 'timeout'], \
[Protocol,Timeout], AcceptingSocket)
* 2.循环接受客户端链接 ****************************************
tuple_regexp_match (Protocol, 'TCP', BaseProtocol)
if (BaseProtocol == 'TCP')
dev_error_var (Error, 1)
dev_set_check ('~give_error')
OpenStatus := 5
* 循环等待客户端链接
while (OpenStatus != 2)
socket_accept_connect (AcceptingSocket, 'auto', Socket)
OpenStatus := Error
endwhile
dev_set_check ('give_error')
* 设置参数--超时
set_socket_param (Socket, 'timeout', Timeout)
else
Socket := AcceptingSocket
endif
* 获取TCP连接的信息
get_socket_param (Socket, 'address_info', Address)
* 历史数据
data_hst := []
* 当前接受的数据
Answer := []
* 3.循环获取接收 客户端发送的数据 ****************************************
while (Answer != 'End')
* 接收数据
try
Format:=['z']
receive_data (Socket, Format, Answer, From)
catch (Exception)
endtry
* 数据 是否为空
recv_len := |Answer|
if(recv_len == 0)
continue
endif
* json_to_dict (Answer, [], [], DictHandle)
* 4.返回接受到的数据 ****************************************
if (From[1] == 0)
Data := '' + Answer[1]
else
Data := '' + Answer
endif
* 返回的IP地址与端口
To := [From[0],From[1]]
Format := 'z'
* 发送回复命令
send_data (Socket, Format, Data, To)
* 保存历史命令
data_hst := [data_hst, Answer]
Answer := []
endwhile