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...
Here is my listener timer tick event:
And here is my send message event
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.
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.