Et bien repartons des exple de TCPsend et TCPrecv en les modifiant de la manière suivante :
le serveur va attendre une première info qui sera la taille du fichier envoyée. Celle ci sera converti en Hexa avec 8 caractère de formatage, comme ça on pourra faire n tcprecv sur 8 octets pour toutes les tailles.
Puis on lance un tcprecv sur la taille obtenu précédément.
côté client :
on lit le fichier à envoyé, on converti sa taille en hexa sur 8 bytes. on envoie cette info, puis on envoie le fichier buffurisé grâce à fileread.
serveur :
► Afficher le texte
Code : Tout sélectionner
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.0.0
Author: myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
Opt('TCPTimeout',2000)
;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================
Example()
Func Example()
; Set Some reusable info
; Set your Public IP address (@IPAddress1) here.
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
Local $msg, $recv,$recv_size
Local $dbg_log = FileOpen("dbg.txt",2)
; Start The TCP Services
;==============================================
TCPStartup()
; Create a Listening "SOCKET".
; Using your IP Address and Port 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
; If the Socket creation fails, exit.
If $MainSocket = -1 Then Exit
; Create a GUI for messages
;==============================================
$GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
GUISetState()
; Initialize a variable to represent a connection
;==============================================
$ConnectedSocket = -1
;Wait for and Accept a connection
;==============================================
Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; Get IP of client connecting
$szIP_Accepted = SocketToIP($ConnectedSocket)
; GUI Message Loop
;==============================================
While 1
$msg = GUIGetMsg()
; GUI Closed
;--------------------
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
; Try to receive the size of file
;----------------------------------------------------------------
$recv_size = TCPRecv($ConnectedSocket, 8)
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
FileWrite($dbg_log,$recv_size&@CRLF)
$recv_size=Dec($recv_size)
FileWrite($dbg_log,$recv_size&@CRLF)
If $recv_size > 1024*1024*1024 Then ExitLoop; on peut admettre qu'1Go s'est beaucoups ...
; Try to receive the size of file
;----------------------------------------------------------------
$recv = TCPRecv($ConnectedSocket, $recv_size)
; If the receive failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
FileWrite($dbg_log,$recv&@CRLF)
; Update the edit control with what we have received
;----------------------------------------------------------------
If $recv <> "" Then
GUICtrlSetData($edit, _
$szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
FileWrite("exple_recopie_serveur.txt",$recv)
EndIf
ExitLoop
WEnd
FileClose($dbg_log)
If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
TCPShutdown()
EndFunc ;==>Example
; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
Local $sockaddr, $aRet
$sockaddr = DllStructCreate("short;ushort;uint;char[8]")
$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
client :
► Afficher le texte
Code : Tout sélectionner
#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================
Example()
Func Example()
; Set Some reusable info
;--------------------------
Local $ConnectedSocket, $szData
; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $file_to_send = "param.ini",$szData_size
; Start The TCP Services
;==============================================
TCPStartup()
; Initialize a variable to represent a connection
;==============================================
$ConnectedSocket = -1
;Attempt to connect to SERVER at its IP and PORT 33891
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
; If there is an error... show it
If @error Then
MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
; If there is no error loop an inputbox for data
; to send to the SERVER.
Else
;Loop forever asking for data to send to the SERVER
While 1
; InputBox for data to transmit
;$szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")
$szData_size=FileGetSize($file_to_send)
$szData = FileRead($file_to_send)
; If they cancel the InputBox or leave it blank we exit our forever loop
If @error Or $szData = "" Then ExitLoop
ConsoleWrite(Hex($szData_size,8)&@CRLF)
ConsoleWrite($szData)
; We should have data in $szData... lets attempt to send it through our connected socket.
ConsoleWrite(TCPSend($ConnectedSocket, Hex($szData_size,8))&@CRLF)
Sleep(500)
ConsoleWrite(TCPSend($ConnectedSocket, $szData)&@CRLF)
; If the send failed with @error then the socket has disconnected
;----------------------------------------------------------------
If @error Then ExitLoop
ExitLoop
WEnd
EndIf
EndFunc ;==>Example
Tour l'aspect GUI viens de l'exple d'origine ... j'ai fait au plus rapide ...
le "serveur recopie dans cette exple tout fichier reçu dans "exple_recopie_serveur.txt" et laisse un log de debug
Toi qui cherche à mettre le doigt sur la solution, appuie sur F1.