帮你改了一下代码
服务器:#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
$ip=@IPAddress1
UDPStartup()
$socket = UDPBind(@IPAddress1, 65532)
If @error <> 0 Then Exit
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("发送端", 291, 208, 192, 114)
GUISetOnEvent($GUI_EVENT_CLOSE, "_ext")
$Edit1 = GUICtrlCreateEdit("", 8, 28, 273, 116, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_CLIPSIBLINGS,$ES_READONLY,$ws_VSCROLL))
$Edit2 = GUICtrlCreateEdit("", 8, 148, 273, 20, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_CLIPSIBLINGS))
GUICtrlSetData(-1, "")
$Button1 = GUICtrlCreateButton("发送[&Enter]", 8, 176, 113, 25, 0)
GUICtrlSetOnEvent(-1, "_send")
$Button2 = GUICtrlCreateButton("退出[&Esc]", 168, 176, 113, 25, 0)
GUICtrlSetOnEvent(-1, "_ext")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$data = UDPRecv($socket, 1050,1)
;If $data <> "" Then GUICtrlSetData($Edit1, GUICtrlRead($Edit1)& @CRLF & $data )
;Sleep(100)
If $data <> "" Then
$data = BinaryToString($data,4)
GUICtrlSetData($edit1, $data & @CRLF & GUICtrlRead($edit1))
EndIf
WEnd
Func _ext()
UDPCloseSocket($socket)
UDPShutdown()
Exit
EndFunc
Func _send()
$lr= GUICtrlRead($Edit2)
$bj="已发送消息:"&GUICtrlRead($Edit2)
$szData = StringToBinary($lr, 4)
$connect = UDPOpen(@IPAddress1,65531);这里的IP地址,必须是对方的IP地址和端口,这里用@ipaddress1是用作本机测试
$status = UDPSend($connect, $szData)
GUICtrlSetData($edit1, $bj& @CRLF &GUICtrlRead($edit1))
GUICtrlSetData($Edit2, "")
If $status = 0 then
MsgBox(16, "错误", "请输入消息再发送")
EndIf
EndFunc
客户端:#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
$ip=@IPAddress1
UDPStartup()
$socket = UDPBind(@IPAddress1, 65531)
If @error <> 0 Then Exit
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("接收端", 291, 208, 192, 114)
GUISetOnEvent($GUI_EVENT_CLOSE, "_ext")
$Edit1 = GUICtrlCreateEdit("", 8, 28, 273, 116, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_CLIPSIBLINGS,$ES_READONLY,$ws_VSCROLL))
$Edit2 = GUICtrlCreateEdit("", 8, 148, 273, 20, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_CLIPSIBLINGS))
GUICtrlSetData(-1, "")
$Button1 = GUICtrlCreateButton("发送[&Enter]", 8, 176, 113, 25, 0)
GUICtrlSetOnEvent(-1, "_send")
$Button2 = GUICtrlCreateButton("退出[&Esc]", 168, 176, 113, 25, 0)
GUICtrlSetOnEvent(-1, "_ext")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$data = UDPRecv($socket, 1050,1)
;If $data <> "" Then GUICtrlSetData($Edit1, GUICtrlRead($Edit1)& @CRLF & $data )
;Sleep(100)
If $data <> "" Then
$data = BinaryToString($data,4)
GUICtrlSetData($edit1, $data & @CRLF & GUICtrlRead($edit1))
EndIf
WEnd
Func _ext()
UDPCloseSocket($socket)
UDPShutdown()
Exit
EndFunc
Func _send()
$lr= GUICtrlRead($Edit2)
$bj="已发送消息:"&GUICtrlRead($Edit2)
$szData = StringToBinary($lr, 4)
$connect = UDPOpen(@IPAddress1,65532);这里的IP地址,必须是对方的IP地址和端口,这里用@ipaddress1是用作本机测试
$status = UDPSend($connect, $szData)
GUICtrlSetData($edit1, $bj& @CRLF &GUICtrlRead($edit1))
GUICtrlSetData($Edit2, "")
If $status = 0 then
MsgBox(16, "错误", "请输入消息再发送")
EndIf
EndFunc
本机测试正常,但如果不同IP地址的通迅,应相应更改UDPOpen(@IPAddress1,65532)处的IP地址 |