CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2014
    Posts
    3

    please help to solve this error in my code

    Hello all ;

    each time Im running this code i have an error saying : bad file name or number . and it referred to this command : Put Clients(Index).FileNum, , sData .

    the code :
    Code:
    Option Explicit
    Dim FNamwx As String
    
    Private Type tClient
        FileName As String
        FileSize As Long
        BytesReceived As Long
        
        FileNum As Integer
    End Type
    
    Private Clients() As tClient
    
    Private Sub cmdConnect_Click()
        If cmdConnect.Caption = "Start Listening for Connections" Then
            SckReceiveFile(0).LocalPort = Val(Me.txtListenPort.Text)
            SckReceiveFile(0).Listen
            
            Label2.Caption = "IP: " & SckReceiveFile(0).LocalIP
            cmdConnect.Caption = "Stop Listening for Connections"
        Else
            SckReceiveFile(0).Close
            cmdConnect.Caption = "Start Listening for Connections"
        End If
    End Sub
    
    
    Private Sub Form_Load()
        lstConnections.ListItems.Add , , "0"
        
    End Sub
    
    Private Sub lstConnections_BeforeLabelEdit(Cancel As Integer)
        Cancel = 1
    End Sub
    
    Private Sub SckReceiveFile_Close(Index As Integer)
        On Error Resume Next
        
        SckReceiveFile(Index).Close
        
        Close Clients(Index).FileNum
        
        If Clients(Index).BytesReceived < Clients(Index).FileSize Then
            Kill App.Path & "\Images\" & Clients(Index).FileName
            
            Me.lstConnections.ListItems(Index + 1).SubItems(4) = "Incomplete, File Deleted"
        Else
            Me.lstConnections.ListItems(Index + 1).SubItems(4) = "Transfer Complete"
            Image1.Picture = LoadPicture(FNamwx)
            
    '        Call bmp_rotate(Image1, Image2, Pi / 2)
            
            
        End If
        FitTextInListView Me.lstConnections, 4, , Index + 1
        
        Clients(Index).FileNum = 0
        Clients(Index).BytesReceived = 0
        Clients(Index).FileSize = 0
        Clients(Index).FileName = ""
    End Sub
    
    Private Sub FitTextInListView(LV As ListView, ByVal Column As Integer, Optional ByVal Text As String, Optional ByVal ItemIndex As Long = -1)
        Dim TLen As Single, CapLen As Single
        
        CapLen = Me.TextWidth(LV.ColumnHeaders(Column + 1).Text) + 195
        
        If ItemIndex >= 0 Then
            If ItemIndex = 0 Then
                TLen = Me.TextWidth(LV.ListItems(ItemIndex).Text)
            Else
                TLen = Me.TextWidth(LV.ListItems(ItemIndex).SubItems(Column))
            End If
        Else
            TLen = Me.TextWidth(Text)
        End If
        
        TLen = TLen + 195
        
        If CapLen > TLen Then TLen = CapLen
        
        If LV.ColumnHeaders(Column + 1).Width < TLen Then LV.ColumnHeaders(Column + 1).Width = TLen
    End Sub
    
    Private Sub SckReceiveFile_ConnectionRequest(Index As Integer, ByVal requestID As Long)
        Dim K As Integer, LI As ListItem
        
        For K = 1 To SckReceiveFile.UBound
            If SckReceiveFile(K).State = sckClosed Then Exit For
        Next K
        
        If K = SckReceiveFile.UBound + 1 Then
            Load SckReceiveFile(SckReceiveFile.UBound + 1)
            ReDim Preserve Clients(SckReceiveFile.UBound)
            
            K = SckReceiveFile.UBound
            lstConnections.ListItems.Add , , CStr(K)
        End If
        
        SckReceiveFile(K).Accept requestID
        
        If Len(SckReceiveFile(K).RemoteHost) = 0 Then
            Me.lstConnections.ListItems(K + 1).SubItems(2) = SckReceiveFile(K).RemoteHostIP
        Else
            Me.lstConnections.ListItems(K + 1).SubItems(2) = SckReceiveFile(K).RemoteHost
        End If
        
        FitTextInListView Me.lstConnections, 2, , K + 1
    End Sub
    
    Private Sub SckReceiveFile_DataArrival(Index As Integer, ByVal bytesTotal As Long)
        Dim sData As String
        Dim Pos As Long
        
        SckReceiveFile(Index).GetData sData, vbString
            
        If Len(sData) > 0 And Len(sData) < 20 Then
            Pos = Val(sData)
            Me.lstConnections.ListItems(Index).SubItems(3) = Pos
            
            Clients(Index).FileName = "MyIm" & Hour(Time) & Minute(Time) & Second(Time) & ".jpg"
            Clients(Index).FileSize = Pos
            Clients(Index).FileNum = FreeFile
            FNamwx = App.Path & "\Images\" & Clients(Index).FileName
            Me.lstConnections.ListItems(Index + 1).SubItems(3) = Clients(Index).FileName
            FitTextInListView Me.lstConnections, 3, , Index + 1
            
            Open FNamwx For Binary Access Write Lock Write As Clients(Index).FileNum
    
       Else
    
            Clients(Index).BytesReceived = Clients(Index).BytesReceived + Len(sData)
            Put Clients(Index).FileNum, , sData
    
            Me.lstConnections.ListItems(Index + 1).SubItems(4) = Format(Clients(Index).BytesReceived / Clients(Index).FileSize * 100#, "#0.00") & " %"
            FitTextInListView Me.lstConnections, 4, , Index + 1
    
            If Clients(Index).BytesReceived >= Clients(Index).FileSize Then
                SckReceiveFile_Close Index
            End If
        
        End If
    
    End Sub
    
    Private Sub tmrStatus_Timer()
        Dim K As Long, TmpStr As String
        
        For K = 0 To SckReceiveFile.UBound
            TmpStr = Choose(SckReceiveFile(K).State + 1, "Closed", "Open", "Listening", "Connection pending", "Resolving host", "Host resolved", "Connecting", "Connected", "Server is disconnecting", "Error")
            
            If Me.lstConnections.ListItems(K + 1).SubItems(1) <> TmpStr Then
                Me.lstConnections.ListItems(K + 1).SubItems(1) = TmpStr
                FitTextInListView Me.lstConnections, 1, , K + 1
            End If
        Next K
    End Sub
    
    Private Sub txtListenPort_Validate(Cancel As Boolean)
        txtListenPort.Text = Val(txtListenPort.Text)
    End Sub
    
    Public Sub Delay(ByVal interval As Integer)
        
        Dim currentTime1, targetTime1 As Date
        targetTime1 = Now + (interval)  ' converting intervals to seconds and adding to currenttime.
        currentTime1 = Now
        While currentTime1 < targetTime1
            DoEvents
            currentTime1 = Now
        Wend
        ' Delay introduced for "interval" seconds :P
    
    End Sub
    Last edited by DataMiser; April 18th, 2014 at 05:28 PM. Reason: added code tags

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: please help to solve this error in my code

    Is the file getting opened and filenum assigned? I see that code is in an else clause so it could possibly be executing before the filenum has been assigned and before the file is opened.

    Have you stepped through the code to see what is happening?

    On a side note you should not use yellow to highlight text, makes it pretty much impossible to read
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Apr 2014
    Posts
    3

    Re: please help to solve this error in my code

    Dear datamiser ;

    Actually I'm new vb6 , but if you noticed that i registered filenum
    As integer .

    I tried thousands times to solve it , i thought its a silly problem for
    Experts .

    Thank you

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: please help to solve this error in my code

    Yet you did not address anything I said in my post. I wasn't talking about the Dim statement, I was talking about where you assign the filenum i.e. filenum=freefile that is inside an if test and will only happen when the test is true in the else portion you are using that filenum to write to the file but if the first part is not true first then the else part will execute without a valid file number and you will get an error of invalid file name or number.

    So I'll ask again have you stepped through the code to see what it is actually doing?

    This should always be your first step when trying to find a problem, set a breakpoint then run the program, when it reaches the break point then step through the code to see which lines execute and if need be check the values of some of the variables while you are doing it. This is the single most powerful and useful tool for debugging your application in VB.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Apr 2014
    Posts
    3

    Re: please help to solve this error in my code

    sir, i just use break points to figure put whats going on with the variables, i believe that i receive some of the data and its stored in "sdata" variable, because when i get the error and after assigning a watch on the variable, i get some kind of value in it, but not all of it, i tried to google it out, some say that its related to winsock and its capacity, others say that its related to opening and closing something, can you please help me furthur solving my problem?

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: please help to solve this error in my code

    You said the error is occurring on this line
    Code:
    Put Clients(Index).FileNum, , sData
    The error itself should be pretty easy to understand. Bad File Name or Number means pretty much what it says. Here it is referring to the file number. Most likely because Filenum is not assigned to an open file at the time this line is executed.

    Did you confirm that the file is being opened before this line is executed?
    Did you confirm that .filenum as a valid value when that line is executed?

    How much data is coming in when the data arrival fires?
    Always use [code][/code] tags when posting code.

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

Featured