Please help with TCP Client/Listener

canaanp

New member
Joined
Apr 13, 2011
Messages
2
Programming Experience
Beginner
Hello, I am trying to follow a tutorial on a simple P2P chat program in VB.NET which I found here:
P2P Connections - VB.NET Tutorials | Dream.In.Code

I am attempting to make mine a little more robust. However, I am having an issue when sending a message, here is what I have (in regards to sending/receiving the messages):


First to setup the listener in a separate thread in form load event...
VB.NET:
Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))

        ListThread.Start()

VB.NET:
Private Sub Listening()

        Listener.Start()

End Sub

Here is my listener timer tick event:
VB.NET:
Private Sub tmrMessageListener_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrMessageListener.Tick

        If Listener.Pending = True Then

            Message = ""

            Client = Listener.AcceptTcpClient()

            Dim Reader As New StreamReader(Client.GetStream())

            While Reader.Peek > -1

                Message = Message + Convert.ToChar(Reader.Read()).ToString

            End While

            MsgBox(Message, MsgBoxStyle.OkOnly)

        End If

End Sub

And here is my send message event
VB.NET:
    Public Sub SendMessage(ByRef recipientUser As String, ByVal outgoingMessage As String)

        'Lookup recipient's IP address from the userList dictionary...
        Dim userAddress As String = ""

        Dim pair As KeyValuePair(Of String, String)

        For Each pair In userList

            If pair.Key = recipientUser Then

                userAddress = pair.Value

            End If

        Next

        'Send the message...
        Try

            Client = New TcpClient(userAddress, intPort)

            Dim Writer As New StreamWriter(Client.GetStream())

            Writer.Write(outgoingMessage)

            Writer.Flush()

            frmChat.toolstriplabelStatus.Text = "Message to " & recipientUser & ": Success!"


        Catch ex As Exception

            Dim Errorresult As String = ex.Message

            MessageBox.Show(Errorresult & vbCrLf, "Error Sending Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

            frmChat.toolstriplabelStatus.Text = "Message to " & recipientUser & ": Fail!"

        End Try

End Sub

What happens when I send a message to a user is that I get the exception message that the target machine actively refused the connection. Does this mean that my listener is setup wrong? I don't understand what I'm doing wrong here, could someone please point me in the right direction? If this matters at all, I am using the same port but on UDP to broadcast users to add to a list, so I know that is working, but cannot establish TCP connection.
 
Does this mean that my listener is setup wrong?
How is your listener set up? You have only shown that you call Start.
The thread you started there is pointless by the way, it ends right after your TcpListener.Start call. What you really should do on secondary threads is connections and communications.
If this matters at all, I am using the same port but on UDP to broadcast users
UDP and TCP on same port is ok.
target machine actively refused the connection
There could be a firewall blocking the connection.
 
How is your listener set up? You have only shown that you call Start.
The thread you started there is pointless by the way, it ends right after your TcpListener.Start call. What you really should do on secondary threads is connections and communications.

UDP and TCP on same port is ok.

There could be a firewall blocking the connection.

I got it working, port was a variable that had not been set before getting used so I guess I was listening on port=nothing.
 
Back
Top