UDP学习完了,学习TCP,使用 wsock.tco异步客户端练习,做了一个简单的测试,可以字符串,十六进制收发数据,发上来供初学者一起学习。
连接按钮事件,同时把 wsock.tcp.asynClient放在一起处理,没有使用 tcpClient.onReceive,不是很熟悉 tcpClient.onReceive 和 tcpClient.onRead有什么区别和优点。
慢慢学,共同进步哈

Code AardioLine:169复制
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166.167.168.169.import
win
.ui;import
fonts.fontAwesome;import
wsock.tcp.asynClient; import
godking.message 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(); 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 ){ - winform.txtLog.text =
""
; -
return
true
; - }
- }
- winform.chkHex.checked =
true
; - 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()){ -
- 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
(( - 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," "
)); - }
else
{ - client.send(winform.txtSend.text)
- }
- }
import
style;- winform.chkHex.skin(style.checkBox);
- winform.btnConnect.skin(skin);
- winform.show();
win
.loopMessage();