Results 1 to 4 of 4

Thread: Socket file corruption [RESLVD]

  1. #1

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Socket file corruption [RESLVD]

    Allright, this is my first attempt at sending a file over winsock ... heres my code

    CLIENT
    Code:
    Private Sub sendPicture(strFileName As String)
    
     Dim picMain As New StdPicture
    
        Dim fileLEN As Long
        Dim byteChunk(999) As Byte
        
    
        Open strFileName For Binary As #1
        
        sckSocket.SendData "[FIS]" & LOF(1)
        
        fileLEN = LOF(1)
        
        While (fileLEN >= 0)
        
        DoEvents
            Get 1, , byteChunk()
            fileLEN = fileLEN - 1000
            
            If (fileLEN > 0) Then
            
                sckSocket.SendData sndChunk()
                sckSocket.SendData byteChunk()
            
            Else
                
                sckSocket.SendData endChunk()
                sckSocket.SendData byteChunk()
                
            
            End If
        Wend
        Close #1
     
        MsgBox "Send complete!"
    End Sub

    SERVER
    Code:
            
        If (Left(Data, 5) = "[FIS]") Then
        
            Data = cutLData(Data, 5)
            getFileIndex = 0
            totalSize = Data
            ReDim getFile(0 To (totalSize + 2000)) 'need fix +2000, keeps giving (out of bound error)
               
            With frmVote 'ignore me, display only
                
                .Show
                .frameVote.Visible = False
                .frameGetFile.Visible = True
                .lblTotalSize.Caption = "Total size: " & Val(Data)
            End With
           
            
            
        ElseIf (Left(Data, 5) = "[FIL]") Then
        
            Data = cutLData(Data, 5)
            
    
            For i = 1 To Len(Data) - 1
                getFile(getFileIndex) = Asc(Mid(Data, i, 1))
                getFileIndex = getFileIndex + 1
            Next i
            
            
            frmVote.lblGotSize = "Got: " & getFileIndex & " bytes"
            
            
        ElseIf (Left(Data, 5) = "[FIE]") Then
            
            Dim intTrim As Integer
            
            intTrim = totalSize - getFileIndex
            Data = cutLData(Data, 5)
            Trim Data
            
            For i = 1 To Len(Data) - 1 - (intTrim) '(OOB) here, ***?
                getFile(getFileIndex) = Asc(Mid(Data, i, 1))
                getFileIndex = getFileIndex + 1
            Next i
            
            Open "c:\test.jpg" For Binary As #2
            Put #2, , getFile
            Close #2
            frmVote.lblGotSize = "Got: " & getFileIndex & " bytes (done!)"
        
        End If
    When i get the picture on the other side, its really corrupt... I mean the effect is cool and all but its not what I'm going for

    What am i doing wrong, AND am i doing this really inefficently?
    I tried to poke around the forum for sending around files but didnt really come accross anything good so i came up with my own way of doing it...

    some variables are declared globaly, so just assume they work.

    Any help would be good, thanks!
    Last edited by invitro; Mar 27th, 2004 at 08:56 PM.
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

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

  3. #3

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547
    hmmm, interesting... it looks to me that you dont need to use bytes at all... just accept it as a string and write it out to a binary file. That would solve my array problem... the code seems kind of messy, to me... so im going to rewrite my code and post it on here for anyone else to use

    thanks for the reply
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  4. #4

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547
    Ok, for much much simplified data sending... heres my code for those of you who are curious. What it does is take chunks of 1000bytes and sends em over, no biggie.... it works i just tested it.
    Oh sorry cutLData is a function that cutes off 5 bytes from the left... just makes it easier oppose to using left all the time... yeah yeah im anal i know


    CLIENT
    Code:
    Private Sub sendPicture(strFileName As String)
    
     Dim picMain As New StdPicture
    
        Dim fileLEN As Long
        Dim byteChunk As String * 1000
        
    
        Open strFileName For Binary As #1
        
        sckSocket.SendData "[FIS]" & LOF(1)
        
        While (Not EOF(1))
        
        DoEvents
            Get 1, , byteChunk
              
                sckSocket.SendData "[FIL]" & byteChunk
    
            
        Wend
        Close 1
         MsgBox "Send complete!"
    End Sub

    SERVER
    Code:
    If (Left(Data, 5) = "[FIS]") Then
        
            Data = cutLData(Data, 5)
            getFileIndex = 0
            totalSize = Data
               
            With frmVote
                .Show
                .frameVote.Visible = False
                .frameGetFile.Visible = True
                .lblTotalSize.Caption = "Total size: " & Val(Data)
            End With
           
            
            
        ElseIf (Left(Data, 5) = "[FIL]") Then
        
            Data = cutLData(Data, 5)
    
            
                getFile = getFile & Data
                getFileIndex = getFileIndex + Len(Data)
            
            
            
            frmVote.lblGotSize = "Got: " & getFileIndex & " bytes"
            
            If (getFileIndex >= totalSize) Then
                
                Open "C:\test.jpg" For Binary As #2
                
                    Put 2, , getFile
                
                Close #2
            
            
            End If
            
        End If
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

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