Results 1 to 25 of 25

Thread: Sending files via winsock

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Sending files via winsock

    Hello,

    For my client server program that I am using on my home network I have to send a file from the server to the client or vise versa.

    I have been looking at the necessay code although it is quite complex.

    Please could someone try and explain what this code would be like or where to find it.

    I know that I have to use either a binary transfer or winsock control.
    Thank you

  2. #2
    Junior Member
    Join Date
    Jul 2006
    Posts
    17

    Re: Sending files via winsock

    You could take blocks of data from your file, convert the binary to hexadecimal values (in a format like string:FF071BEE, send it through your winsock control (ascii) and convert it on the other side (at the receiver's)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    That sounds complex, heres what I got so far:

    Client
    Code:
    Private Sub command3_click()
        Dim pb As PropertyBag
        Set pb = New PropertyBag
        Dim bArr() As Byte
        
        Winsock1.GetData bArr()
        pb.Contents = bArr()
        
        screenCapture.Picture = pb.ReadProperty("hello")
    End Sub
    Server
    Code:
    Dim pb As PropertyBag
            Set pb = New PropertyBag
            
            Dim bArr() As Byte
            
            pb.WriteProperty "hello", "c:\hello.bmp", Nothing
            bArr = pb.Contents
            
            Winsock1.SendData bArr()
            
            DoEvents
            DoEvents
            DoEvents
    does not work tho, please help
    Cheers

  4. #4
    Junior Member
    Join Date
    Jul 2006
    Posts
    17

    Re: Sending files via winsock

    try this:
    VB Code:
    1. Public Sub sendFile(location As String, socket As Winsock)
    2.     Dim fNum As Integer
    3.     Dim fLen As Integer
    4.     fNum = FreeFile
    5.     fLen = FileLen(location)
    6.     Dim bytes(1 To fLen) As Byte
    7.     Open location For Binary As fNum
    8.     Get #fNum, 1, bytes
    9.     Close fNum
    10.     socket.SendData (bytes)
    11. End Sub
    12.  
    13. Public Sub getFile(location As String, socket As Winsock)
    14.     Dim fNum As Integer
    15.     fNum = FreeFile
    16.     Dim bytes() As Byte
    17.     socket.GetData (bytes)
    18.     Open location For Binary As fNum
    19.     Get #fNum, 1, bytes
    20.     Close fNum
    21. End Sub

    Haven't tested it yet, but something like this should work.. It's a bit less complicated than my first idea. Maybe you'll have to send file info like size first, to determine the array size on the receiver's side.
    Goodluck!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    How would i customize this to make it work for me e.g. would lcoation be the file and path - c:\hello.bmp

    Cheers

  6. #6
    Junior Member
    Join Date
    Jul 2006
    Posts
    17

    Re: Sending files via winsock

    server:
    Create a form with a winsock component in it.
    Accept client connection
    call the getfile function: location is the location of the new file to be received on your hdd, socket is the just connected socket (I do not know how the getData function of the winsock object works exactly, I think it'll just wait for data being received)

    Client:
    Create a form with a winsock component in it.
    Setup a connection with the component (connect to server)
    call the sendfile function: location is the location of the file to be sent on your hdd, socket is the just connected socket

    Again: I haven't tested this yet

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    i get a compile error:

    constant expression required

    and is "fLen" highlighted

    cheers

  8. #8
    Junior Member
    Join Date
    Jul 2006
    Posts
    17

    Re: Sending files via winsock

    Try this:
    Dim bytes() As Byte
    ReDim bytes(1 To fLen) As Byte

    Instead of:
    Dim bytes(1 To fLen) As Byte

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    OK cheers that got rid of that error.
    Server:
    Code:
    Dim location As String
            location = "c:\desktop.bmp"
            Dim fNum As Integer
            Dim fLen As Integer
            fNum = FreeFile
            fLen = FileLen(location)
            Dim bytes() As Byte
            ReDim bytes(1 To fLen) As Byte
            Open location For Binary As fNum
            Get #fNum, 1, bytes
            Close fNum
            Winsock1.SendData (bytes)
    Client:

    Code:
    Private Sub command3_click()
        
        Dim location As String
        Dim fNum As Integer
        location = "c:\desktop.bmp"
        fNum = FreeFile
        Dim bytes() As Byte
        Winsock1.GetData (bytes)
        Open location For Binary As fNum
        Get #fNum, 1, bytes
        Close fNum
    End Sub
    Please can you tell me what is wrong with this code. It sends a file but it is of 0 bytes and therefoer does not show anything.

    Cheers

  10. #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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    Thank you for that code, this is most helpful.

    I finally got a file to send over the network although it onyl works when I use the default gateway for the IP - why is this?

    Cheers

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    The other problem is that when the file is sent and then recieved it is of size 0 bytes.

    Just about to give up on this, unless someone can tell what I have to chaneg in the code above. Not critising the code, just cant get it to work.

    Cheers

  13. #13
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Sending files via winsock

    works for me .. make sure any firewalls are open etc ..

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    So it will work when not using default gateway i.e. using external IP instead?
    cheers

  15. #15
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Sending files via winsock

    You need to set an IP address .. eg. 24.244.23.545
    not the gateway ..

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    OK ill try again. If you change the IP in your code, do you only have to change one thing. Just thought that I might not be changing enough code for it to work.

  17. #17
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Sending files via winsock

    just the IP and the Port Number ... Port Number has to be the same in both, it will give a Message box error anyway if it cant connect.

    and make sure you either have a file called tmp.jpg in the app path, or rename that file to something else that exists ..

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    Well sorry but its still not working - the file tmp.jpg is on the machine with the Recieve.exe on right - and then tmp.jpg arrives on machnie with Send.exe on.

    I keep getting supscript out of range error (9).

    Would really like to get this going.
    cheers for helps so far

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    30

    Re: Sending files via winsock

    OK got it working, dunno what i did but it works

  20. #20
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    256

    Re: Sending files via winsock

    The File tranfers work wonder..but what if I want to tranfer an Entire folder...(which may contains some files and folders)..how to go about it..please Help....

  21. #21
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Sending files via winsock

    Quote Originally Posted by rory
    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 ..
    That would be me, and you got it from here: http://www.vbforums.com/showthread.php?t=331990

    Here's another client/server to transfer files:
    http://www.vbforums.com/showthread.php?t=377648

    To send multiple files, list the files in a collection (or something like that), send each file one by one.
    Don't forget to WAIT until file is complectly sent, and then send the other file, and so on...

  22. #22
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Sending files via winsock

    Quote Originally Posted by Kuamr
    The File tranfers work wonder..but what if I want to tranfer an Entire folder...(which may contains some files and folders)..how to go about it..please Help....
    Another way to do it, is to ZIP the whole directory, therefore having one file (and less data to transfer because is compressed), then send that file, and on the other side, just un-zip the file at your target location...

  23. #23
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Sending files via winsock

    Quote Originally Posted by CVMichael
    That would be me, and you got it from here: http://www.vbforums.com/showthread.php?t=331990

    Here's another client/server to transfer files:
    http://www.vbforums.com/showthread.php?t=377648

    To send multiple files, list the files in a collection (or something like that), send each file one by one.
    Don't forget to WAIT until file is complectly sent, and then send the other file, and so on...
    Thanks

  24. #24
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    256

    Re: Sending files via winsock

    thanks CV Michael..

  25. #25
    New Member
    Join Date
    Jan 2008
    Posts
    1

    Re: Sending files via winsock

    Hi, CVMicheal....

    Nice to meet you..., i am new member of this forum....

    i already trial your program,..
    but if i send Exe File, server never complete receive data....

    do you know why am i always fail(never complete) send exe file ???


    Thanks.....

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