[已解决]UDP只能向接收端发送信息,服务端怎么回复?
本帖最后由 永远活着 于 2009-12-18 14:09 编辑看了下论坛,基本都是用的TCP,UDP能像TCP一样么?UDP方面太少资料了,都建议用TCP
但有朋友说,聊天用UDP传文件之类用TCP好些
服务端
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$ip=@IPAddress1
UDPStartup()
$socket = UDPBind(@IPAddress1, 65532)
If @error <> 0 Then Exit
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("接收端", 291, 208, 192, 114)
$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
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button2
Exit
EndSwitch
WEnd
Func _ext()
UDPCloseSocket($socket)
UDPShutdown()
EndFunc
Func _send()
$lr= @IPAddress1&">"&GUICtrlRead($Edit2)
$bj="已发送消息:"&GUICtrlRead($Edit2)
$szData = StringToBinary($lr, 4)
$status = UDPSend($socket, $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>
$ip=@IPAddress1
UDPStartup()
$socket = UDPOpen($ip, 65532)
If @error <> 0 Then Exit
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("发送端", 291, 208, 192, 114)
$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)
$Button2 = GUICtrlCreateButton("退出[&Esc]", 168, 176, 113, 25, 0)
GUICtrlSetOnEvent(-1, "_ext")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
;$lr= @ComputerName&">"&GUICtrlRead($Edit2)
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
case $Button1
_send()
Case $Button2
Exit
EndSwitch
WEnd
Func _ext()
TCPCloseSocket($Socket)
TCPShutdown ( )
Exit
EndFunc
Func _send()
$lr= @IPAddress1&">"&GUICtrlRead($Edit2)
$bj="已发送消息:"&GUICtrlRead($Edit2)
$szData = StringToBinary($lr, 4)
$status = UDPSend($socket, $szData)
GUICtrlSetData($edit1, $bj& @CRLF &GUICtrlRead($edit1))
GUICtrlSetData($Edit2, "")
If $status = 0 then
MsgBox(16, "错误", "请输入消息再发送")
EndIf
EndFunc
看了一下代码,服务器端只有完整的接收代码,处理发送的代码不完整,客户端也只有发送的代码,接收的代码不完整
这里是你服务器端关于UDP的代码:
UDPStartup()
$socket = UDPBind(@IPAddress1, 65532);====》
$data = UDPRecv($socket, 1050,1)
UDPCloseSocket($socket)
UDPShutdown()
上面这段代码只能接收。
$status = UDPSend($socket, $szData)
这里有一句发送的,前面没有UDPOpen语句,肯定不能发送。
客户端的代码反了过来,功能也不齐全
另外,服务器端的端口和客户端的端口都是同一个端口,建议使用不同的端口。 帮你改了一下代码
服务器:
#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地址 上个图:
谢谢!马上测试一下! 好同志呀。。。。。。。。。。。 3#的人不错。 这样的话如果客户端处于内网的话能通讯成功吗?
而且端口能用80的吗?会不会有什么冲突的问题. 看贴回贴是美德 ~~~ 恩不错,学习到东西了{:face (411):} 十分強大。 留记号,要用时来取~~~~~~~ 学习了,感谢各位楼主。 学习了,感谢各位楼主 感谢3#
可以学怎么用UDP了
页:
[1]
2