Results 1 to 11 of 11

Thread: [RESOLVED] Trying winsock File Transfer (RESOLVED)

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Trying winsock File Transfer (RESOLVED)

    Hi,

    I had a go today at making my own file transfer example. This is what I have so far but I can't get in to work properly. What code do I need to add?


    VB Code:
    1. 'Client Code
    2. Dim file As String
    3. Dim Blocksize As Long
    4. Dim NF As Integer
    5.  
    6. Private Sub Command1_Click()
    7. CommonDialog1.ShowOpen
    8. Text1.Text = CommonDialog1.FileTitle
    9. End Sub
    10.  
    11. Private Sub Command2_Click()
    12. Winsock1.Connect "127.0.0.1", "7000"
    13. End Sub
    14.  
    15. Private Sub Command3_Click()
    16. NF = FreeFile
    17. Open CommonDialog1.filename For Binary Access Read As #NF
    18. file = Space$(Blocksize)
    19. Get #NF, , file
    20. Do Until EOF(NF)
    21. Winsock1.SendData file
    22. Loop
    23. Close #NF
    24. End Sub
    25.  
    26. 'Server Code
    27. Dim file As String
    28. Dim dat As String
    29. Dim NF As Integer
    30. Private Sub Form_Load()
    31. Winsock1.LocalPort = 7000
    32. Winsock1.Listen
    33. End Sub
    34.  
    35. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    36. If Winsock1.State <> sckClosed Then Winsock1.Close
    37. Winsock1.Accept requestID
    38. End Sub
    39.  
    40. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    41. On Error Resume Next
    42. Winsock1.GetData dat
    43. If InStr(1, dat, "|DATA|") <> 0 Then
    44. NF = FreeFile
    45. Open App.Path & "\test.exe" For Binary Access Write As #NF
    46. Put #NF, , file
    47. Close #NF
    48. End If
    49. End Sub

    Please give me some help to point me in the right direction.

    TIA

    NW
    Last edited by Nightwalker83; Apr 1st, 2003 at 04:30 AM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112
    hi!!

    Here is how I did it...

    Send File
    VB Code:
    1. Dim filenum As Integer
    2.     Dim var_data As String
    3.     filenum = FreeFile
    4.     Open g_FilePath For Binary Access Read Lock Read As #filenum
    5.         Do While (Loc(filenum) < LOF(filenum))
    6.             DoEvents
    7.             var_data = Input(4096, #filenum)
    8.             If Winsock1.State = 7 Then
    9.                 Winsock1.SendData var_data
    10.             End If
    11.         Loop
    12.     Close #filenum

    Receive file

    VB Code:
    1. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    2.     Dim bData As String
    3.     Winsock1.GetData bData, vbString
    4.    
    5.     If g_PutByte = 1 Then
    6.         g_FileOpen = FreeFile
    7.         Open g_FilePath For Binary As #g_FileOpen
    8.     End If
    9.  
    10.     Put #g_FileOpen, g_PutByte, bData
    11.  
    12.     g_PutByte = g_PutByte + Len(bData)
    13.    
    14.     ' The File Transfer is complete
    15.     If g_PutByte >= g_InFileSize Then
    16.         Close #g_FileOpen
    17.     End If
    18.    
    19. End Sub


    VB Code:
    1. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    2. On Error Resume Next
    3. Winsock1.GetData dat
    4. If InStr(1, dat, "|DATA|") <> 0 Then
    5. NF = FreeFile
    6. Open App.Path & "\test.exe" For Binary Access Write As #NF
    7. Put #NF, , file
    8. Close #NF
    9. End If
    10. End Sub
    perheps you only need to set the byte pos where to start
    storing the received bytes in the file

    Put #NF, nBytePos, file
    [vbcode]
    SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
    [/vbcode]

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    VB Code:
    1. 'Client Code
    2. Dim NF As Integer
    3. Dim file As String
    4.  
    5. Private Sub Command1_Click()
    6. CommonDialog1.ShowOpen
    7. Text1.Text = CommonDialog1.filename
    8. End Sub
    9.  
    10. Private Sub Command2_Click()
    11. Winsock1.Connect "127.0.0.1", 6000
    12. End Sub
    13.  
    14. Private Sub Command3_Click()
    15. NF = FreeFile
    16. Open CommonDialog1.FileTitle For Binary Access Read Lock Read As #NF
    17. Do While (Loc(NF) < LOF(NF))
    18. DoEvents
    19. file = Input(4096, NF)
    20. If Winsock1.State = sckConnected Then
    21. Winsock1.SendData file
    22. End If
    23. Loop
    24. Close #NF
    25. End Sub
    26.  
    27. 'Server Code
    28. Dim WF As Integer
    29. Dim dat As String
    30. Private Sub Form_Load()
    31. Winsock1.LocalPort = 6000
    32. Winsock1.Listen
    33. End Sub
    34.  
    35. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    36. If Winsock1.State <> sckClosed Then Winsock1.Close
    37. Winsock1.Accept requestID
    38. End Sub
    39.  
    40. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    41. Winsock1.GetData dat, vbString
    42. If received = 1 Then
    43. WF = FreeFile
    44. Open WF_filepath For Binary As #WF 'How do you get the file path from the server?
    45. Put #WF, received, dat
    46. received = received + Len(dat)
    47. If received >= LOF(dat) Then 'How do you get to file size from the server?
    48. Close #WF
    49. End If
    50. End If
    51. End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112
    hi!

    1# when receiving a file you should only open it one time
    2# g_PutByte = g_PutByte + bytesTotal so that you start writing at the end of the new file.
    3# when all of the bytes is reveived then close the file.


    to know the size of the file you must get the file size on the client
    side and send it to the server ( this is how I did it, maybe there is a better way )

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Winsock1.GetData dat, vbString
    If received = 1 Then
    WF = FreeFile
    Open WF_filepath For Binary As #WF 'How do you get the file path from the server?
    Put #WF, received, dat
    received = received + Len(dat)
    If received >= LOF(dat) Then 'How do you get to file size from the server?
    Close #WF
    End If
    End If
    End Sub
    in your example you only open the file and put tada in it if received = 1 you must put the data in the new file on every
    DataArrival.

    ex:
    1# Put #WF, 1, dat
    2# Put #WF, 4097, dat
    3# Put #wf, 8193, dat
    untill the whole file is received.
    [vbcode]
    SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
    [/vbcode]

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    VB Code:
    1. 'Server
    2. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    3. Winsock1.GetData dat, vbString
    4. WF = FreeFile
    5. received = 1
    6. Do Until Len(dat)
    7. Open App.Path & "\test.jpg" For Binary As #WF
    8. Put #WF, received, dat
    9. received = received + Len(dat)
    10. Loop
    11. If received >= Len(dat) Then
    12. Close #WF
    13. End If
    14. End Sub

    It doesn't send all the data before closing the file. I put a loop so it would keep writing the data to the file until the file is the correct size.
    Last edited by Nightwalker83; Mar 30th, 2003 at 06:21 PM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112
    try to send pakets of 4096 bytes

    VB Code:
    1. Dim filenum As Integer
    2.     Dim var_data As String
    3.     filenum = FreeFile
    4.     Open g_FilePath For Binary Access Read Lock Read As #filenum
    5.         Do While (Loc(filenum) < LOF(filenum))
    6.             DoEvents
    7.             var_data = Input(4096, #filenum)
    8.             If Winsock1.State = 7 Then
    9.                 Winsock1.SendData var_data
    10.             End If
    11.         Loop
    12.     Close #filenum

    you should not have to use the loop in the dataarrival sub..
    And you cant open the file on each dataArrival but only close
    it once..!
    And if you shoose to open the file on each DataArrival it will slow
    down the transfer..
    [vbcode]
    SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
    [/vbcode]

  7. #7

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    VB Code:
    1. 'Client Code
    2. Dim NF As Integer
    3. Dim file As String
    4.  
    5. Private Sub Command1_Click()
    6. CommonDialog1.ShowOpen
    7. Text1.Text = CommonDialog1.FileName
    8. End Sub
    9.  
    10. Private Sub Command2_Click()
    11. Winsock1.Connect "127.0.0.1", 6000
    12. End Sub
    13.  
    14. Private Sub Command3_Click()
    15. NF = FreeFile
    16. Open CommonDialog1.FileTitle For Binary Access Read Lock Read As #NF
    17. Do While (Loc(NF) < LOF(NF))
    18. DoEvents
    19. file = Input(4096, NF)
    20. If Winsock1.State = sckConnected Then
    21. Winsock1.SendData file
    22. End If
    23. Loop
    24. Close #NF
    25. End Sub
    26.  
    27. 'Server Code
    28.  
    29. Dim WF As Integer
    30. Dim dat As String
    31. Private Sub Form_Load()
    32. Winsock1.LocalPort = 6000
    33. Winsock1.Listen
    34. End Sub
    35.  
    36. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    37. If Winsock1.State <> sckClosed Then Winsock1.Close
    38. Winsock1.Accept requestID
    39. End Sub
    40.  
    41. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    42. Winsock1.GetData dat, vbString
    43. If received = 1 Then
    44. WF = FreeFile
    45. Open App.Path & "\Test.jpg" For Binary As #WF
    46. End If
    47. Put #WF, received, dat
    48. received = received + Len(dat)
    49. If received >= Len(dat) Then
    50. Close #WF
    51. End If
    52. End Sub

    How can I be sure it is sending data all the time? I tried the above code and I get a message on the server saying:
    Bad file name or number
    I tried to fix the problem by putting:
    VB Code:
    1. received = 1

    Before the first if statement in the data arrival event but when I send files the output size on the server end is alway 4096bytes.

    That is for every file not just the files that are suppose to be that size.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112
    hi!

    received needs to be a global variable
    and it sould be set to 1 before the start of
    receiving a file...

    try this..

    VB Code:
    1. 'Server Code
    2.  
    3. Option Explicit ' new line
    4. Private received As long ' new line
    5.  
    6. Dim WF As Integer
    7. Dim dat As String
    8. Private Sub Form_Load()
    9. Winsock1.LocalPort = 6000
    10. Winsock1.Listen
    11.  
    12. received =1 ' new line
    13.  
    14. End Sub
    15.  
    16. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    17. If Winsock1.State <> sckClosed Then Winsock1.Close
    18. Winsock1.Accept requestID
    19. End Sub
    20.  
    21. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    22. Winsock1.GetData dat, vbString
    23. If received = 1 Then
    24. WF = FreeFile
    25. Open App.Path & "\Test.jpg" For Binary As #WF
    26. End If
    27. Put #WF, received, dat
    28. received = received + Len(dat)
    29. If received >= Len(dat) Then
    30. Close #WF
    31. End If
    32. End Sub
    [vbcode]
    SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
    [/vbcode]

  9. #9

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by meteor
    hi!

    received needs to be a global variable
    and it sould be set to 1 before the start of
    receiving a file...

    try this..

    VB Code:
    1. 'Server Code
    2.  
    3. Option Explicit ' new line
    4. Private received As long ' new line
    5.  
    6. Dim WF As Integer
    7. Dim dat As String
    8. Private Sub Form_Load()
    9. Winsock1.LocalPort = 6000
    10. Winsock1.Listen
    11.  
    12. received =1 ' new line
    13.  
    14. End Sub
    15.  
    16. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    17. If Winsock1.State <> sckClosed Then Winsock1.Close
    18. Winsock1.Accept requestID
    19. End Sub
    20.  
    21. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    22. Winsock1.GetData dat, vbString
    23. If received = 1 Then
    24. WF = FreeFile
    25. Open App.Path & "\Test.jpg" For Binary As #WF
    26. End If
    27. [color=red]Put #WF, received, dat[/color]
    28. received = received + Len(dat)
    29. If received >= Len(dat) Then
    30. Close #WF
    31. End If
    32. End Sub
    I still get this error on the highlighted line.

    Bad file name or number
    Sorry if I am annoying you with this but I have no experience with the data transfer client/server application.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112
    hi!

    well!!! it might me because you only open the file one (correct)
    but you close the file on each dataarrival..

    If received >= inFileSize Then
    Close #WF
    End If

    here is the full working source code.. try it and compare it with
    your own...

    VB Code:
    1. 'Client Code
    2.  
    3. 'APIs to get the file size..
    4. Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    5. Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
    6. Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
    7. Private Const OF_READ = &H0&
    8.  
    9. Private Sub Command2_Click()
    10.     Winsock1.Connect "xxx.xxx.xxx.xxx", 6000
    11. End Sub
    12.  
    13. Private Sub Command3_Click()
    14.     Dim strFileName As String
    15.     Dim strFilePath As String
    16.     strFilePath = "c:\tmp\anything.exe"
    17.     strFileName = "anything.exe"
    18.    
    19.     Dim lFileSize As Long
    20.     lFileSize = FileSize(strFilePath)
    21.  
    22.     ' Send the size of the file to the server.
    23.     ' Send the file name to the server.
    24.     If Winsock1.State = sckConnected Then
    25.         Winsock1.SendData "FILESIZE=" & lFileSize
    26.         DoEvents
    27.         Winsock1.SendData "FILENAME=" & strFileName
    28.         DoEvents
    29.     End If
    30.  
    31.     NF = FreeFile
    32.     Open strFilePath For Binary Access Read Lock Read As #NF
    33.         Do While (Loc(NF) < LOF(NF))
    34.             DoEvents
    35.             file = Input(4096, NF)
    36.             If Winsock1.State = sckConnected Then
    37.                 Winsock1.SendData file
    38.             End If
    39.         Loop
    40.     Close #NF
    41. End Sub
    42.  
    43. Private Function FileSize(ByVal strSrc As String) As Long
    44.     Dim Pointer As Long, sizeofthefile As Long, lpFSHigh
    45.     Pointer = lOpen(strSrc, OF_READ)
    46.     'size of the file
    47.     sizeofthefile = GetFileSize(Pointer, lpFSHigh)
    48.     lclose Pointer
    49.     FileSize = sizeofthefile
    50. End Function
    51.  
    52. Private Sub Timer1_Timer()
    53. Label1.Caption = Winsock1.State
    54. End Sub
    55.  
    56. 'Server Code
    57.  
    58. Private WF As Integer
    59. Private dat As String
    60. Private received As Long
    61. Private g_strFileName As String
    62. Private g_lFileSize As Long
    63. Private g_strInPath As String
    64.  
    65. Private Sub Form_Load()
    66.     Winsock1.LocalPort = 6000
    67.     Winsock1.Listen
    68.    
    69.     g_strInPath = "c:\incoming\" ' where to store the file..
    70.     received = 1
    71. End Sub
    72.  
    73. Private Sub Timer1_Timer()
    74. Label1.Caption = Winsock1.State
    75. End Sub
    76.  
    77. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    78.     If Winsock1.State <> sckClosed Then Winsock1.Close
    79.     Winsock1.Accept requestID
    80. End Sub
    81.  
    82. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    83.     Winsock1.GetData dat, vbString
    84.    
    85.     Dim pos As Integer
    86.    
    87.     If Left(dat, 8) = "FILESIZE" Then
    88.         pos = InStr(1, dat, "=")
    89.         g_lFileSize = Right(dat, Len(dat) - pos)
    90.         Exit Sub
    91.     ElseIf Left(dat, 8) = "FILENAME" Then
    92.         pos = InStr(1, dat, "=")
    93.         g_strFileName = Right(dat, Len(dat) - pos)
    94.         Exit Sub
    95.     End If
    96.  
    97.     If received = 1 Then
    98. Debug.Print "OPEN = " & g_strInPath & g_strFileName
    99.         WF = FreeFile
    100.         Open g_strInPath & g_strFileName For Binary As #WF
    101.     End If
    102.    
    103.     Put #WF, received, dat
    104.     received = received + Len(dat)
    105. Debug.Print received
    106.     If received >= g_lFileSize Then
    107. Debug.Print "CLOSE = " & g_strInPath & g_strFileName
    108.         Close #WF
    109.     End If
    110.  
    111.  
    112. End Sub

    Hope this helps!!
    Last edited by meteor; Apr 7th, 2003 at 02:06 AM.
    [vbcode]
    SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
    [/vbcode]

  11. #11

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    VB Code:
    1. 'Add a reference to MicrosoftScriptingRuntimes
    2. 'Client
    3. Option Explicit
    4. Dim FSO As New FileSystemObject
    5. Dim F As file
    6. Dim nf As Integer
    7. Dim fname As String
    8. Dim fsize As Long
    9. Dim file As String
    10.  
    11. Private Sub Command1_Click()
    12. CommonDialog1.ShowOpen
    13. End Sub
    14.  
    15. Private Sub Command2_Click()
    16. Winsock1.Connect "127.0.0.1", 6000
    17. End Sub
    18.  
    19. Private Sub Command3_Click()
    20. On Error Resume Next
    21. nf = FreeFile
    22. Open CommonDialog1.FileName For Binary Access Read Lock Read As #nf
    23. Set F = FSO.GetFile(CommonDialog1.FileName)
    24. If Winsock1.State = sckConnected Then
    25. fname = F.Name
    26. fsize = F.Size
    27. Winsock1.SendData "|FNAME|" & fname
    28. DoEvents
    29. Do While (Loc(nf) < LOF(nf))
    30. DoEvents
    31. file = Input(4096, nf)
    32. Winsock1.SendData "|FILE|" & file
    33. Loop
    34. Set F = Nothing
    35. Close #nf
    36. If Winsock1.State = sckConnected Then
    37. Winsock1.SendData "|FSIZE|" & fsize
    38. DoEvents
    39. End If
    40. End If
    41. End Sub
    42.  
    43. 'Server
    44. Option Explicit
    45. Dim WF As Integer
    46. Dim dat As String
    47. Dim fname As String
    48. Dim fsize As Long
    49. Dim file As String
    50. Private received As Long
    51. Private Sub Form_Load()
    52. Winsock1.LocalPort = 6000
    53. Winsock1.Listen
    54. received = 1
    55. End Sub
    56.  
    57.  
    58. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    59. If Winsock1.State <> sckClosed Then Winsock1.Close
    60. Winsock1.Accept requestID
    61. End Sub
    62.  
    63. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    64. On Error Resume Next
    65. Winsock1.GetData dat, vbString
    66. If InStr(1, dat, "|FNAME|") <> 0 Then
    67. fname = Mid$(dat, 8, Len(dat))
    68. If received = 1 Then
    69. WF = FreeFile
    70. Open App.Path & "\" & fname For Binary As #WF
    71. End If
    72. End If
    73. If InStr(1, dat, "|FILE|") <> 0 Then
    74. file = Mid$(dat, 7, Len(dat))
    75. Put #WF, received, file
    76. received = received + Len(file)
    77. End If
    78. If InStr(1, dat, "|FSIZE|") <> 0 Then
    79. fsize = Mid$(dat, 10, Len(dat))
    80. If received >= fsize Then
    81. Close #WF
    82. received = 1 'Must be set back to one  after each file transfer
    83. End If
    84. End If
    85. End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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