CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Bad file name or number error in VB.Net

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Bad file name or number error in VB.Net

    I have a form with two objects. One is of type MessageStore and the other NetListener, these are custom classes. NetListener listens for incoming messages from the network (this works fine). The MessageStore class works with a disk file opened using FileOpen.

    What I want to do is when a new message arrives, to store it in the MessageStore. My code works as follows.

    1. When a message arrives, my NetListener object raises an event, which is handled by a Sub in the main form.

    2. From this event handler, when I try MessageStore.AddMessage(Msg) I get a "Bad file name or number" error in the AddMessage sub in the MessageStore class. The file number is 1 (same as when the file was opened and read from - file was not closed) but I get this error.

    Calling MessageStore.AddMessage outside this even handler works without any error.

    Any ideas as to why this does not work?


    VB.Net 2005 (.Net 2.0)

  2. #2
    Join Date
    Oct 2006
    Posts
    2

    Sample Code

    I have the following classes:

    Public Class InstantMessage - Defines a message structure (not included)

    Code:
    Public Class MessageStore  'stores InstantMessage objects
    
    
    #Region "Message Store Public Methods"
    
    ' Constructor
    
    Public Sub New(ByVal FilePath As String)
    
    Open(FilePath)
    
    End Sub
    
    
    
    ' Creates or Opens an existing Message Store at the given path
    
    Public Sub Open(ByVal FilePath As String)
    
    ' Clear the Array List and other variables
    
    _MsgList.Clear()
    
    _MsgCount = 0
    
    _DeletedMsgCount = 0
    
    _LastMsgID = 0
    
    ' Open the message store
    
    OpenFile(FilePath)
    
    End Sub
    
    
    
    ' Adds an InstantMessage to the Message Store 
    
    Public Overridable Sub AddMessage(ByVal msg As InstantMessage)
    
    ' Increment the last message ID and set the ID for the message
    
    _LastMsgID += 1
    
    msg.MsgID = _LastMsgID
    
    ' Save the message to the disk file
    
    AddToFile(msg)
    
    ' Add the message to the ArrayList after saving the file
    
    ' to obtain the position of the record and message in the file.
    
    ' Remove the message part of the record from the ArrayList
    
    msg.Message = ""
    
    _MsgList.Add(msg)
    
    _MsgCount += 1
    
    End Sub
    
    #End Region
    
    
    
    #Region "Message Store Private Methods"
    
    ' Opens a disk file to store and retrieve InstantMessages
    
    Private Sub OpenFile(ByVal FilePath As String)
    
    Dim I As Integer
    
    ' Get a free file number and open the file specified in FilePath
    
    _FileNum = FreeFile()
    
    FileOpen(_FileNum, FilePath, OpenMode.Binary)
    
    ' Check if the file is empty, otherwise get the number of messages
    
    If LOF(_FileNum) <> 0 Then
    
    FileGet(_FileNum, _MsgCount)
    
    FileGet(_FileNum, _DeletedMsgCount)
    
    Else
    
    ' Create a new file structure
    
    FilePut(_FileNum, _MsgCount)
    
    FilePut(_FileNum, _DeletedMsgCount)
    
    End If
    
    ' Get details of all messages in the store and store them in
    
    ' the ArrayList _MsgList
    
    For I = 1 To _MsgCount
    
    Dim Message As New InstantMessage
    
    GetMessageHeaderFromFile(Message)
    
    _LastMsgID = Message.MsgID
    
    ' Do not allow the ArrayList to have deleted messages.
    
    If Not Message.Deleted Then
    
    _MsgList.Add(Message)
    
    Else
    
    ' Distroy the message object
    
    Message = Nothing
    
    _DeletedMsgCount += 1
    
    End If
    
    Next
    
    End Sub
    
    
    
    ' Adds an Instant Message to the Message Store on disk
    
    Private Sub AddToFile(ByRef msg As InstantMessage)
    
    'Dim TmpPos As Integer
    
    ' Goto the end of the file and add the new message
    
    ' Check if the file is empty
    
    If LOF(_FileNum) > 0 Then
    
    Seek(_FileNum, LOF(_FileNum) + 1)
    
    End If
    
    ' Get the position of the record in the disk file
    
    msg.RecordStartPos = Seek(_FileNum)
    
    FilePut(_FileNum, msg.MsgID)
    
    ' Save the length of the HostName and then the HostName
    
    FilePut(_FileNum, msg.HostName.Trim.Length)
    
    FilePut(_FileNum, msg.HostName.Trim)
    
    ' Save the length of the UserName and then the UserName
    
    FilePut(_FileNum, msg.UserName.Trim.Length)
    
    FilePut(_FileNum, msg.UserName.Trim)
    
    FilePut(_FileNum, msg.MsgDateTime)
    
    FilePut(_FileNum, msg.Priority)
    
    ' Save the length of the Subject and then the Subject
    
    FilePut(_FileNum, msg.Subject.Trim.Length)
    
    FilePut(_FileNum, msg.Subject.Trim)
    
    ' Get the position of the start of the message in the disk file
    
    msg.MessageStartPos = Seek(_FileNum)
    
    msg.Size = msg.Message.Trim.Length * 2
    
    ' Save the length of the Message and then the Message
    
    FilePut(_FileNum, msg.Message.Trim.Length)
    
    FilePut(_FileNum, msg.Message.Trim)
    
    FilePut(_FileNum, msg.Read)
    
    FilePut(_FileNum, msg.Deleted)
    
    End Sub
    
    #End Region
    
    
    
    Public Class NetworkListener ' Listens to the network
    
    Public Class NetworkListener
    
    
    
    
    
    ' Public Events
    
    Public Event IncomingMessage(ByVal Message As InstantMessage)
    
    ' Private Events
    
    Private WithEvents Timer As New Timers.Timer(100)
    
    ' Private variables
    
    Private TCPListener As Sockets.TcpListener
    
    Private ListenerStarted As Boolean = False
    
    
    
    #Region "Listener Public Properties"
    
    ' Returns the whether the Listener is started
    
    Public ReadOnly Property IsRunning() As Boolean
    
    Get
    
    Return ListenerStarted
    
    End Get
    
    End Property
    
    #End Region
    
    
    
    #Region "Listener Private Methods"
    
    ' Initialise the Listener
    
    Private Sub InitialiseListener(ByVal Port As Integer)
    
    'LIP = GetLocalIP()
    
    TCPListener = New Sockets.TcpListener(IPAddress.Any, Port)
    
    End Sub
    
    
    
    ' Check for pending connections and process them
    
    ' This sub is called every Timer tick and checks if there are connections pending, 
    
    ' if so a thread is started to process 1 request.
    
    Private Sub CheckForPendingRequests(ByVal o As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
    
    If TCPListener.Pending Then
    
    Timer.Enabled = False
    
    Dim ProcessRequest As New Threading.Thread(AddressOf ProcessConnectionRequest)
    
    ProcessRequest.Start()
    
    Timer.Enabled = True
    
    End If
    
    End Sub
    
    
    
    ' This sub processes each connection request (runs in a new thread)
    
    Private Sub ProcessConnectionRequest()
    
    ' Socket returned by the TCPListener
    
    Dim ClientConnection As System.Net.Sockets.TcpClient
    
    Dim Msg As New InstantMessage
    
    Dim LenBuffer(3) As Byte
    
    Dim MessageLength As Integer
    
    Dim Buffer() As Byte
    
    Dim MessageChars() As Char
    
    Dim strMessage As String
    
    Try
    
    ClientConnection = TCPListener.AcceptTcpClient
    
    ClientConnection.ReceiveTimeout = 10000
    
    ' Get the length of the message
    
    ClientConnection.GetStream.Read(LenBuffer, 0, LenBuffer.Length)
    
    MessageLength = BitConverter.ToInt32(LenBuffer, 0)
    
    ' Get the message
    
    ReDim Buffer(MessageLength - 1)
    
    ClientConnection.GetStream.Read(Buffer, 0, Buffer.Length)
    
    MessageChars = System.Text.Encoding.Default.GetChars(Buffer)
    
    strMessage = New String(MessageChars, 0, MessageLength)
    
    ' Decode the received message
    
    If strMessage.StartsWith("MESSAGE:") Then
    
    DecodeEncodedMessage(strMessage.Substring(8), Msg)
    
    End If
    
    Catch ex As Exception
    
    ClientConnection.Close()
    
    Throw New Exception("Connection error")
    
    'RaiseEvent DiagnosticMessage("Connection error")
    
    End Try
    
    ' Return the message
    
    ClientConnection.Close()
    
    RaiseEvent IncomingMessage(Msg)
    
    
    
    End Sub
    
    #End Region
    
    
    
    #Region "Listener Public Methods"
    
    ' Constructor
    
    Public Sub New(ByVal Port As Integer, Optional ByVal AutoStart As Boolean = False)
    
    InitialiseListener(Port)
    
    If AutoStart Then StartListener()
    
    End Sub
    
    ' Starts the Listener
    
    Public Sub StartListener()
    
    If Not ListenerStarted Then
    
    TCPListener.Start()
    
    Timer.Start()
    
    Timer.Enabled = True
    
    ListenerStarted = True
    
    End If
    
    End Sub
    
    ' Stops the Listener
    
    Public Sub StopListener()
    
    If ListenerStarted Then
    
    Timer.Enabled = False
    
    Timer.Stop()
    
    TCPListener.Stop()
    
    ListenerStarted = False
    
    End If
    
    End Sub
    
    #End Region
    
    End Class
    
    
    
    
    
    Module Common ' Public variables are defined here
    
    ' Define Message Stores
    
    Public Inbox As MessageStore
    
    Public Sub InitialiseMessageStores()
    
    ' Open the message stores
    
    Inbox = New MessageStore(Application.StartupPath & "\Inbox.box")
    
    End Sub
    
    
    
    Then the actual event handler that causes the error
    
    Public Class MainForm  ' Windows form that starts the app
    
    Private WithEvents NetListener As New NetworkListener(SystemPort)
    
    
    
    ' Handles incoming messages returned by the listerner NetListener
    
    Public Sub NetListener_IncomingMessage(ByVal Message As InstantMessage) Handles NetListener.IncomingMessage
    
    Inbox.AddMessage(Message)
    
    End Sub
    
    End Class


    The code is very long and I have removed most of the other code.
    Last edited by HanneSThEGreaT; October 28th, 2006 at 03:46 AM. Reason: Added Code tags

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)