I'm trying to make a private chat between two people but I can't get it to connect. I Googled it and found out that I can use TCPListener and TCPClient. I tried it out and it won't work. I'm usually good with making programs that don't require Internet but this one does and it's bugging me. I haven't worked on the sending message part because of the connection problem. Can anyone find out why this isn't working?
One computer is the hoster of the chat and one is the client connecting to the hoster of the chat.
Code:
Imports System.Net.Sockets
Public Class Form1
Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
Dim ClientSocket As TcpClient
Me.txtLog.AppendText("Connecting...")
Try
ClientSocket.Connect(Me.txtIP.Text, Me.txtPort.Text)
If ClientSocket.Connected = True Then
Me.cmdConnect.Enabled = False
Me.cmdSend.Enabled = True
Me.txtLog.AppendText(vbCrLf)
Me.txtLog.AppendText("Successfully connected.")
Me.txtLog.AppendText(vbCrLf)
Me.cmdSend.Enabled = True
Me.cmdConnect.Enabled = False
Else
Me.txtLog.AppendText(vbCrLf)
Me.txtLog.AppendText("Connection could not be established.")
Me.txtLog.AppendText(vbCrLf)
End If
Catch ex As Exception
Me.txtLog.AppendText(vbCrLf)
Me.txtLog.AppendText("Error: Cannot connect.")
Me.txtLog.AppendText(vbCrLf)
End Try
End Sub
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
End Sub
Private Sub btnHost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHost.Click
Dim HostSocket As New TcpListener(Me.txtPort.Text)
Try
Me.txtLog.AppendText("Creating new Host and Client Socket listener...")
Do Until HostSocket.Pending = True
HostSocket.Start(1)
Loop
If HostSocket.Pending = True Then
HostSocket.AcceptTcpClient()
HostSocket.Stop()
Me.txtLog.AppendText("Connection established with client.")
End If
Catch ex As Exception
HostSocket.Stop()
End Try
End Sub
End Class
One computer is the hoster of the chat and one is the client connecting to the hoster of the chat.