Results 1 to 25 of 25

Thread: Sending files via winsock

Threaded View

  1. #10
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Sending files via winsock

    needs alot more than that .. you need to use the Winsock Functions ..
    im working on a specific winsock Send/Receive File app, ill post the code when done ..

    Meanwhile this is what im basing it off .. got this (or a version of it) off this forum somewhere... if its your code .. thanks .. cant remember who exactly posted it ..

    I stripped it down from a VNC type app i was messing with .. so in this case it waits for a connection from a remote client .. then once it accepts the connection it simply sends the file to the client (tmp.jpg in this instance).. very basic sample but this may give you an idea ..

    Both projects use a Winsock1 and a Timer1 .. Load Send Program First .. Receive Program Connects on Form Load. Port is 1003 and default IP is 192.168.1.2 for both of them ... (testing on local PC!). Missing alot of other features which i have in my other winsock app/s but it will work as is, for an example. An image called tmp.jpg is required in the Send Project's path to test .. or change the file name in the code.

    EDIT: i uploaded the 2 projects to make it easier..

    Send Project:
    VB Code:
    1. Option Explicit
    2.  
    3. Private iFileNum As Integer, lPacketSize As Long
    4.  
    5. Private Sub Form_Load()
    6. On Error GoTo Err
    7.     Winsock1.Close
    8.     Winsock1.LocalPort = 1003
    9.     Winsock1.Listen
    10.     Me.Caption = "Listening: Port 1003"
    11.     Exit Sub
    12. Err:
    13.     MsgBox "Socket Error!" & vbNewLine & _
    14.             Err.Description
    15.     Unload Me
    16.     Exit Sub
    17. End Sub
    18.  
    19. Private Sub Form_Unload(Cancel As Integer)
    20.     Winsock1.Close
    21.     Timer1.Interval = 0
    22.     Timer1.Enabled = False
    23. End Sub
    24.  
    25. Private Sub Winsock1_Close()
    26.     If Winsock1.State = sckClosing Then
    27.         Winsock1.Close
    28.     End If
    29. End Sub
    30.  
    31. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    32.     If Winsock1.State <> sckClosed Then
    33.         Winsock1.Close
    34.     End If
    35.     Winsock1.Accept requestID
    36.     SendFile Winsock1, App.Path & "\tmp.jpg"
    37. End Sub
    38.  
    39. Public Sub SendFile(SocketObject As Winsock, ByVal FilePath As String, Optional ByVal PacketSize As Long = 1024)
    40.     Dim Buffer() As Byte
    41.    
    42.     lPacketSize = PacketSize ' save the PacketSize for the timer
    43.     Timer1.Enabled = False ' make suze timer is not enabled
    44.    
    45.     iFileNum = FreeFile ' get free file number
    46.     Open FilePath For Binary Access Read As iFileNum ' open file
    47.    
    48.     ' if file size is smaller than PacketSize, then send the whole file, but not more
    49.     ReDim Buffer(lngMIN(LOF(iFileNum), PacketSize) - 1)
    50.     Get iFileNum, , Buffer ' read data
    51.    
    52.     SocketObject.SendData Buffer ' send data
    53. End Sub
    54.  
    55. Public Function lngMIN(ByVal L1 As Long, ByVal L2 As Long) As Long
    56.     If L1 < L2 Then
    57.         lngMIN = L1
    58.     Else
    59.         lngMIN = L2
    60.     End If
    61. End Function
    62.  
    63. Private Sub Winsock1_SendComplete()
    64.     Timer1.Enabled = False
    65.     Timer1.Interval = 1
    66.     Timer1.Enabled = True
    67. End Sub
    68.  
    69. Private Sub Timer1_Timer()
    70.     Dim Buffer() As Byte, BuffSize As Long
    71.     Timer1.Enabled = False
    72.     If iFileNum <= 0 Then Exit Sub
    73.     If Loc(iFileNum) >= LOF(iFileNum) Then ' FILE COMPLETE
    74.         Close iFileNum ' close file
    75.         iFileNum = 0 ' set file number to 0, timer will exit if another timer event
    76.         BuffSize = 0
    77.         Winsock1.Close
    78.         Winsock1.LocalPort = 1003
    79.         Winsock1.Listen
    80.         Me.Caption = "Listening: Port 1003"
    81.         Exit Sub
    82.     End If
    83.     'if the remaining size in the file is smaller then PacketSize, the read only whatever is left
    84.     BuffSize = lngMIN(LOF(iFileNum) - Loc(iFileNum), lPacketSize)
    85.     ReDim Buffer(BuffSize - 1) ' resize buffer
    86.     Get iFileNum, , Buffer ' read data
    87.     Winsock1.SendData Buffer ' send data
    88.     ' Show progress
    89.     Me.Caption = "Sending: " & Format(Loc(iFileNum) / CDbl(LOF(iFileNum)) * 100#, "#0.00") & "% Done"
    90.     ' timer event will be called again when last packet is sent, close the file then
    91. End Sub

    Receive Project;
    VB Code:
    1. Option Explicit
    2.  
    3. '// PROGRAM SETTINGS
    4. Const TCP_IP                As String = "192.168.1.2"
    5. Const TCP_PORT              As Long = 1003
    6.  
    7. Private ReceiveData         As String
    8. Private FileBytes           As Long
    9.  
    10. '// CLOSE WINSOCK
    11. Private Sub Winsock1_Close()
    12.     Dim strTempFile As String
    13.     Dim intFile As Integer
    14.     If Winsock1.State = sckClosing Then
    15.         Winsock1.Close
    16.     End If
    17.     strTempFile = App.Path & "\tmp.jpg"
    18.     intFile = FreeFile
    19.     Open strTempFile For Binary As #intFile
    20.     Put #intFile, , ReceiveData
    21.     Close #intFile
    22.     ReceiveData = ""
    23.     Me.Caption = "Done"
    24. End Sub
    25.  
    26. '// DATA ARRIVES
    27. Private Sub Winsock1_dataArrival(ByVal bytesTotal As Long)
    28.     Dim strData As String
    29.     Winsock1.GetData strData
    30.     ReceiveData = ReceiveData & strData
    31.     FileBytes = FileBytes + bytesTotal
    32.     Me.Caption = "Downloading: " & FileBytes & " bytes"
    33. End Sub
    34.  
    35. '// ERROR
    36. Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, _
    37. ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    38.     Winsock1.Close
    39.     MsgBox "Error Connecting!"
    40. End Sub
    41.  
    42. '// FORM LOAD
    43. Private Sub Form_Load()
    44.     Timer1.Enabled = True
    45.     Timer1.Interval = 100
    46.     With Winsock1
    47.         .RemoteHost = TCP_IP
    48.         .RemotePort = TCP_PORT
    49.         .Connect
    50.     End With
    51. End Sub
    52.  
    53. '// UNLOAD FORM
    54. Private Sub Form_Unload(Cancel As Integer)
    55.     Winsock1.Close
    56.     Timer1.Interval = 0
    57.     Timer1.Enabled = False
    58. End Sub
    Last edited by rory; Jul 16th, 2006 at 12:27 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width