找回密码
 加入
搜索
查看: 6854|回复: 14

[网络通信] 怎样使用TCP或者UDP协议发送数据到服务端【已解决】

  [复制链接]
发表于 2010-3-30 22:58:55 | 显示全部楼层 |阅读模式
本帖最后由 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 写入后的效果成了



谁能帮我改改好么这是我想要的效果

本帖子中包含更多资源

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

×
 楼主| 发表于 2010-3-30 23:51:31 | 显示全部楼层
期待神的出现!
发表于 2010-3-31 00:57:32 | 显示全部楼层
我一窍不通的飘过
 楼主| 发表于 2010-3-31 03:15:17 | 显示全部楼层
本帖最后由 261869247 于 2010-3-31 03:26 编辑

继续期待中 嘿嘿!
发表于 2010-3-31 06:58:28 | 显示全部楼层
继续期待中
发表于 2010-3-31 07:08:50 | 显示全部楼层
新手学习期待中!!
发表于 2010-3-31 07:56:42 | 显示全部楼层
UDPSend($socket,"" & $en & "" & $cn)
客户端发送时$en和$cn中间的“”加空格,这里改成:UDPSend($socket,"" & $en & " " & $cn)
发表于 2010-3-31 08:54:58 | 显示全部楼层
本帖最后由 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
上面的代码只改了三处,改动和增加的地方我都加了注释。

评分

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

查看全部评分

 楼主| 发表于 2010-3-31 10:46:52 | 显示全部楼层
非常感谢啊 谢谢了
正好忘记说了 输入中文名字和密码 无法完全接受数据!
 楼主| 发表于 2010-3-31 12:05:26 | 显示全部楼层
本帖最后由 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
 楼主| 发表于 2010-3-31 12:11:08 | 显示全部楼层
本帖最后由 261869247 于 2010-3-31 12:15 编辑

嘿嘿 解决了
不过还有其它好多问题
发表于 2010-3-31 16:16:19 | 显示全部楼层
呵呵,学习一下。。
发表于 2013-1-21 13:13:18 | 显示全部楼层
学习了啊,谢谢了.
发表于 2013-5-1 16:43:06 | 显示全部楼层
楼主这里有个问题,服务端有一个缺点。有些功能要完善,应该弄个按钮可以删除。或者自动替换
发表于 2013-6-22 17:07:37 | 显示全部楼层
回复 8# C.L


    大哥有个问题请教一下,有空请回复一下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-14 12:22 , Processed in 0.092520 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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