Results 1 to 3 of 3

Thread: Please help with TCP Client/Listener

  1. #1
    canaanp is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Apr 2011
    Posts
    2
    Reputation
    0

    Please help with TCP Client/Listener

    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...
    Code:
    Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
    
            ListThread.Start()
    Code:
    Private Sub Listening()
    
            Listener.Start()
    
    End Sub
    Here is my listener timer tick event:
    Code:
    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
    Code:
        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.

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,197
    Reputation
    2368
    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.

  3. #3
    canaanp is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Apr 2011
    Posts
    2
    Reputation
    0
    Quote Originally Posted by JohnH View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking