Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Receive File on SocketDemonstrates how to listen for an incoming socket connection and receive a file. The received file is saved to disk.
Dim listenSocket As New ChilkatSocket Dim success As Long success = listenSocket.UnlockComponent("Anything for 30-day trial") If (success <> 1) Then MsgBox "Failed to unlock component" Exit Sub End If ' Bind to a port and listen for incoming connections: ' This example will listen at port 5555 and allows for a backlog ' of 25 pending connection requests. success = listenSocket.BindAndListen(5555,25) If (success <> 1) Then MsgBox listenSocket.LastErrorText Exit Sub End If ' Get the next incoming connection ' Wait a maximum of 20 seconds (20000 millisec) Dim connectedSocket As ChilkatSocket Set connectedSocket = listenSocket.AcceptNextConnection(20000) If (connectedSocket Is Nothing ) Then MsgBox listenSocket.LastErrorText Exit Sub End If ' Set maximum timeouts for reading an writing (in millisec) connectedSocket.MaxReadIdleMs = 10000 connectedSocket.MaxSendIdleMs = 10000 ' Receive the byte count of the file data that is forthcoming: Dim fileSize As Long fileSize = connectedSocket.ReceiveCount() If (fileSize < 0) Then MsgBox connectedSocket.LastErrorText Exit Sub End If Dim fileData() As Byte fileData = connectedSocket.ReceiveBytesN(fileSize) If (connectedSocket.LastMethodFailed = 1) Then MsgBox connectedSocket.LastErrorText Exit Sub End If ' Close the connection. ' Wait a max of 20 seconds (20000 millsec) connectedSocket.Close 20000 ' Write the received data to a file. ' This example receives a GIF image file. Dim fac As New CkFileAccess success = fac.WriteEntireFile("received.gif",fileData) If (success <> 1) Then MsgBox listenSocket.LastErrorText Exit Sub End If MsgBox "success!" |
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.