PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_BdToSend
oleobject loo_SndSock
integer li_BUseTls
integer li_Port
integer li_MaxWaitMs
integer li_NumBytes
integer li_BBigEndian
oleobject loo_ListenSock
oleobject loo_RcvSock
integer li_NumBytesComing
oleobject loo_BdReceived
integer li_MaxWaitMs
li_Success = 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.
loo_BdToSend = create oleobject
li_rc = loo_BdToSend.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
destroy loo_BdToSend
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_BdToSend.LoadFile("somePath/someFile.dat")
// Assume success for the example...
loo_SndSock = create oleobject
li_rc = loo_SndSock.ConnectToNewObject("Chilkat.Socket")
li_BUseTls = 1
li_Port = 5555
li_MaxWaitMs = 5000
li_Success = loo_SndSock.Connect("some_domain_or_ip.com",li_Port,li_BUseTls,li_MaxWaitMs)
// Assume success for the example...
// Tell the receiver how many bytes are coming.
li_NumBytes = loo_BdToSend.NumBytes
li_BBigEndian = 1
li_Success = loo_SndSock.SendInt32(li_NumBytes,li_BBigEndian)
// Send the file data (sends the entire contents of bdToSend).
li_Success = loo_SndSock.SendBd(loo_BdToSend,0,0)
// Get an acknowledgement.
li_Success = loo_SndSock.ReceiveInt32(li_BBigEndian)
if li_Success = 0 then
Write-Debug loo_SndSock.LastErrorText
destroy loo_BdToSend
destroy loo_SndSock
return
end if
// Did the receiver get the correct number of bytes?
if loo_SndSock.ReceivedInt <> li_NumBytes then
Write-Debug "The receiver did not acknowledge with the correct number of bytes."
destroy loo_BdToSend
destroy loo_SndSock
return
end if
Write-Debug "File sent!"
// ------------------------------------------------------------------------------------
// The code below is for the receiving side (running on some other computer..)
loo_ListenSock = create oleobject
li_rc = loo_ListenSock.ConnectToNewObject("Chilkat.Socket")
li_Success = loo_ListenSock.BindAndListen(5555,25)
if li_Success = 0 then
Write-Debug loo_ListenSock.LastErrorText
destroy loo_BdToSend
destroy loo_SndSock
destroy loo_ListenSock
return
end if
// Get the next incoming connection
// Wait a maximum of 20 seconds (20000 millisec)
loo_RcvSock = create oleobject
li_rc = loo_RcvSock.ConnectToNewObject("Chilkat.Socket")
li_Success = loo_ListenSock.AcceptNext(20000,loo_RcvSock)
if li_Success = 0 then
Write-Debug loo_ListenSock.LastErrorText
destroy loo_BdToSend
destroy loo_SndSock
destroy loo_ListenSock
destroy loo_RcvSock
return
end if
// The sender will first send the big-endian integer for the number of bytes
// that are forthcoming..
li_Success = loo_RcvSock.ReceiveInt32(li_BBigEndian)
if li_Success <> 1 then
Write-Debug loo_RcvSock.LastErrorText
destroy loo_BdToSend
destroy loo_SndSock
destroy loo_ListenSock
destroy loo_RcvSock
return
end if
li_NumBytesComing = loo_RcvSock.ReceivedInt
// Receive that many bytes..
loo_BdReceived = create oleobject
li_rc = loo_BdReceived.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_RcvSock.ReceiveBdN(li_NumBytesComing,loo_BdReceived)
if li_Success <> 1 then
Write-Debug loo_RcvSock.LastErrorText
destroy loo_BdToSend
destroy loo_SndSock
destroy loo_ListenSock
destroy loo_RcvSock
destroy loo_BdReceived
return
end if
// Acknowledge the sender by sending back the number of bytes we received.
li_Success = loo_RcvSock.SendInt32(loo_BdReceived.NumBytes,li_BBigEndian)
// Close the connection.
li_MaxWaitMs = 20
loo_RcvSock.Close(li_MaxWaitMs)
// Save the received data to a file.
li_Success = loo_BdReceived.WriteFile("somePath/someFile.dat")
// Assume success for the example...
Write-Debug "File received!"
destroy loo_BdToSend
destroy loo_SndSock
destroy loo_ListenSock
destroy loo_RcvSock
destroy loo_BdReceived