Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Transfer a File using Sockets (TLS or non-TLS)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.
integer li_rc oleobject loo_BdToSend integer li_Success 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 // 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 // Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 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 // Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0 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 <> 1 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 // Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0 li_rc = loo_ListenSock.ConnectToNewObject("Chilkat.Socket") li_Success = loo_ListenSock.BindAndListen(5555,25) if li_Success <> 1 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 = loo_ListenSock.AcceptNextConnection(20000) if loo_ListenSock.LastMethodSuccess = 0 then Write-Debug loo_ListenSock.LastErrorText destroy loo_BdToSend destroy loo_SndSock destroy loo_ListenSock 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_RcvSock destroy loo_BdToSend destroy loo_SndSock destroy loo_ListenSock return end if li_NumBytesComing = loo_RcvSock.ReceivedInt // Receive that many bytes.. loo_BdReceived = create oleobject // Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 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_RcvSock destroy loo_BdToSend destroy loo_SndSock destroy loo_ListenSock 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) destroy loo_RcvSock // 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_BdReceived |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.