0
I am developing a Windows Form C# application and I am using Tcpclient to carry out communication within my internal network, in some moments I need to close an existing connection and after a few minutes reopen this connection, but I’m having trouble performing this procedure.
First I start Tcpclient.
TcpClient clientSocket = new TcpClient();
clientSocket.Connect(textBox1.Text, int.Parse(textBox2.Text));
At some point I want to close this connection, I tried Dispose() and Close().
clientSocket.Dispose();
clientSocket.Close();
And then wish in a moment to reconnect by performing the following commands
if (!clientSocket.Connected)//Verifico se a conexão esta ativa
{
clientSocket = new TcpClient();//Estáncio novamente clientSocket
clientSocket.Connect(textBox1.Text, int.Parse(textBox2.Text));//Mas dá exceção nesta linha.
}
With all procedures adopted above is returned an Exception
System.Net.Sockets.Socketexception: 'A socket operation has been attempted on an inaccessible host xx.xx.x.xx:8080', in the line below
clientSocket.Connect(textBox1.Text, int.Parse(textBox2.Text));
This means that the connection really gets instantiated by not letting makes a new connection.
How to carry out this procedure, so that you have the freedom to carry out these procedures ?