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...
Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
Here is my listener timer tick event:
Private Sub Listening()
And here is my send message event
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
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.
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
'Send the message...
Client = New TcpClient(userAddress, intPort)
Dim Writer As New StreamWriter(Client.GetStream())
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!"