zjg2003 发表于 2009-3-16 15:01:10

为什么不能接收信息???

;服务器的源码
;---------------------------------
#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$AForm1 = GUICreate("serv", 282, 202, 294, 149)
GUISetOnEvent($GUI_EVENT_CLOSE, "_ext")
$Edit1 = GUICtrlCreateEdit("", 0, 0, 281, 161)
GUICtrlSetData(-1, "")
$Button1 = GUICtrlCreateButton("send", 32, 168, 73, 25, 0)
GUICtrlSetOnEvent(-1, "_send")
$Button2 = GUICtrlCreateButton("exit", 176, 168, 73, 25, 0)
GUICtrlSetOnEvent(-1, "_ext")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;----------------------------------

TCPStartUp()


$MainSocket = TCPListen(@IPAddress1, 65432,100 )
If $MainSocket = -1 ThenExit    ;创建监听不成功就退出

       
While 1
        $Socket = TCPAccept( $MainSocket)   ;接受一个套接字
        If @error Then Exit   ;如果错误就退出

If $Socket >= 0 Then
        $recv = TCPRecv( $Socket, 2048 );接收数据
        If @error Then Exit   ;如果错误就退出
        ;把接收的内容放到文本框内
        If $recv <> "" Then
                MsgBox(0,"接收内容",$recv)
                GUICtrlSetData($Edit1, $recv)
        EndIf
EndIf
        Sleep(100)
Wend

Func _ext()
        TCPCloseSocket($Socket)
        TCPShutdown ( )
   Exit
EndFunc

Func _send()
   $szData = GUICtrlRead($Edit1)
    If @error Then Exit
        If $szData = "" Then
                MsgBox(0,"提示","消息不能为空!",2)
        Else
                MsgBox(0,"提示","发送内容为:" & $szData,2)
                TCPSend($Socket,$szData)   
        EndIf;发送信息
        If @error Then Exit
EndFunc

;=======================================================================

;客户机源码
;----------------------------------
#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$AForm1 = GUICreate("cl--", 282, 202, 294, 149)
GUISetOnEvent($GUI_EVENT_CLOSE, "_ext")
$Edit1 = GUICtrlCreateEdit("", 0, 0, 281, 161)
GUICtrlSetData(-1, "")
$Button1 = GUICtrlCreateButton("send", 32, 168, 73, 25, 0)
GUICtrlSetOnEvent(-1, "_send")
$Button2 = GUICtrlCreateButton("exit", 176, 168, 73, 25, 0)
GUICtrlSetOnEvent(-1, "_ext")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;-------------------------------------------

$Server_IP = "192.168.1.22"    ;设置服务器IP

TCPStartUp()

$socket = TCPConnect( $Server_IP, 65432 )
If $socket = -1 Then    ;创建连接不成功就弹出信息
        MsgBox(0,"提示","服务器连接不成功!",2)
EndIf

While 1
        If $Socket >= 0 Then
        $recv = TCPRecv( $Socket, 2048 );接收数据
        If @error Then Exit   ;如果错误就退出
        ;把接收的内容放到文本框内
        If $recv <> "" Then
                MsgBox(0,"接收内容",$recv)
                GUICtrlSetData($Edit1, $recv)
        EndIf
EndIf
        Sleep (100)
WEnd

Func _ext()
        TCPCloseSocket($Socket)
        TCPShutdown ( )
   Exit
EndFunc

Func _send()
   $szData = GUICtrlRead($Edit1)
    If @error Then Exit
        If $szData = "" Then
                MsgBox(0,"提示","消息不能为空!",2)
        Else
                MsgBox(0,"提示","发送内容为:" & $szData,2)
                TCPSend($Socket,$szData)   ;发送信息
        EndIf
        If @error Then Exit
EndFunc

[ 本帖最后由 zjg2003 于 2009-3-16 19:32 编辑 ]

zjg2003 发表于 2009-3-16 15:52:07

占楼备用,高手帮忙看下哪写错了,谢谢先。

[ 本帖最后由 zjg2003 于 2009-3-16 17:27 编辑 ]
页: [1]
查看完整版本: 为什么不能接收信息???