找回密码
 加入
搜索
查看: 3786|回复: 8

[网络通信] 关于UDP获取数据问题【已解决】

[复制链接]
发表于 2010-3-31 17:05:43 | 显示全部楼层 |阅读模式
本帖最后由 261869247 于 2010-4-2 13:02 编辑

刚才和你聊的问题

客户端
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("CS账号注册系统客户端", 319, 186, 350, 205)
$Button1 = GUICtrlCreateButton("CS英文ID", 24, 8, 81, 25)
$Button2 = GUICtrlCreateButton("CS中文ID", 24, 40, 81, 25)
$Button3 = GUICtrlCreateButton("你的密码", 24, 72, 81, 25)
$Button4 = GUICtrlCreateButton("确定注册", 72, 144, 81, 25)
$Input1 = GUICtrlCreateInput("", 120, 8, 121, 21)
$Input2 = GUICtrlCreateInput("", 120, 40, 121, 21)
$Input3 = GUICtrlCreateInput("", 120, 72, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$Button5 = GUICtrlCreateButton("你的QQ", 24, 104, 81, 25)
$Input4 = GUICtrlCreateInput("", 120, 104, 121, 21)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

UDPStartup()
$socket = UDPOpen("127.0.0.1", 64213)

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
case $Button4
$EN = GUICtrlRead($Input1)
$CN = GUICtrlRead($Input2)
$Pass = GUICtrlRead($Input3)
$QQ = GUICtrlRead($Input4)
 
        UDPSend($socket,StringToBinary ('"' & "" & $en & '" "' & "" & $cn & '" "' & "" & $Pass & '" "' & "" & $QQ & '"',4));转换并发送二进制数据(支持中文全显示)
        MsgBox(0,"服务器提示","注册成功")

EndSwitch
WEnd
服务端
#include <GUIConstants.au3>
HotKeySet("!q","CLOSEClicked")
hotkeyset("!s","startbtn")
dim $socket = -1,$PS = True
$mainwindow = GUICreate("阳光网吧专用CS账号中文ID自助注册系统", 481, 440, 193, 121)
$text1 = GUICtrlCreateEdit("", 12, 12, 457, 384,0x0844 + 0x0008)
$startBtn = GUICtrlCreateButton("启动服务(ALT+&S)", 340, 405, 130, 25)
guictrlcreatelabel("启动服务 ALT+S,暂停服务 ALT+P,退出程序 ALT+Q",10,410)
GUISetState(@SW_SHOW)

pause()

Func pause()
if $socket <> -1 then UDPCloseSocket($socket)

While 1
$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
  CLOSEClicked()
Case $msg = $startBtn
  startBtn()
EndSelect
WEnd

EndFunc

Func startbtn()
if $ps = True then
guictrlsetdata($startbtn,"暂停服务(ALT+&P)")
HotKeySet("!p","startbtn")
HotKeySet("!s")
$ps = not $ps
UDPs()
else
guictrlsetdata($startbtn,"启动服务(ALT+&S)")
$ps = not $ps

HotKeySet("!s","startbtn")
HotKeySet("!p")

pause()
endif
EndFunc

Func udps()
UDPStartup()
$socket = UDPBind("127.0.0.1", 64213)
If @error <> 0 Then Exit
While 1
    $data = UDPRecv($socket, 1024,1);改动:接收数据调大一点,可以接收多一点数据,后面的1是强制以二进制方式接收

    If $data <> "" Then
    $data = BinaryToString ($data,4);增加:将二进制数据转换成字符
        GUICtrlSetData($text1,$data & @CRLF & guictrlread($text1))
        $file = FileOpen("reg.ini", 1)
        FileWriteLine($file,$data)
    EndIf

$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
  CLOSEClicked()
Case $msg = $startBtn
  startBtn()
EndSelect
    sleep(50)
WEnd
EndFunc

Func CLOSEClicked()
$ynexit = MsgBox(262196, "提示", "确定退出本程序吗?")
if $ynexit = 6 then 
  if $socket <> -1 then
   UDPCloseSocket($socket)
   UDPShutdown()
  EndIf
  Exit
EndIf
EndFunc
点了注册后 会按照客户端 固定的格式 发送到服务端
比如输入 玩家英文ID user
                   中文ID 玩家
          你的密码 123
                  你的QQ 123
那么服务端获取数据并写入 123.ini  后的格式如下
"user" "玩家" "123" "123"

现在的问题是能把获取的数据分别写入多个cfg 文件吗? 比如
"user" "玩家"  这部分数据写入 1.cfg
"123" "123"这部分数据写入 2.cfg
发表于 2010-3-31 17:06:14 | 显示全部楼层
这样不行,会被锁帖……
发表于 2010-3-31 17:28:30 | 显示全部楼层
应该只需要修改第57行FileWriteLine($file,$data)是吧?
数据就是$data?
$data 字符串是什么?
 楼主| 发表于 2010-3-31 17:43:29 | 显示全部楼层
我也晕 嘿嘿!
 楼主| 发表于 2010-3-31 17:52:53 | 显示全部楼层
$data 返回的

本帖子中包含更多资源

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

×
发表于 2010-4-1 07:53:52 | 显示全部楼层
现在的问题是能把获取的数据分别写入多个cfg 文件吗? 比如
"user" "玩家"  这部分数据写入 1.cfg
"123" "123"这部分数据写入 2.cfg


可以啊,不过这样分开多个cfg文件有什么意义吗?

用标准的ini格式的配置文件不行吗
如:
123.ini
[config]
id=user
cnid=玩家
qq=123
password=123

这样做更好处理数据吧.
发表于 2010-4-1 08:40:19 | 显示全部楼层
按你的要求

$file = FileOpen("reg.ini", 1)

FileWriteLine($file,$data)

这两行代码改成:
$aSplit = StringSplit ($data," ")
If UBound($aSplit)>3 Then
        FileWriteLine ("1.cfg",$aSplit[1]&" "&$aSplit[2])
        FileWriteLine ("2.cfg",$aSplit[3]&" "&$aSplit[4])
EndIf
ini版:
同样的也是将上面的这两行代码换成:
$aSplit = StringSplit ($data," ")
If UBound($aSplit)>3 Then
        IniWrite ("123.ini","config","id",$aSplit[1])
        IniWrite ("123.ini","config","cnid",$aSplit[2])
        IniWrite ("123.ini","config","password",$aSplit[3])
        IniWrite ("123.ini","config","qq",$aSplit[4])
EndIf

评分

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

查看全部评分

 楼主| 发表于 2010-4-2 01:46:27 | 显示全部楼层
非常感谢了 学习了
 楼主| 发表于 2010-4-2 02:07:45 | 显示全部楼层
再次感谢你 呵呵 在论坛学到了不少东西!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-10 23:35 , Processed in 0.094067 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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