如何处理VB.net套接字编程中的异常

如何处理VB.net套接字编程中的异常,vb.net,sockets,exception,client,Vb.net,Sockets,Exception,Client,我正在使用VB.net编写一个客户机/服务器应用程序。 我使用中的代码连接到服务器: ' ManualResetEvent instances signal completion. Private Shared connectDone As New ManualResetEvent(False) Private Shared sendDone As New ManualResetEvent(False) Private Shared receiveDone As New ManualResetE

我正在使用VB.net编写一个客户机/服务器应用程序。 我使用中的代码连接到服务器:

' ManualResetEvent instances signal completion.
Private Shared connectDone As New ManualResetEvent(False)
Private Shared sendDone As New ManualResetEvent(False)
Private Shared receiveDone As New ManualResetEvent(False)

' The response from the remote device.
Private Shared response As String = String.Empty


Public Shared Sub Main()
    ' Establish the remote endpoint for the socket.
    ' For this example use local machine.
    Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
    Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
    Dim remoteEP As New IPEndPoint(ipAddress, port)

    ' Create a TCP/IP socket.
    Dim client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    ' Connect to the remote endpoint.
    client.BeginConnect(remoteEP, New AsyncCallback(AddressOf ConnectCallback), client)

    ' Wait for connect.
    connectDone.WaitOne()

    ' Send test data to the remote device.
    Send(client, "This is a test<EOF>")
    sendDone.WaitOne()

    ' Receive the response from the remote device.
    Receive(client)
    receiveDone.WaitOne()

    ' Write the response to the console.
    Console.WriteLine("Response received : {0}", response)

    ' Release the socket.
    client.Shutdown(SocketShutdown.Both)
    client.Close()
End Sub 'Main
但当使用错误的IP地址和端口执行以引发异常时,应用程序将停止而不做任何操作。知道此函数是从Form_Load事件调用的,即使下面的MsgBox(“loaded”)也不会执行

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SendNetworkRequest(GV_ServerAddress, GV_ServerPort)
    MsgBox("Loaded")
End Sub
有人知道这次突然退出的原因吗?
提前谢谢。

ConnectCallBack在哪里?我在您发布的代码中没有看到它。我添加了ConnectCallBack方法。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SendNetworkRequest(GV_ServerAddress, GV_ServerPort)
    MsgBox("Loaded")
End Sub