UDP学习完了,学习TCP,使用 wsock.tco异步客户端练习,做了一个简单的测试,可以字符串,十六进制收发数据,发上来供初学者一起学习。
连接按钮事件,同时把 wsock.tcp.asynClient放在一起处理,没有使用 tcpClient.onReceive,不是很熟悉 tcpClient.onReceive 和 tcpClient.onRead有什么区别和优点。
慢慢学,共同进步哈
![](https://aar.chengxu.online/upload/files/20240626/1719389993.jpg)
import win.ui;
import fonts.fontAwesome;
import wsock.tcp.asynClient; //TCP客户端
import godking.message //godking信息框
/*DSG{{*/
var winform = win.form(text="TCP客户端示例";right=759;bottom=542)
winform.add(
btnConnect={cls="plus";text='\uF0C1 连接';left=413;top=11;right=550;bottom=41;bgcolor=5329231;border={radius=8};color=16777215;dl=1;dt=1;font=LOGFONT(h=-19;name='FontAwesome');iconStyle={align="left";font=LOGFONT(h=-20;name='FontAwesome');padding={left=10;top=2}};notify=1;tabstop=1;textPadding={right=-35};x=8;y=5;z=6};
btnSend={cls="plus";text="发送";left=656;top=479;right=744;bottom=509;bgcolor=5329231;border={radius=8};color=16777215;dl=1;dt=1;font=LOGFONT(h=-16;name='微软雅黑');iconStyle={align="left";font=LOGFONT(h=-21;name='FontAwesome');padding={left=10}};iconText='\uF124';notify=1;tabstop=1;textPadding={right=-35};x=8;y=5;z=8};
chkHex={cls="plus";text='\uF0C8 十六进制';left=30;top=450;right=197;bottom=481;align="left";dl=1;dt=1;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=9};
ioIP={cls="ipaddress";text="192.168.1.110";left=77;top=13;right=232;bottom=39;bgcolor=16777215;dl=1;dt=1;edge=1;multiline=1;z=3};
ioPort={cls="edit";text="8899";left=316;top=13;right=385;bottom=39;dl=1;dt=1;edge=1;multiline=1;z=2};
static={cls="static";text="设备IP:";left=16;top=15;right=90;bottom=37;dl=1;dt=1;transparent=1;z=4};
static2={cls="static";text="设备端口:";left=248;top=15;right=320;bottom=37;dl=1;dt=1;transparent=1;z=5};
txtLog={cls="richedit";left=8;top=50;right=749;bottom=397;bgcolor=1;db=1;dl=1;dr=1;edge=1;font=LOGFONT(h=-13;name='微软雅黑');multiline=1;readonly=1;vscroll=1;wrap=1;z=1};
txtSend={cls="edit";text="55 AA AA AA AA AA 00 01 2F D7 16";left=33;top=480;right=635;bottom=508;edge=1;multiline=1;z=7}
)
/*}}*/
var tcpClient = wsock.tcp.asynClient();
// godking信息框
var msg = godking.message()
msg.showOK = false
msg.showTitlebar =false;
msg.round = 12;
msg.borderWidth=2;
msg.borderColor = 0xFFFF0000;
msg.minWidth =500;
msg.btnBgColor =0xFFFF7F00
//显示调试信息
logview = function(data,color,bkcolor){
winform.txtLog.appendText( tostring(time(," [%H:%M:%S] ") ),data,'\r\n');
var line = winform.txtLog.lineCount-1;
var start = winform.txtLog.lineToChar(line);
winform.txtLog.setsel(start,-1);
winform.txtLog.setSelCharformat( textColor = color;backColor = bkcolor)
winform.txtLog.deselect();
winform.txtLog.lineScroll();
if (line>100) {
winform.txtLog.text="";
}
}
//双击清空调试信息
winform.txtLog.wndproc = function(hwnd,message,wParam,lParam){
if( message == 0x203/*_WM_LBUTTONDBLCLK*/ ){
winform.txtLog.text ="";
return true;
}
}
winform.chkHex.checked =true;
//连接事件,同时把 wsock.tcp.asynClient放在一起处理
winform.btnConnect.oncommand = function(id,event){
if winform.btnConnect.text="断开" {
logview("断开连接!",0x0000FF);
tcpClient.close();
return ;
}
winform.btnConnect.disabledText = {'\uF254';'\uF251';'\uF252';'\uF253';'\uF250';text='请稍候...'}
tcpClient = wsock.tcp.asynClient();
tcpClient.onClosed = function(err){
if(tcpClient == null || tcpClient.isClosed()){
//logview("in 断开",0xffffff);
winform.btnConnect.iconText = '\uF0C1'
winform.btnConnect.text ="连接"
return; }
}
//接收事件
tcpClient.onRead = function(err){
var remoteIP,remotePort = tcpClient.getRemoteIp();
var clientName = remoteIP+":"+remotePort
var allData = tcpClient.readAll()
if(winform.chkHex.checked){
logview("接收:"+clientName+">" + string.hex(allData," "),0xffffff);
}else{
logview("接收:"+clientName+">" + allData,0xffffff);
}
}
tcpClient.onError = function(err){
logview("ERROR:"+err,0x0000FF);
}
//客户端连接事件
tcpClient.onConnect = function(err){
if(tcpClient != null && tcpClient.isConnected()){
winform.btnConnect.disabledText = null;
winform.btnConnect.iconText = '\uF127'
winform.btnConnect.text = "断开"
logview(winform.ioIP.text+":"+winform.ioPort.text+" 连接成功",0x00FF00);
}else{
winform.btnConnect.disabledText =null
winform.btnConnect.iconText = '\uF0C1'
winform.btnConnect.text ="连接"
logview(winform.ioIP.text+":"+winform.ioPort.text+" 连接失败",0x0000FF);
}
}
tcpClient.connect(winform.ioIP.text,tonumber(winform.ioPort.text))
logview("开始连接:"+winform.ioIP.text+":"+winform.ioPort.text,0x00FF00);
}
//按钮前景色、背景色可以都改为图像路径
var skin = {
background={
active=0xFF4B739A;
default=0xFF4F5151;
hover=0xFF4B739A;
}
foreground = {
disabled = 0xFFCCCCCC;
};
color = {
};
border = {
active = {bottom=3;color=0xFF8FC31F;padding=8;radius=10;}
};
}
//发送
winform.btnSend.oncommand = function(id,event){
if(tcpClient == null || !tcpClient.isConnected()){
msg.err("请连接到服务器!",1000)
return;
}
if(winform.txtSend.text == null || winform.txtSend.text == ""){
msg.err("发送内容为空!",1000)
return;
}
//判断发送内容合法性
var strd = string.replace(winform.txtSend.text,"\s","");
var tt = string.find(strd,"[^0-9a-fA-F]");
if(tt != null){
msg.err("发送内容不是十六进制字符!",1000)
return;
}else {
if((#strd%2) != 0){ //HEX非偶数,不完整
msg.err("十六进制格式出错!",1000)
return;
}
}
var client = tcpClient;
var remoteIP,remotePort = client.getRemoteIp();
var clientname = remoteIP+":"+remotePort;
logview("发送:"+clientname+">" + winform.txtSend.text,0x00FF00);
if(winform.chkHex.checked){
client.send(string.unhex(winform.txtSend.text," ")); //去除\x
}else{
client.send(winform.txtSend.text)
}
}
import style;
winform.chkHex.skin(style.checkBox);
winform.btnConnect.skin(skin);
winform.show();
win.loopMessage();