Sample code for 30+ languages & platforms
AutoIt

Transfer a File using Sockets (TLS or non-TLS)

See more Socket/SSL/TLS Examples

Demonstrates how to two programs, one a socket writer and the other a socket reader, can transfer a file. The connection can be TLS or a regular non-encrypted TCP connection.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

; On the sending side, we'll load the file into a BinData object and send.
; On the receiving side, we'll read from the socket connection into a BinData, and save to a file.
; This example assumes the file is not crazy-large, and that the entire contents
; can fit into memory.  
; (If the file is too large for memory, there are other ways to send. It just involves streaming or 
; sending the file chunk-by-chunk..)

; This section of code is for the sender.
$oBdToSend = ObjCreate("Chilkat.BinData")
$bSuccess = $oBdToSend.LoadFile("somePath/someFile.dat")
; Assume success for the example...

$oSndSock = ObjCreate("Chilkat.Socket")
Local $bUseTls = True
Local $iPort = 5555
Local $iMaxWaitMs = 5000
$bSuccess = $oSndSock.Connect("some_domain_or_ip.com",$iPort,$bUseTls,$iMaxWaitMs)
; Assume success for the example...

; Tell the receiver how many bytes are coming.
Local $iNumBytes = $oBdToSend.NumBytes
Local $bBigEndian = True
$bSuccess = $oSndSock.SendInt32($iNumBytes,$bBigEndian)

; Send the file data (sends the entire contents of bdToSend).
$bSuccess = $oSndSock.SendBd($oBdToSend,0,0)

; Get an acknowledgement.
$bSuccess = $oSndSock.ReceiveInt32($bBigEndian)
If ($bSuccess = False) Then
    ConsoleWrite($oSndSock.LastErrorText & @CRLF)
    Exit
EndIf

; Did the receiver get the correct number of bytes?
If ($oSndSock.ReceivedInt <> $iNumBytes) Then
    ConsoleWrite("The receiver did not acknowledge with the correct number of bytes." & @CRLF)
    Exit
EndIf

ConsoleWrite("File sent!" & @CRLF)

; ------------------------------------------------------------------------------------
; The code below is for the receiving side (running on some other computer..)

$oListenSock = ObjCreate("Chilkat.Socket")

$bSuccess = $oListenSock.BindAndListen(5555,25)
If ($bSuccess = False) Then
    ConsoleWrite($oListenSock.LastErrorText & @CRLF)
    Exit
EndIf

; Get the next incoming connection
; Wait a maximum of 20 seconds (20000 millisec)
$oRcvSock = ObjCreate("Chilkat.Socket")
$bSuccess = $oListenSock.AcceptNext(20000,$oRcvSock)
If ($bSuccess = False) Then
    ConsoleWrite($oListenSock.LastErrorText & @CRLF)
    Exit
EndIf

; The sender will first send the big-endian integer for the number of bytes
; that are forthcoming..
$bSuccess = $oRcvSock.ReceiveInt32($bBigEndian)
If ($bSuccess <> True) Then
    ConsoleWrite($oRcvSock.LastErrorText & @CRLF)
    Exit
EndIf

Local $iNumBytesComing = $oRcvSock.ReceivedInt

; Receive that many bytes..
$oBdReceived = ObjCreate("Chilkat.BinData")
$bSuccess = $oRcvSock.ReceiveBdN($iNumBytesComing,$oBdReceived)
If ($bSuccess <> True) Then
    ConsoleWrite($oRcvSock.LastErrorText & @CRLF)
    Exit
EndIf

; Acknowledge the sender by sending back the number of bytes we received.
$bSuccess = $oRcvSock.SendInt32($oBdReceived.NumBytes,$bBigEndian)

; Close the connection.
Local $iMaxWaitMs = 20
$oRcvSock.Close($iMaxWaitMs)

; Save the received data to a file.
$bSuccess = $oBdReceived.WriteFile("somePath/someFile.dat")
; Assume success for the example...

ConsoleWrite("File received!" & @CRLF)