无法通过TCP连接双向发送消息

时间:2014-11-18 22:02:46

标签: vb.net

我正在尝试创建代码,以便非portforwarded计算机可以通过TCP连接到一个端口转发的邮件,并且可以双向发送邮件。您能否查看下面的代码,看看为什么我无法收到要发送的消息?我付出了很多努力让它发挥作用而它没有:C Form1应该尝试连接到Form2,Form2应该接受连接并发送消息。

Form1(非移植计算机):

Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Runtime.Serialization.Formatters.Binary
    Public Class Form1
    Dim SenderClient As TcpClient
    Dim ReceiverClient As TcpClient
    Dim Receiver As TcpListener
    Dim Listening As New Thread(AddressOf Listen)
    Dim GetMessage As New Thread(AddressOf ReceiveMessage)
    Dim BF As New BinaryFormatter
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateNewReceiver(44444)
        Listening.Start()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CreateNewSender()
        Connect("loopback", 55555)
    End Sub
    Private Sub CreateNewSender()
        Try
            SenderClient.Client.Close()
        Catch ex As Exception

        End Try
        SenderClient = New TcpClient
    End Sub
    Private Sub CreateNewReceiver(ReceiverPort As Integer)
        Try
            Receiver.Server.Close()
            ReceiverClient.Client.Close()
        Catch ex As Exception

        End Try
        Receiver = New TcpListener(ReceiverPort)
    End Sub
    Private Sub Connect(IPAddress As String, SenderPort As Integer)
1:      Try
            SenderClient.Connect(IPAddress, SenderPort)
        Catch ex As Exception
            If MessageBox.Show("Error connecting to Form2: " & vbNewLine & ex.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Retry Then
                GoTo 1
            End If
        End Try
    End Sub
    Private Sub ReceiveMessage()
        While ReceiverClient.Connected = True
            MsgBox(BF.Deserialize(ReceiverClient.GetStream))
        End While
    End Sub
    Private Sub Listen()
        Try
            While ReceiverClient.Connected = False
                Receiver.Start()
                ReceiverClient = Receiver.AcceptTcpClient
            End While
            GetMessage.Start()
        Catch ex As Exception

        End Try
    End Sub
End Class

Form2(移植计算机):

Imports System.Net.Sockets
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Threading
Public Class Form2
    Dim SenderClient As TcpClient
    Dim ReceiverClient As TcpClient
    Dim Receiver As TcpListener
    Dim Listening As New Thread(AddressOf Listen)
    Dim GetMessage As New Thread(AddressOf ReceiveMessage)
    Dim BF As New BinaryFormatter
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateNewSender()
        CreateNewReceiver(55555)
        Listening.Start()
    End Sub
    Private Sub CreateNewSender()
        Try
            SenderClient.Client.Close()
        Catch ex As Exception

        End Try
        SenderClient = New TcpClient
    End Sub
    Private Sub CreateNewReceiver(ReceiverPort As Integer)
        Try
            Receiver.Server.Close()
            ReceiverClient.Client.Close()
        Catch ex As Exception

        End Try
        Receiver = New TcpListener(ReceiverPort)
    End Sub
    Private Sub ReceiveMessage()
        While ReceiverClient.Connected = True
            BF.Serialize(SenderClient.GetStream, "I've accepted the connection")
            MsgBox(BF.Deserialize("Data received: " & ReceiverClient.GetStream))
        End While
    End Sub
    Private Sub Listen()
        Try
            While ReceiverClient.Connected = False
                Receiver.Start()
                ReceiverClient = Receiver.AcceptTcpClient
            End While
            GetMessage.Start()
        Catch ex As Exception

        End Try
    End Sub
End Class

您可能会发现自己更容易尝试:只需要两种形式:Form1和Form2,Form1需要一个按钮。

-Rixterz

0 个答案:

没有答案