Re: TCP Functionality example?
"valery_vi" <[email protected]>
| Newsgroups | gmane.comp.windows.autoit.user |
|---|---|
| Message-ID | <[email protected]> |
Keath,
I can mistake about who is the author of these scripts (Sender.au3 &
Receiver.au3). Larry?
Sender.au3:
; BINARY FILE TEST SENDER
Global Const $TCP_PORT = 3333
Dim $nSocketSend = -1
Dim $file = ""
TCPStartup()
$file = FileOpenDialog("File To Send","","All Files (*.*)")
If @error Then Exit
$pc = InputBox("",@LF & @LF & "Computer to receive file." & @LF
& "(Prefix with \ if computer name.)")
If $pc = "" Then Exit
If StringLeft($pc,1) = "\" Then
$pc = TCPNameToIP(StringTrimLeft($pc,1))
EndIf
ToolTip("Sending file...")
$socket = TCPConnect($pc,$TCP_PORT)
If $socket = -1 Then Exit
$n = FileGetSize($file)
$buffer = StringTrimLeft($file,StringInStr($file,"\",0,-1)) & "," &
$n & "," & FileRead($file)
While StringLen($buffer)
$x = TCPSend($socket,$buffer)
MsgBox(0,"", "n -= " & $n)
If @error Then ExitLoop
$n -= $x
$buffer = StringTrimLeft($buffer,$x)
WEnd
While 1
If TCPRecv($socket,2048) <> "" Then ExitLoop
If @error Then ExitLoop
WEnd
MsgBox(4096,"","Finished Sending " & $file)
;=========================================
Receiver.au3:
; BINARY FILE TEST RECEIVER
Global Const $TCP_PORT = 3333
Dim $nSocketRecv = -1
Dim $bWriting = 0
Dim $szRecvBuffer = ""
Dim $nMainSocket = -1
Dim $szFileWrite = ""
Dim $nFileBytes = 0
TCPStartup()
$nMainSocket = TCPListen( @IPAddress1, $TCP_PORT )
While $nSocketRecv = -1
ToolTip( "waiting for incoming file...", 0, 0 )
$nSocketRecv = TCPAccept( $nMainSocket )
WEnd
ToolTip("Receiving file...")
While 1
$szRecvBuffer &= TCPRecv( $nSocketRecv, 2048 )
If @error Then ExitLoop
If StringLen($szRecvBuffer) Then
If $bWriting = 0 Then
If StringInStr($szRecvBuffer,",") Then
$bWriting = 1
$szFileWrite = StringLeft($szRecvBuffer,StringInStr
($szRecvBuffer,",")-1)
$szRecvBuffer = StringTrimLeft
($szRecvBuffer,StringLen($szFileWrite)+1)
EndIf
EndIf
If $bWriting = 1 Then
If StringInStr($szRecvBuffer,",") Then
$bWriting = 2
$nFileBytes = StringLeft($szRecvBuffer,StringInStr
($szRecvBuffer,",")-1)
$szRecvBuffer = StringTrimLeft
($szRecvBuffer,StringLen($nFileBytes)+1)
$nFileBytes = Number($nFileBytes)
EndIf
EndIf
If $bWriting = 2 Then
If StringLen($szRecvBuffer) Then
If StringLen($szRecvBuffer) >= $nFileBytes Then
FileWrite($szFileWrite,StringLeft
($szRecvBuffer,$nFileBytes))
ExitLoop
Else
FileWrite($szFileWrite,$szRecvBuffer)
$nFileBytes -= StringLen($szRecvBuffer)
$szRecvBuffer = ""
EndIf
EndIf
EndIf
EndIf
WEnd
TCPSend($nSocketRecv,"go away")
ToolTip("")
MsgBox(4096,"","done with " & @ScriptDir & "\" & $szFileWrite)
It can be useful for you, I think.
Enjoy,
Valery
--- In [email protected], <marx.external@...> wrote:
>
> Does anyone have a script they might share on the use of TCP
> functionality? I have worked with the TCP Server and Client stuff
in
> the help file and understand that for the most part but I am
looking to
> do something with 2 way communication and am not sure how handle
that.
> It doesn't HAVE to be asynchronous communication. Synchronous
would
> work but I was just trying to see if there was a better way to do
it
> other than sending data and then waiting for an acknowledgement
and then
> sending more data, etc...
>
>
>
> I was hoping perhaps someone had implemented this type
communication and
> could share.
>
>
>
> Thanks
>
> Keath
>
>
>
> [Non-text portions of this message have been removed]
>