ulta
06-26-2004, 05:31 PM
I'm fooling around with Microsoft Visual Studio .net 2003, trying to learn some winsock programming.
Right now I'm trying to make a simple server. I've reference MSwinsck.ocx.
When I compile the code below, I get the following error
"An unhandled exception of type 'System.NullReferenceException' occurred in SimpleChat.exe
Additional information: Object reference not set to an instance of an object."
Am I not putting something on my form correctly? The tutorials said I needed a winsock component on the form, but I have no idea how to put it on there.
Thxs
Public Class Form1
Inherits System.Windows.Forms.Form
Dim tcpServer As MSWinsockLib.WinsockClass
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TxtSend As System.Windows.Forms.TextBox
Friend WithEvents TxtRecieve As System.Windows.Forms.TextBox
Friend WithEvents send As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TxtSend = New System.Windows.Forms.TextBox
Me.TxtRecieve = New System.Windows.Forms.TextBox
Me.send = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TxtSend
'
Me.TxtSend.Location = New System.Drawing.Point(32, 216)
Me.TxtSend.Name = "TxtSend"
Me.TxtSend.Size = New System.Drawing.Size(152, 20)
Me.TxtSend.TabIndex = 0
Me.TxtSend.Text = ""
'
'TxtRecieve
'
Me.TxtRecieve.AutoSize = False
Me.TxtRecieve.Location = New System.Drawing.Point(32, 40)
Me.TxtRecieve.Name = "TxtRecieve"
Me.TxtRecieve.Size = New System.Drawing.Size(240, 160)
Me.TxtRecieve.TabIndex = 1
Me.TxtRecieve.Text = ""
'
'send
'
Me.send.Location = New System.Drawing.Point(224, 216)
Me.send.Name = "send"
Me.send.Size = New System.Drawing.Size(48, 24)
Me.send.TabIndex = 2
Me.send.Text = "Send"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.send)
Me.Controls.Add(Me.TxtRecieve)
Me.Controls.Add(Me.TxtSend)
Me.Name = "Form1"
Me.Text = "Server"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'// Set the LocalPort property to an integer.
'// Then invoke the Listen method.
tcpServer.LocalPort = 100 <-------- The error happens here
tcpServer.Listen()
'// Get our Server Name, and set the form's caption
End Sub
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
' Check if the control's State is closed. If not,
' close the connection before accepting the new
' connection.
If tcpServer.State <> MSWinsockLib.StateConstants.sckClosed Then tcpServer.Close()
' Accept the request with the requestID
' parameter.
tcpServer.Accept(requestID)
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpServer.GetData(strData)
TxtRecieve.Text = strData
End Sub
Private Sub cmdSend_Click()
If tcpServer.State <> MSWinsockLib.StateConstants.sckConnected Then
MsgBox("You must be connected to the client first")
Else
tcpServer.SendData(TxtSend.Text)
End If
End Sub
End Class
Right now I'm trying to make a simple server. I've reference MSwinsck.ocx.
When I compile the code below, I get the following error
"An unhandled exception of type 'System.NullReferenceException' occurred in SimpleChat.exe
Additional information: Object reference not set to an instance of an object."
Am I not putting something on my form correctly? The tutorials said I needed a winsock component on the form, but I have no idea how to put it on there.
Thxs
Public Class Form1
Inherits System.Windows.Forms.Form
Dim tcpServer As MSWinsockLib.WinsockClass
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TxtSend As System.Windows.Forms.TextBox
Friend WithEvents TxtRecieve As System.Windows.Forms.TextBox
Friend WithEvents send As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TxtSend = New System.Windows.Forms.TextBox
Me.TxtRecieve = New System.Windows.Forms.TextBox
Me.send = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TxtSend
'
Me.TxtSend.Location = New System.Drawing.Point(32, 216)
Me.TxtSend.Name = "TxtSend"
Me.TxtSend.Size = New System.Drawing.Size(152, 20)
Me.TxtSend.TabIndex = 0
Me.TxtSend.Text = ""
'
'TxtRecieve
'
Me.TxtRecieve.AutoSize = False
Me.TxtRecieve.Location = New System.Drawing.Point(32, 40)
Me.TxtRecieve.Name = "TxtRecieve"
Me.TxtRecieve.Size = New System.Drawing.Size(240, 160)
Me.TxtRecieve.TabIndex = 1
Me.TxtRecieve.Text = ""
'
'send
'
Me.send.Location = New System.Drawing.Point(224, 216)
Me.send.Name = "send"
Me.send.Size = New System.Drawing.Size(48, 24)
Me.send.TabIndex = 2
Me.send.Text = "Send"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.send)
Me.Controls.Add(Me.TxtRecieve)
Me.Controls.Add(Me.TxtSend)
Me.Name = "Form1"
Me.Text = "Server"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'// Set the LocalPort property to an integer.
'// Then invoke the Listen method.
tcpServer.LocalPort = 100 <-------- The error happens here
tcpServer.Listen()
'// Get our Server Name, and set the form's caption
End Sub
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
' Check if the control's State is closed. If not,
' close the connection before accepting the new
' connection.
If tcpServer.State <> MSWinsockLib.StateConstants.sckClosed Then tcpServer.Close()
' Accept the request with the requestID
' parameter.
tcpServer.Accept(requestID)
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpServer.GetData(strData)
TxtRecieve.Text = strData
End Sub
Private Sub cmdSend_Click()
If tcpServer.State <> MSWinsockLib.StateConstants.sckConnected Then
MsgBox("You must be connected to the client first")
Else
tcpServer.SendData(TxtSend.Text)
End If
End Sub
End Class