怎样使用TCP或者UDP协议发送数据到服务端【已解决】
本帖最后由 261869247 于 2010-3-31 17:05 编辑请教下往服务端发送数据
用TCP协议还是 UDP 协议好
刚研究了下借用论坛网友的代码
服务端#include <GUIConstants.au3>
HotKeySet("!q","CLOSEClicked")
hotkeyset("!s","startbtn")
dim $socket = -1,$PS = True
$mainwindow = GUICreate("基于UDP协议的局域网通信工具--服务端", 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, 50)
If $data <> "" Then
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
客户端#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 311, 133, 303, 184)
$Input1 = GUICtrlCreateInput("", 80, 24, 177, 21)
$Input2 = GUICtrlCreateInput("", 80, 64, 177, 21)
$Button1 = GUICtrlCreateButton("账号", 8, 24, 65, 25)
$Button2 = GUICtrlCreateButton("密码", 8, 64, 65, 25)
$Button3 = GUICtrlCreateButton("注册", 96, 96, 73, 25)
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 $Button3
$EN = GUICtrlRead($Input1)
$CN = GUICtrlRead($Input2)
UDPSend($socket,"" & $en & "" & $cn)
EndSwitch
WEnd
可是输入账号 user 密码 password 写入后的效果成了
谁能帮我改改好么这是我想要的效果
期待神的出现! 我一窍不通的飘过 本帖最后由 261869247 于 2010-3-31 03:26 编辑
继续期待中 嘿嘿! 继续期待中 新手学习期待中!! UDPSend($socket,"" & $en & "" & $cn)
客户端发送时$en和$cn中间的“”加空格,这里改成:UDPSend($socket,"" & $en & " " & $cn) 本帖最后由 C.L 于 2010-3-31 09:00 编辑
LZ的这段代码,如果稍稍改动就可以支持中文名称和中文密码,无聊中,我帮你改了一下,呵呵
服务端代码:
#include <GUIConstants.au3>
HotKeySet("!q","CLOSEClicked")
hotkeyset("!s","startbtn")
dim $socket = -1,$PS = True
$mainwindow = GUICreate("基于UDP协议的局域网通信工具--服务端", 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
客户端:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 311, 133, 303, 184)
$Input1 = GUICtrlCreateInput("", 80, 24, 177, 21)
$Input2 = GUICtrlCreateInput("", 80, 64, 177, 21)
$Button1 = GUICtrlCreateButton("账号", 8, 24, 65, 25)
$Button2 = GUICtrlCreateButton("密码", 8, 64, 65, 25)
$Button3 = GUICtrlCreateButton("注册", 96, 96, 73, 25)
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 $Button3
$EN = GUICtrlRead($Input1)
$CN = GUICtrlRead($Input2)
UDPSend($socket,StringToBinary ("" & $en & "" & $cn,4));转换并发送二进制数据(支持中文全显示)
EndSwitch
WEnd
上面的代码只改了三处,改动和增加的地方我都加了注释。 非常感谢啊 谢谢了
正好忘记说了 输入中文名字和密码 无法完全接受数据! 本帖最后由 261869247 于 2010-3-31 12:14 编辑
请问怎么同时判断多个输入框有数据
我这个要是一个输入框有数据就提示注册成功#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 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("192.168.0.236", 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)
If $EN = 0 then
MsgBox(16, "错误", "请输入完整你的注册信息")
ElseIf $CN = 0 then
MsgBox(16, "错误", "请输入完整你的注册信息")
ElseIf $Pass = 0 then
MsgBox(16, "错误", "请输入完整你的注册信息")
ElseIf $QQ = 0 then
MsgBox(16, "错误", "请输入完整你的注册信息")
else
UDPSend($socket,StringToBinary ("" & $en & " " & "" & $cn,4));转换并发送二进制数据(支持中文全显示)
MsgBox(0,"服务器提示","注册成功")
EndIf
EndSwitch
WEnd 本帖最后由 261869247 于 2010-3-31 12:15 编辑
嘿嘿 解决了
不过还有其它好多问题 呵呵,学习一下。。 学习了啊,谢谢了. 楼主这里有个问题,服务端有一个缺点。有些功能要完善,应该弄个按钮可以删除。或者自动替换 回复 8# C.L
大哥有个问题请教一下,有空请回复一下
页:
[1]