5 Replies - 1193 Views - Last Post: 07 April 2011 - 08:51 AM Rate Topic: -----

#1 theotherguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-April 11

vb.net p2p chat "InvalidOperationException was unhandled." Err

Posted 07 April 2011 - 07:36 AM

Hello there,

I'm a beginner in VB.net, I'm trying to develop P2P based Chat application from this thread.

The chat application has server side and client side. Serverside items: Form1.vb, BB_Server, RTS_Listener, and RTS_SocketClient. And for Clientside items: Form1.vb, BB_Client, RTS_SocketClient. Below is the code of simple form I tried to build. I'm still trying to establish a connection for the server, so the form only has txtDisplay, txtSend, and btnSend controls.
Public Class Form1
    Dim Server As New BB_Server
    Dim msg As String = txtSend.Text

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Server.StopServer()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Server.StartServer()
        Catch ex As InvalidOperationException
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnSend_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Try
            Server.SendMessage(msg)
            txtSend.Text = ""
        Catch ex As InvalidOperationException
            MsgBox(ex.Message)
        End Try
    End Sub

End Class


Have read the code repeatedly and still hardly to understand it. Also started many attempts to build Form for the classes provided. But I've always ran into the same error whenever I try to run the server side application which is "InvalidOperationException was unhandled." Here is screenshot of the error to provide clearer view.

Posted Image

The build is successful without any error. But when I try to run it, then it always return the same error. I append the BB_Server code below incase you need. Any help really will be appreciated. Thanks.

BB_Server
Imports Serverside.RTS_Listener
Imports System.Net

''' <summary>
''' This is the BB server, fill in properties for full effect 
''' </summary>
''' <remarks></remarks>
Public Class BB_Server
    Inherits Object
    Private WithEvents Server As New RTS_Listener

#Region " Properties "
    Private esAdminUsername As String = "Admin"
    Public Property AdminUsername() As String
        Get
            Return esAdminUsername
        End Get
        Set(ByVal value As String)
            esAdminUsername = value
        End Set
    End Property

    Private esPort As Integer = 8888
    Public Property Port() As Integer
        Get
            Return esPort
        End Get
        Set(ByVal value As Integer)
            esPort = value
        End Set
    End Property

    Private esLog As String = String.Empty
    Public ReadOnly Property Log() As String
        Get
            Return esLog
        End Get
    End Property

    Private esMaxConnections As Integer = 500 '(499 as zero counts as a connection)
    Public Property MaxConnections() As Integer
        Get
            Return esMaxConnections
        End Get
        Set(ByVal value As Integer)
            esMaxConnections = value
        End Set
    End Property

    Private esCurrentConnections As Integer = 0
    Public ReadOnly Property CurrentConnections() As Integer
        Get
            Return esCurrentConnections
        End Get
    End Property

    Private esMaxMessageLength As Integer = 200
    Public Property MaxMessagelength() As Integer
        Get
            Return esMaxMessageLength
        End Get
        Set(ByVal value As Integer)
            esMaxMessageLength = value
        End Set
    End Property

    Private esLogInToTalk As Boolean = True
    Public Property LogInToTalk() As Boolean
        Get
            Return esLogInToTalk
        End Get
        Set(ByVal value As Boolean)
            esLogInToTalk = value
        End Set
    End Property

    Private esServerStatus As _ServerStatus
    Public Property ServerStatus() As _ServerStatus
        Get
            Return esServerStatus
        End Get
        Set(ByVal value As _ServerStatus)
            esServerStatus = value
        End Set
    End Property
#End Region

#Region " Server Commands "
    ''' <summary>
    ''' Starts the server on the dedicated port defualt is, 50000
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub StartServer()
        Server.Listen(Port)
        ServerStatus = _ServerStatus.Running
    End Sub

    ''' <summary>
    ''' Stops and disposes of the current server
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub StopServer()
        ServerStatus = _ServerStatus.Stopping
        Server.Broadcast(Contact.Shutdown)
        Server.Close()
        ServerStatus = _ServerStatus.Stoped
    End Sub

    ''' <summary>
    ''' Sends A message on behalf of you to all clients
    ''' </summary>
    ''' <param name="Message"></param>
    ''' <remarks></remarks>
    Public Sub SendMessage(ByVal Message As String)
        Server.Broadcast(Contact.Normal, AdminUsername & " : " & Message)
    End Sub

    ''' <summary>
    ''' Resets the events log
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ResetLog()
        esLog = String.Empty
    End Sub

    ''' <summary>
    ''' Return the entire log. (Or up till it was last cleared)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ReadLog()
        Return Log
    End Function

    Public Sub BootUser(ByVal Username As String)
        Server.BootUser(Username)
    End Sub
#End Region

#Region " Enums "
    Private Enum Contact As Byte
        Normal = 1
        AdminMessage = 2
        ServerMessage = 3
        Shutdown = 4
        Login = 5
        LoginRejected = 6
    End Enum

    Public Enum _ServerStatus
        Running = 1
        Stoped = 2
        Stopping = 3
    End Enum
#End Region

#Region " Server Defiened Events "


    Private Sub Server_ClientLoggedIn(ByVal Username As String) Handles Server.ClientLoggedIn
        RaiseEvent OnUserLoggedIn(Username)
        Server.Broadcast(Contact.Normal, Username & " has joined the chat.")
    End Sub
    ' We only need these three unless you want to fileshare, which may soon loose
    ' you your internet connection, PERNEMENTLY!!!!!

    Private Sub Server_ConnectionRequest(ByVal Requestor As System.Net.Sockets.TcpClient, ByRef AllowConnection As Boolean) Handles Server.ConnectionRequest
        ' to prevent a buffer overflow or a major blue screen because of lack 
        ' of ram I introduced this fail safe system that will block all 
        ' connections over the specified limit.

        If CurrentConnections >= MaxConnections Then
            AllowConnection = False
        Else
            AllowConnection = True
            RaiseEvent OnConnectionRequest()
            esCurrentConnections = esCurrentConnections + 1
        End If
    End Sub


    Private Sub Server_Disconnect(ByVal Client As RTS_SocketClient) Handles Server.Disconnect
        ' We must remember to remove a connection or our server will get clogged!
        esCurrentConnections = esCurrentConnections - 1
        If Client.Username = "" Then
            RaiseEvent OnClientDisconect()
        Else
            RaiseEvent OnUserDisconnect(Client.Username)
        End If
        Server.Broadcast(Contact.Normal, Client.Username & " has just left the chat.")
    End Sub

    Private Sub Server_MessageReceived(ByVal Client As RTS_SocketClient, ByVal message As Byte) Handles Server.MessageReceived
        RaiseEvent OnMessageRecived(message)
    End Sub

    Private Sub Server_StringReceived(ByVal Client As RTS_SocketClient, ByVal msgTag As Byte, ByVal message As String) Handles Server.StringReceived
        If msgTag = Contact.Normal Then
            If message.Count > MaxMessagelength Then
                Client.Send(Contact.ServerMessage, "Message must not be longer than " & MaxMessagelength & " characters. Yours was " & message.Count & " characters long.")
            Else
                Dim b As String = Server.ReturnUsername(Client)
                If Not b = "" Then
                    Dim c As String = (b & ": " & message)
                    Server.Broadcast(Contact.Normal, c)
                    RaiseEvent OnMessageRecived(c)
                Else
                    If LogInToTalk = False Then
                        Dim c As String = (b & ": " & message)
                        Server.Broadcast(Contact.Normal, c)
                        RaiseEvent OnMessageRecived(c)
                    Else
                        Client.Send(Contact.ServerMessage, "You must login before you can talk.")
                    End If
                End If
            End If
        ElseIf msgTag = Contact.Login Then
            If Server.ValidateLogin(message, AdminUsername, Client) = True Then
                Server.Login(Client, message)
                RaiseEvent OnUserLoggedIn(message)
                Server.Broadcast(Contact.Normal, message & " has joined the chat.")
            Else
                Client.Send(Contact.LoginRejected, "Your username was rejected.")
            End If
        Else
            ' We should only be getting messages with the normal tag, otherwise
            ' Our server may of been comprimised / someone has got a rouge app
            ' in the conversation.
            Exit Sub
        End If
    End Sub
#End Region

#Region " Events "
    Public Event OnConnectionRequest()
    Public Event OnClientDisconect()
    Public Event OnMessageRecived(ByVal Message As String)
    Public Event OnUserLoggedIn(ByVal Username As String)
    Public Event OnUserDisconnect(ByVal Username As String)
#End Region
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: vb.net p2p chat "InvalidOperationException was unhandled." Err

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: vb.net p2p chat "InvalidOperationException was unhandled." Err

Posted 07 April 2011 - 07:41 AM

What line is that happening on? An 'object reference not set to an instance of an object' means you have a variable that is "nothing". It means you need to instantiate it - make it something! Usually this is accomplished by doing the "new". This sets up the memory for the object and it can be interacted with.

dim <object name> as new <object type>


or

dim <object name> as <object type> = new <object type>

Was This Post Helpful? 0
  • +
  • -

#3 theotherguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-April 11

Re: vb.net p2p chat "InvalidOperationException was unhandled." Err

Posted 07 April 2011 - 07:59 AM

View Postmodi123_1, on 07 April 2011 - 07:41 AM, said:

What line is that happening on? An 'object reference not set to an instance of an object' means you have a variable that is "nothing". It means you need to instantiate it - make it something! Usually this is accomplished by doing the "new". This sets up the memory for the object and it can be interacted with.

dim <object name> as new <object type>


or

dim <object name> as <object type> = new <object type>


Hello, thanks for fast reply. The compiler doesn't tell which line it is happening, that's the biggest problem :( . And in Form1.vb, I have created New object for BB_Server class. I don't have idea where else to look at. :(
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: vb.net p2p chat "InvalidOperationException was unhandled." Err

Posted 07 April 2011 - 08:10 AM

Interesting. Well here's a new experience for you - breakpoints and debugging! Start with the first line to execute in your app, right click -> breakpoints -> insert break point. Now when you run your code it should stop right there... then use the 'next line' button at the toolbar (or F10 if you have kept the default step keybinding). From there walk through your code until you find the line that blows up! This is a skill you will use over and over so better to learn it now!
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1029
  • View blog
  • Posts: 4,007
  • Joined: 02-July 08

Re: vb.net p2p chat "InvalidOperationException was unhandled." Err

Posted 07 April 2011 - 08:40 AM

It's probably line 3 where you declare.
Dim msg As String = txtSend.Text


This object is not ready, and it is empty here anyway. Set it somewhere where it would make better sense.
Was This Post Helpful? 1
  • +
  • -

#6 theotherguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-April 11

Re: vb.net p2p chat "InvalidOperationException was unhandled." Err

Posted 07 April 2011 - 08:51 AM

modi123_1 said:

Interesting. Well here's a new experience for you - breakpoints and debugging! Start with the first line to execute in your app, right click -> breakpoints -> insert break point. Now when you run your code it should stop right there... then use the 'next line' button at the toolbar (or F10 if you have kept the default step keybinding). From there walk through your code until you find the line that blows up! This is a skill you will use over and over so better to learn it now!


Thanks! I will look and try to follow up. :)

View Posthawkvalley1, on 07 April 2011 - 08:40 AM, said:

It's probably line 3 where you declare.
Dim msg As String = txtSend.Text


This object is not ready, and it is empty here anyway. Set it somewhere where it would make better sense.


Yeah, you are right. Thanks for the help. Now I'm trying to get the message from stream into txtDisplay. Still figuring it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1