Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oBdToSend
LOCAL oSndSock
LOCAL nBUseTls
LOCAL nPort
LOCAL nMaxWaitMs
LOCAL nNumBytes
LOCAL nBBigEndian
LOCAL oListenSock
LOCAL oRcvSock
LOCAL nNumBytesComing
LOCAL oBdReceived
LOCAL nMaxWaitMs

nSuccess := 0

//  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 := CreateObject("Chilkat.BinData")
nSuccess := oBdToSend:LoadFile("somePath/someFile.dat")
//  Assume success for the example...

oSndSock := CreateObject("Chilkat.Socket")
nBUseTls := 1
nPort := 5555
nMaxWaitMs := 5000
nSuccess := oSndSock:Connect("some_domain_or_ip.com", nPort, nBUseTls, nMaxWaitMs)
//  Assume success for the example...

//  Tell the receiver how many bytes are coming.
nNumBytes := oBdToSend:NumBytes
nBBigEndian := 1
nSuccess := oSndSock:SendInt32(nNumBytes, nBBigEndian)

//  Send the file data (sends the entire contents of bdToSend).
nSuccess := oSndSock:SendBd(oBdToSend, 0, 0)

//  Get an acknowledgement.
nSuccess := oSndSock:ReceiveInt32(nBBigEndian)
IF (nSuccess == 0)
    ? oSndSock:LastErrorText
    oBdToSend:destroy()
    oSndSock:destroy()
    RETURN
ENDIF

//  Did the receiver get the correct number of bytes?
IF (oSndSock:ReceivedInt != nNumBytes)
    ? "The receiver did not acknowledge with the correct number of bytes."
    oBdToSend:destroy()
    oSndSock:destroy()
    RETURN
ENDIF

? "File sent!"

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

oListenSock := CreateObject("Chilkat.Socket")

nSuccess := oListenSock:BindAndListen(5555, 25)
IF (nSuccess == 0)
    ? oListenSock:LastErrorText
    oBdToSend:destroy()
    oSndSock:destroy()
    oListenSock:destroy()
    RETURN
ENDIF

//  Get the next incoming connection
//  Wait a maximum of 20 seconds (20000 millisec)
oRcvSock := CreateObject("Chilkat.Socket")
nSuccess := oListenSock:AcceptNext(20000, oRcvSock)
IF (nSuccess == 0)
    ? oListenSock:LastErrorText
    oBdToSend:destroy()
    oSndSock:destroy()
    oListenSock:destroy()
    oRcvSock:destroy()
    RETURN
ENDIF

//  The sender will first send the big-endian integer for the number of bytes
//  that are forthcoming..
nSuccess := oRcvSock:ReceiveInt32(nBBigEndian)
IF (nSuccess != 1)
    ? oRcvSock:LastErrorText
    oBdToSend:destroy()
    oSndSock:destroy()
    oListenSock:destroy()
    oRcvSock:destroy()
    RETURN
ENDIF

nNumBytesComing := oRcvSock:ReceivedInt

//  Receive that many bytes..
oBdReceived := CreateObject("Chilkat.BinData")
nSuccess := oRcvSock:ReceiveBdN(nNumBytesComing, oBdReceived)
IF (nSuccess != 1)
    ? oRcvSock:LastErrorText
    oBdToSend:destroy()
    oSndSock:destroy()
    oListenSock:destroy()
    oRcvSock:destroy()
    oBdReceived:destroy()
    RETURN
ENDIF

//  Acknowledge the sender by sending back the number of bytes we received.
nSuccess := oRcvSock:SendInt32(oBdReceived:NumBytes, nBBigEndian)

//  Close the connection.
nMaxWaitMs := 20
oRcvSock:Close(nMaxWaitMs)

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

? "File received!"

oBdToSend:destroy()
oSndSock:destroy()
oListenSock:destroy()
oRcvSock:destroy()
oBdReceived:destroy()