找回密码
 加入
搜索
查看: 8590|回复: 17

[已解决]UDP只能向接收端发送信息,服务端怎么回复?

 火.. [复制链接]
发表于 2009-12-17 23:50:19 | 显示全部楼层 |阅读模式
本帖最后由 永远活着 于 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                        

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +5 收起 理由
afan + 5 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 2009-12-18 09:14:27 | 显示全部楼层
看了一下代码,服务器端只有完整的接收代码,处理发送的代码不完整,客户端也只有发送的代码,接收的代码不完整

这里是你服务器端关于UDP的代码:
UDPStartup()
   $socket = UDPBind(@IPAddress1, 65532);====》
   $data = UDPRecv($socket, 1050,1)



    UDPCloseSocket($socket)
    UDPShutdown()
上面这段代码只能接收。
  $status = UDPSend($socket, $szData)
这里有一句发送的,前面没有UDPOpen语句,肯定不能发送。

客户端的代码反了过来,功能也不齐全

另外,服务器端的端口和客户端的端口都是同一个端口,建议使用不同的端口。
发表于 2009-12-18 09:56:20 | 显示全部楼层
帮你改了一下代码

服务器:
#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地址
发表于 2009-12-18 10:02:26 | 显示全部楼层
上个图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +20 贡献 +2 收起 理由
afan + 20 + 2

查看全部评分

 楼主| 发表于 2009-12-18 14:08:46 | 显示全部楼层
谢谢!马上测试一下!
发表于 2010-4-20 13:39:32 | 显示全部楼层
好同志呀。。。。。。。。。。。
发表于 2010-6-7 07:32:38 | 显示全部楼层
3#  的人不错。
发表于 2010-6-11 21:03:03 | 显示全部楼层
这样的话如果客户端处于内网的话能通讯成功吗?
而且端口能用80的吗?会不会有什么冲突的问题.
发表于 2010-7-9 05:38:41 | 显示全部楼层
看贴回贴是美德 ~~~
发表于 2010-7-10 12:25:23 | 显示全部楼层
恩不错,学习到东西了
发表于 2010-7-26 20:11:36 | 显示全部楼层
十分強大。
发表于 2010-10-12 03:56:25 | 显示全部楼层
留记号,要用时来取~~~~~~~
发表于 2010-10-14 10:00:02 | 显示全部楼层
学习了,感谢各位楼主。
发表于 2011-8-23 02:59:20 | 显示全部楼层
学习了,感谢各位楼主
发表于 2012-7-22 01:55:38 | 显示全部楼层
感谢3#
可以学怎么用UDP了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 09:32 , Processed in 0.121068 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表