Reputation: 11
I'm having an issue establishing a connection to my TcpClient
using the local server address of 127.0.0.1
and port 1000
. It attempts to establish a connection and then hits my Catch SocketException
that it is unable to find a server.
I've set breakpoints and have went through the code line by line and it catches my exception once the line NetStream = Client.GetStream()
is called from within my Try/Catch
after pressing my button btnStart
. Showing that no connection is being established at all. Why is this?
Public Class frmForm1
Dim Client As TcpClient
Dim connection As Socket
Dim NetStream As NetworkStream
Private Sub btnStartClient_Click(sender As Object, e As EventArgs) Handles btnStartClient.Click
Try
txtLog.Text &= "Attempting to connect"
Client = New TcpClient()
Client.Connect(txtAddress.Text, CInt(txtPort.Text))
NetStream = Client.GetStream()
' Catch errors in trying to connect to server
Catch SocketEx As SocketException
txtLog.Text &= "Cannot find server"
End Try
End Sub
It shouldn't be throwing my exception and then creating my NetworkStream Reader/Writer objects and print that they were created. As well as setting up the listening thread. However, I cannot even get that far as my Catch SocketException
is called at the line NetStream = Client.GetStream()
Upvotes: 0
Views: 264
Reputation: 1917
You need at least 2 threads. One for the server to listen and one for the clinet to connect. Make sure to call .Start()
on the TcpListener
object (Server.Start()
in your case) before trying to connect the client to the server.
Upvotes: 1