找回密码
 加入
搜索
查看: 2563|回复: 6

udp通信丢失汉字问题?

[复制链接]
发表于 2009-8-21 10:16:33 | 显示全部楼层 |阅读模式
最近开发一个通过udp通信的工具,但是发过去的汉字信息总是显示前4,5个字 其他的字都不显示了,不知道为什么?望高手解答~
发表于 2009-8-21 10:23:27 | 显示全部楼层
   先把源码帖出来.
 楼主| 发表于 2009-8-21 10:46:58 | 显示全部楼层
2# iftodo

实际上我用的就是最简单的例子:


; 启动 UDP 服务
;==============================================
UDPStartup()

; 打开 "套接字"
;==============================================
$socket = UDPOpen("127.0.0.1", 65532)
If @error <> 0 Then Exit

$n=0
While 1
    Sleep(2000)
    $n = $n + 1
    $status = UDPSend($socket, "消息计算机 #" & $n)
    If $status = 0 then
        MsgBox(0, "错误", "当发送 UDP 消息时发生错误: " & @error)
        Exit
    EndIf
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc




;;这是一个 UDP 服务器
;;首先启动这个

; 启动 UDP 服务
;==============================================
UDPStartup()

; 绑定一个套接字
;==============================================
$socket = UDPBind("127.0.0.1", 65532)
If @error <> 0 Then Exit

While 1
    $data = UDPRecv($socket, 50)
    If $data <> "" Then
        MsgBox(0, "UDP 数据", $data, 1)
    EndIf
    sleep(100)
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc
发表于 2009-8-21 11:21:25 | 显示全部楼层
转换成二进制发过来,再解码。这样就不会丢失字符了
http://www.autoitx.com/forum.php?mod=viewthread&tid=8818
 楼主| 发表于 2009-8-21 14:00:54 | 显示全部楼层
4# l4ever

感谢您的回答~!
我改成这样以后 还是 接收不全 您看看 是不是还有什么不妥的地方?


; 启动 UDP 服务
;==============================================
UDPStartup()

; 打开 "套接字"
;==============================================
$socket = UDPOpen("127.0.0.1", 65532)
If @error <> 0 Then Exit

$n=0
While 1
    Sleep(2000)
    $n = $n + 1
        $date = StringToBinary("消息测试看看好不好用",4)
    $status = UDPSend($socket, $date)
    If $status = 0 then
        MsgBox(0, "错误", "当发送 UDP 消息时发生错误: " & @error)
        Exit
    EndIf
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc




;;这是一个 UDP 服务器
;;首先启动这个

; 启动 UDP 服务
;==============================================
UDPStartup()

; 绑定一个套接字
;==============================================
$socket = UDPBind("127.0.0.1", 65532)
If @error <> 0 Then Exit

While 1
    $data = UDPRecv($socket, 5000)
    If $data <> "" Then
        MsgBox(0, "UDP 数据", BinaryToString($data,4), 1)
    EndIf
    sleep(100)
WEnd

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc
发表于 2009-8-21 19:36:50 | 显示全部楼层
本帖最后由 sensel 于 2009-8-21 19:41 编辑

$data = UDPRecv($socket, 5000)
改为:
$data = UDPRecv($socket, 5000, 1)
作用是强制UDPRecv返回二进制数据。

顺便说说,au3对包含汉字的字符串处理一直有问题,测试时需要注意一下。
 楼主| 发表于 2009-8-24 10:30:40 | 显示全部楼层
感谢各位高手的指点~~~~~~~~~~~~~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-22 21:30 , Processed in 0.093913 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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