我正在创建一个简单的服务器多客户端连接类。但是当我在VM上运行某些测试时,它会在某个时刻因错误而停止:
对象引用未设置为对象的实例。
我检查了调试,对象'b'设置为空。这是代码:
来自客户类的示例:
Dim client As Client_Info,
client_tcp As New TcpClient,
client_stream As New MemoryStream,
client_buffer As Byte() = {0}
Dim msg_read_bytes_count As Integer,
msg_header_size As Integer = 5,
msg_header As Header_Info
Dim header_bytes_count As Integer,
data_bytes_count As Long
Public Enum Enum_Data_Type
TEXT = 1
IMAGE = 2
KEEP_ALIVE = 4
FILE = 8
End Enum
Public Enum Enum_Connection_Status
CONNECTED = 1
DISCONNECTED = 2
End Enum
Public Structure Header_Info
Dim PACK_SIZE As Integer
Dim TYPE As Enum_Data_Type
End Structure
Public Structure Client_Info
Dim Connection_Status As Enum_Connection_Status
Dim ID As Integer
Dim IP As String ''The ID,Ip and port is used only by server for extra info.
Dim Port As String
Dim Computer_Name As String
Dim Win_Ver As String
End Structure
Sub New(ByVal hostname As String, ByVal port As Integer, ByVal client_info As Client_Info)
Try
With client
.ID = client_info.ID
.IP = client_info.IP
.Computer_Name = client_info.Computer_Name
.Win_Ver = client_info.Win_Ver
End With
client_tcp.BeginConnect(hostname, port, New AsyncCallback(AddressOf connect_to), client_tcp)
Catch ex As Exception
RaiseEvent Internal_Disconnected(ex)
End Try
End Sub
Public Sub Reconnect(ByVal hostname As String, ByVal port As Integer)
Try
If client.Connection_Status = Enum_Connection_Status.DISCONNECTED Then
client_tcp = New TcpClient
client_tcp.BeginConnect(hostname, port, New AsyncCallback(AddressOf connect_to), client_tcp)
End If
Catch ex As Exception
RaiseEvent Internal_Disconnected(ex)
End Try
End Sub
Public Sub SendText(ByVal text As String, Optional ByVal Data_TYPE As Enum_Data_Type = Enum_Data_Type.TEXT)
Dim buffer As Byte() = ASCII.GetBytes(text)
Dim header As Byte() = Create_Header(buffer.Length, Data_TYPE)
Try
If client.Connection_Status = 1 Then
client_tcp.Client.Send(header)
client_tcp.Client.Send(buffer)
RaiseEvent Process_Info("Sent <" & buffer.Length & "> bytes of string <" & Data_TYPE & ">")
End If
Catch ex As Exception
RaiseEvent Internal_Disconnected(ex)
End Try
End Sub
客户的代码:
Imports System
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text.Encoding
Imports TCP_Client.TCP_Client
Public Class Form1
Dim info As Client_Info
Dim b As TCP_Client.TCP_Client
Delegate Sub add_conn_del()
Delegate Sub proc_data(ByVal data As String)
Delegate Sub disc_del(ByVal reason As Exception)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With info
.Computer_Name = "test_client"
.ID = 1
.IP = "10.000.11"
.Win_Ver = "7"
End With
Dim b = New TCP_Client.TCP_Client("192.168.1.100", 60000, info)
AddHandler b.Connected, AddressOf add_conn
AddHandler b.Process_Info, AddressOf proccess
AddHandler b.Disconnected, AddressOf disc
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
b.SendText(TextBox1.Text)
''This is triggered when I want to send a message-->b is set here to 'nothing'
End Sub
Private Sub add_conn()
If Label1.InvokeRequired Then
Dim d As New add_conn_del(AddressOf add_conn)
Me.Invoke(d)
Else
Label1.Text = "CONECTED!!!"
End If
End Sub
Private Sub proccess(ByVal data As String)
If Label1.InvokeRequired Then
Dim d As New proc_data(AddressOf proccess)
Me.Invoke(d, data)
Else
Label1.Text &= vbCrLf & data
End If
End Sub
Private Sub disc(ByVal reason As Exception)
If label1.InvokeRequired Then
Dim d As New disc_del(AddressOf disc)
Me.Invoke(d, reason)
Else
label1.Text &= vbCrLf & reason.Message
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
b.Reconnect("192.168.1.100", 60000) ''this also doesn't work
End Sub
End Class
这几乎是整个代码。其他地方没有错误。为什么'b'设置为空?