import win.ui;
import wsock.tcp.asynServer;
import wsock.tcp.asynClient;
import console;
import win.timer;
/*DSG{{*/
mainForm = win.form(text="服务端";right=582;bottom=229)
mainForm.add(
button={cls="button";text="暂 停";left=423;top=167;right=528;bottom=199;dl=1;dt=1;font=LOGFONT(h=-14);tabstop=1;z=4};
button2={cls="button";text="开 始";left=423;top=113;right=531;bottom=144;dl=1;dt=1;font=LOGFONT(h=-14);tabstop=1;z=3};
edit={cls="edit";text="Edit";left=30;top=13;right=388;bottom=199;edge=1;multiline=1;z=8};
editSrvPort={cls="edit";left=472;top=50;right=521;bottom=74;dl=1;dt=1;edge=1;font=LOGFONT(h=-14);multiline=1;tabstop=1;z=2};
editserIp={cls="edit";left=473;top=12;right=552;bottom=36;dl=1;dt=1;edge=1;font=LOGFONT(h=-14);multiline=1;tabstop=1;z=1};
static={cls="static";text="端 口:";left=414;top=51;right=472;bottom=81;dl=1;dt=1;font=LOGFONT(h=-14);notify=1;transparent=1;z=5};
static1={cls="static";text="监听IP:";left=411;top=16;right=472;bottom=44;dl=1;dt=1;font=LOGFONT(h=-14);notify=1;transparent=1;z=6};
static10={cls="static";text='\u2B1B';left=533;top=52;right=558;bottom=81;color=255;dl=1;dt=1;font=LOGFONT(h=-14);notify=1;transparent=1;z=7}
)
/*}}*/
io.open(,"服务端v1.01");
serIp = "0.0.0.0"
serPort = 7806
mainForm.editserIp.text = serIp
mainForm.editSrvPort.text = tostring(serPort)
tcpServer = wsock.tcp.asynServer()
//启动服务端监听端口
tcpServerStart = function(...){
serIp = mainForm.editserIp.text
serPort = tonumber(mainForm.editSrvPort.text)
tcpServer.start(serIp,serPort)
if(tcpServer.getLocalIp() != null){
console.log("服务端已启动" , tcpServer.getLocalIp())
mainForm.static10.color = 0x00ee00
mainForm.static10.text = " ▶"
mainForm.editserIp.readonly = true
mainForm.editSrvPort.readonly = true
mainForm.button2.disabled = true
return 1;
}else {
mainForm.button.disabled = true
console.log("服务端启动失败,请检查端口是否被占用。" )
return 0;
}
}
//开始
Start = function(){
console.log("服务端开始")
if (tcpServerStart()){
mainForm.button2.disabled = true;
mainForm.button.disabled = false;
}
}
//暂停
Pause = function(){
console.log("服务端暂停")
tcpServer.stop()
//
mainForm.static10.color = 0x0000ff
mainForm.static10.text = "⬛"
//
mainForm.editserIp.readonly = false
mainForm.editSrvPort.readonly = false
mainForm.button2.disabled = false
mainForm.button.disabled = true
}
//收到信息处理
tcpServer.onRead = function(hSocket,err){
var client = tcpServer.client(hSocket);
message = client.readAll()
console.log("收到信息:" ++ message)
client = tcpServer.client(hSocket)
//回复数据
sendMessage = "回复数据:" ++ message
console.log("收到信息:" ++ message)
client.send(sendMessage,#sendMessage)
}
//连接关闭
tcpServer.onClose = function(hSocket,err){
var client = tcpServer.client(hSocket);
console.log("客户端关闭连接:",client.getRemoteIp())
client.close()
}
//开始
mainForm.button2.oncommand = function(id,event){
Start();
}
//暂停
mainForm.button.oncommand = function(id,event){
Pause();
}
//
Start();
//
mainForm.show();
return win.loopMessage();
import win.ui;
import wsock.tcp.client;
import wsock.tcp.asynClient;
import console;
/*DSG{{*/
mainForm = win.form(text="客户端";right=348;bottom=241;max=false)
mainForm.add()
/*}}*/
io.open()
srvIp = "127.0.0.1"
srvPort = 7806
x = 1
tcpClient = wsock.tcp.asynClient();
tcpClient.connect(srvIp,srvPort)
//重新连接服务器
tcpConnToServer = function(srvIp,srvPort){
console.log("重新连接服务器。")
tcpClient = wsock.tcp.asynClient();
tcpClient.connect(srvIp,srvPort)
}
//收到消息
tcpClient.onReceive = function(err){
var readMsg = tcpClient.readAll()
console.log("收到指令1:",readMsg)
}
//对方关闭连接
tcpClient.onClose = function(err){
tcpClient.close()
console.log('连接关闭:',err)
}
//上传数据
upload = function(x){
msg = tostring(x) ++ "|"
if(tcpClient.isConnected()){
console.log('tcp连接正常')
tcpClient.send(msg,#msg);
}else {
console.log('tcp未连接')
tcpConnToServer(srvIp,srvPort)
}
}
//定时器
mainForm.setInterval(
function(){
upload(x)
x++
// 定时执行参数 @1 指定的回调函数,参数 @2 指定间隔毫秒数
},1000
);
mainForm.show();
return win.loopMessage();
见以上程序,如果先运行服务端,再运行客户端,服务端收到客户端消息之后回复一个新消息,此时客户端能正常接收到服务器的回复消息。
但假如服务端暂停或关闭之后重新打开,客户端会自动重连,重连后,服务端能正常接收客户端发来的消息,但服务端回复给客户端的消息客户端接收不到了。
请教一下各位大神如何修改。