找回密码
 加入
搜索
查看: 3634|回复: 4

[网络通信] 【已解决】TCP_UDF是不是有问题啊,传输的文件只能接收到一次信息(详进)

[复制链接]
发表于 2012-10-5 17:54:09 | 显示全部楼层 |阅读模式
本帖最后由 xg911335 于 2012-10-6 03:11 编辑

服务器:
Func Receive($iError, $sreceive)
        $sString = StringSplit($sreceive,"|")
        If $sString[1] = "Online" Then
                GUICtrlCreateListViewItem($sreceive&"|"&@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC,$ListView1)
        ElseIf $sString[1] = "Csend" Then
                $tmpFSize = $sString[2]
                $tmpFName = $sString[3]
                MsgBox(0,"服务器信息","客户端载屏文件名:"&$tmpFName&" 文件大小:"&$tmpFSize&"完成,准备上传……")
                $file = FileOpen(@ScriptDir & "\temp"& $tmpFName &"",2+8+16)
                _TCP_Server_Send("Srecv|File")
        ElseIf $sString[1] = "tfile" Then
                        FileWrite(@ScriptDir & "\temp"& $tmpFName &"",BinaryToString($sString[2],4))
                        MsgBox(0,"S2",$sString[2])
        ElseIf $sreceive = "FileOver" Then
                        FileClose($file)
                                                ……………………        
                 ElseIf $sString[1] = "Ctest" Then
                MsgBox(0,"S3",$sString[2])
        EndIf
EndFunc
While 1
         Switch GUIGetMsg()
                   Case $GUI_EVENT_CLOSE
                           Exit
                   Case $Button1
                   Case $zppic
                        _TCP_Server_Send("Image|Nmae|Size")
                Case $clsj
                        _TCP_Server_Send("test")
           EndSwitch
WEnd
客户端:
While 1 ;循环接收服务器指令
        Sleep(900)
        $command=TCPRecv($ConnectedSocket,2048)
        If @error Then
                MsgBox(0,"Client","服务器与客户端断开")
        ElseIf $command <> "" Then
                _runcommand($command)
        EndIf
WEnd ;循环接收服务器指令

Func _runcommand($str)
        Switch $str
                Case "Image|Nmae|Size"
                        $tmpNfile = @ComputerName &"_"& @YEAR & "" & @MON & "" & @MDAY & "_" & @HOUR & "" & @MIN & "" & @SEC & ".jpg"
                        $tmpimg = _ScreenCapture_Capture( @ScriptDir & "" & $tmpNfile)
                        Sleep(1000)
                        If $tmpimg = True Then
                                $tmpFsize = FileGetSize($tmpNfile)
                                $file = FileOpen($tmpNfile,16)
                                ;$tmpOpen = FileOpen($tmpfile,16)
                                TCPSend($ConnectedSocket,"Csend|" & $tmpFsize &" | " & $tmpNfile & "")
                        Else
                                MsgBox(0,"客户端信息","载图失败.")
                        EndIf
                Case "Srecv|File"
                                Do
                                        $rDate = FileRead($file,1024 * 2)
                                        If $rDate = 1 Then ExitLoop
                                        $tmpDate = "tfile|"&$rDate&""
                                        Sleep(500)
                                        MsgBox(0,"C1",$tmpDate)
                                        TCPSend($ConnectedSocket,$tmpDate)
                                        If @error Then ;MsgBox(0,"C2","error")
                                Until $rDate = -1
                                MsgBox(0,"C3",$rDate)
                                $tmpFclose = FileClose($file)
                                TCPSend($ConnectedSocket,"FileOver")
                Case "test"
                                While 1
                                        Sleep(1000)
                                        MsgBox(0,"C","Ctest|123123")
                                        $tmptext = TCPSend($ConnectedSocket,"Ctest|123123")
                                        If $tmptext = 0 Then MsgBox(0,"C","Error")
                                WEnd
        EndSwitch
EndFunc
为什么偶尔有一次能接收文件,是不是这个TCP_UDF有问题啊?服务器发送一个test,客户端收到后,也是偶尔一次不断循环向服务器发送信息?请问这是怎么回事?
发表于 2012-10-5 22:24:36 | 显示全部楼层
代码逻辑有问题,UDF没问题。
 楼主| 发表于 2012-10-6 03:11:28 | 显示全部楼层
已经搞定。谢谢楼上!
发表于 2013-3-8 18:54:34 | 显示全部楼层
学习看看..
发表于 2013-4-1 11:39:20 | 显示全部楼层
解决代码能发出来大家看看嘛?
我遇到一个问题是,传输文件,接收端只接收到文件,没有接收到文件里的内容,能给解决下吗?
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-16 03:49 , Processed in 0.078310 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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