|
源码是xinlu8439 的
http://www.autoitx.com/forum.php ... 6%B6%E0%CF%DF%B3%CC
源码修改下。运行客户端后,从服务端下载图片到客户端。下图片能正常。超过4.27KB图片出错。不知道什么问题。还请那个帮我一下
图片没有完全传输。
服务端:#Region -- Main
;~ #NoTrayIcon
Opt("TrayAutoPause", 0) ; do not auto pause the script when click on the tray icon
Global Const $Port = 8888 ; application TCP port to use
Global $SvrIP = "192.168.18.212" ; optional: @IPAddress1, @IPAddress2, @IPAddress3, @IPAddress4, or a preset IP
Global $MaxConc = 10 ; Max clients allowed
Global $MainSocket = TCPStartServer($Port, $MaxConc) ; initial server listening
If @error <> 0 Then Exit MsgBox(16, "Error", "Server unable to initialize.")
Global Const $MaxLength = 512
Global $ConnectedSocket[$MaxConc]
Global $ClientIP[$MaxConc] ; same type as $ConnectedSocket, but for storing Clients' IPs
Global $CurrentSocket = 0
Local $Track = 0
Global Const $MaxConnection = ($MaxConc - 1)
For $Track = 0 To $MaxConnection Step 1
$ClientIP[$Track] = ""
$ConnectedSocket[$Track] = -1
Next
While 1
$ConnectedSocket[$CurrentSocket] = TCPAccept($MainSocket)
If $ConnectedSocket[$CurrentSocket] <> - 1 Then
$CurrentSocket = SocketSearch()
EndIf
$Track = 0
For $Track = 0 To $MaxConnection Step 1
If $ConnectedSocket[$Track] <> - 1 Then
$ClientIP[$Track] = SocketToIP($ConnectedSocket[$Track]) ; to store the client IPs
ConsoleWrite($ClientIP[$Track] & ":" & $ConnectedSocket[$Track] & @LF)
$Data = TCPRecv($ConnectedSocket[$Track], $MaxLength)
Select
Case $Data = ""
; do nothing
Case StringInStr($Data, "--COMMAND1--", 1)
ConsoleWrite($Data & @LF)
; 如果需要,执行自定义Func
TCPSendMessageBack($MaxConnection, "--A--") ; 回馈信息
TCPCloseSocket($ConnectedSocket[$Track])
$ConnectedSocket[$Track] = -1
$CurrentSocket = SocketSearch()
Case Else
ConsoleWrite("Unkown Command: " & $Data & @LF)
ConsoleWrite("Unkown Command: " & FileGetSize(@ScriptDir&$Data) & @LF)
$Size=FileGetSize(@ScriptDir&$Data)
;TCPSendMessageBack($MaxConnection, "--A--")
$rFile = FileOpen(@ScriptDir&$Data, 16)
While 1
$rData = FileRead($rFile, 1024 * 1024);开始读取文件
If @error = -1 Then ExitLoop;读到文件结尾后退出到主循环
TCPSendMessageBack($MaxConnection, $rData)
WEnd
; 如果需要,执行自定义Func
;TCPSendMessageBack($MaxConnection, "--UNKOWN--")
TCPCloseSocket($ConnectedSocket[$Track])
$ClientIP[$Track] = ""
$ConnectedSocket[$Track] = -1
$CurrentSocket = SocketSearch()
EndSelect
EndIf
Next
WEnd
#EndRegion
#Region -- User Defined Actions (functions)
; 所有自定义Func添加在这里
Func _SendFile($SendFile, $rFolder, $Source_IP, $sType = "");开始TCP静默发送文件
$sSize = FileGetSize($SendFile);文件总字节数接口
If $sSize <> 0 Then;如果不是空字节
;_SendFileMsg($SendFile & "|" & $sSize & "|" & $rFolder & "|" & $sType & "|fileok", $Source_IP);通知对方文件执行方式
Sleep(200);延时
$rFile = FileOpen($SendFile, 16);二进制读取文件
$sSocket = TCPConnect($Source_IP, $Port);创建一个套接字连接
If $sSocket <> -1 Then
While 1
$rData = FileRead($rFile, 1024 * 1024);开始读取文件
If @error = -1 Then ExitLoop;读到文件结尾后退出到主循环
TCPSend($sSocket, $rData);发送读到的文件字节
WEnd
Return 1;返回状态码
Else
Return 0
EndIf
FileClose($rFile);关闭文件句柄
If $sSocket <> -1 Then TCPCloseSocket($sSocket);关闭套接字连接
EndIf
EndFunc ;==>_SendFile
#EndRegion
#Region -- TCP Server Components (DO NOT CHANGE!)
; search for an unused socket
Func SocketSearch()
Local $Track = 0
For $Track = 0 To $MaxConnection Step 1
If $ConnectedSocket[$Track] = -1 Then
Return $Track
Else
; Socket In Use
EndIf
Next
EndFunc ; ==> SocketSearch()
; Function to send "feedback" back to the client via $ConnectedSocket[]
; which will be the $MainSocket on the client side.
Func TCPSendMessageBack($ConnectionLimit, $Data)
Local $Track = 0
For $Track = 0 To $ConnectionLimit Step 1
If $ConnectedSocket[$Track] <> - 1 Then TCPSend($ConnectedSocket[$Track], $Data)
Next
EndFunc ; ==> TCPSendMessageBack()
; Function to initialize a socket connection
; returns the listening socket if succeeds
Func TCPStartServer($Port, $MaxConnect = 1)
Local $Socket
$Socket = TCPStartup()
Select
Case $Socket = 0
SetError(@error)
ConsoleWrite("<Error> Server unable to initialize. TCPStartup() Error: " & @error & @LF)
Return -1
EndSelect
$Socket = TCPListen($SvrIP, $Port, $MaxConnect)
Select
Case $Socket = -1
SetError(@error)
ConsoleWrite("<Error> Server unable to initialize. TCPListen() Error: " & @error & @LF)
Return 0
EndSelect
SetError(0)
ConsoleWrite("Server is now running." & @LF)
Return $Socket
EndFunc ; ==> TCPStartServer()
; Function to return IP Address from a connected socket.
Func SocketToIP($SHOCKET)
Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")
Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
"ptr",DLLStructGetPtr($sockaddr),"int*",DLLStructGetSize($sockaddr))
If Not @error And $aRet[0] = 0 Then
$aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))
If Not @error Then $aRet = $aRet[0]
Else
$aRet = 0
EndIf
$sockaddr = 0
Return $aRet
EndFunc ; ==> SocketToIP()
#EndRegion
客户端:Global Const $Port = 8888 ; 服务器监听端口
Global $Svr_IP = "192.168.18.212", $RaNO ; 直接使用IP
$Svra='\PNG\icon\015.png'
Func TalkToServer($Sent_Msg)
TCPStartup()
;~ Local $Svr_IP = TCPNameToIP($Svr_Name) ; 如果服务器直接使用IP,那么不需要解析域名
$MainSocket = TCPConnect($Svr_IP, $Port)
If $MainSocket = -1 Then Exit MsgBox(0, "Error", "Could not connect the server. Please contact your system administrator.")
$TCPSent = TCPSend($MainSocket, $Sent_Msg)
#Region -- 如果不需要等待回馈信息,请删除或注释以下代码
While 1
$rFile = FileOpen( @ScriptDir & ""&$filename, 2 + 8 + 16)
Local $ri = 0
$Recv = TCPRecv($MainSocket, 1024 * 1024, 1)
If $Recv <> "" Then
Select
Case $Recv == "--COMMAND1--"
MsgBox(0, Default, "Recieved: " & $Recv, 3)
ExitLoop
Case Else ; basically, this is for debug purpose
;MsgBox(32, "Need Help", "Oh...I don't understand the server's command: " & $Recv)
;MsgBox(0,0,$Recv)
;_ReceiveFile($Recv, "1.PNG", "")
If Not @error Then
$ri += BinaryLen($Recv);已读取字节计数器
FileWrite($rFile, $Recv);写入文件
EndIf
ExitLoop
EndSelect
EndIf
WEnd
#EndRegion
TCPCloseSocket($MainSocket)
TCPShutdown()
EndFunc
Func _getfilename($spath, $sflag = 0)
If $sflag = 0 Then
$sflag = 0
Else
$sflag = 1
EndIf
If $sflag = 0 Then
$sFileName = StringRegExpReplace($spath, '.+\\', '')
Else
$sFileName = StringRegExpReplace($spath, '.+\\', '')
$sFileName = StringRegExpReplace($sFileName, '\.[^\.]*$', '')
EndIf
Return ($sFileName)
EndFunc ;==>_getfilename
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入
×
|