-1

I have made an application in my Xamarin.Forms project where I can connect my Android phone to my Computer using a TCP connection. I have found while using both TcpClient.ConnectAsync and TcpClient.BeginConnect, they both return that client.Connected is true even though the port isn't open. I have verified this because I tried random IPs and random ports and it still says connection was successful.

When using TcpClient.ConnectAsync, it doesn't return true unless I press the button that runs the code under Button_Clicked 2 times, but when using TcpClient.BeginConnect, client.Connected always returns true. I know for a fact that the client isn't connected because I have a detection system that kicks the user to the reconnect page when the connection is lost.

The code I have for my TCPClient in MainPage.xaml.cs:

TcpClient client = new TcpClient();
private async void Button_Clicked(object sender, EventArgs e)
{
    await client.ConnectAsync(ipAddress.Text, Convert.ToInt32(Port.Text));

    if (client.Connected)
    {
        await DisplayAlert("Connected", "The client has successfully connected", "OK");
    }
    else
    {
        await DisplayAlert("Connection Unsuccessful", "The client couldn't connect!", "OK");
    }
}

I have also tried using TcpClient.BeginConnect from How to set the timeout for a TcpClient?:

TcpClient client = new TcpClient();
private async void Button_Clicked(object sender, EventArgs e)
{
    var result = client.BeginConnect(ipAddress.Text, Convert.ToInt32(Port.Text), null, null);
    var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));

    if (success)
    {
        await DisplayAlert("Connected", "The client has successfully connected", "OK");
    }
    else
    {
        await DisplayAlert("Connection Unsuccessful", "The client couldn't connect!", "OK");
    }
}

I tried looking up the issue and the only thing I found was: TcpClient.Connected returns true yet client is not connected, what can I use instead? but, this link is stating that the client.Connected bool remains true after disconnection, while my problem is that it says the client connects even even though the client never gets a true connection to the server.

The project is currently using .NET Standard 2.0

Crimson
  • 16
  • 1
  • 4
  • 'This is stating that the client remains connected after disconnection.' No it isn't. The `Connected` property only means that you *did* connect some time in the past. It doesn't indicate the current state of the connection. – user207421 Apr 02 '20 at 00:21
  • @user207421 That was not the issue that I am facing. I just added that part in there to say that the issue I'm having is different and so it wouldn't be closed as a duplicate. I meant that the property stated that the client remained connected. What was happening with my TcpClient.Connected property was that it would always return true without a real connection. I have no idea why this happens, and the only way I figured out to fix it was with completely disposing the client and reinstantiate it. – Crimson Apr 02 '20 at 02:59
  • The property *doesn't* 'state that the cilent remains connected'. That's my point. It states that you did connect it at some previous time. It stayes true even afer the peer disconnects. – user207421 Apr 02 '20 at 03:24
  • Anyway if you're just going to block during the connect phase, why aren't you using blocking mode? Why use asynchronous mode, or non-blocking mode? – user207421 Apr 02 '20 at 03:24
  • @user207421 I know it doesn't state that the client remains connected, I implemented a way to check disconnection besides using that method. Also I am planning on adding a form of blocking feature, but I did a test and set the timeout to 1000ms and saw that the issue was only when you tried to connect when it was already trying to connecting. Why does this happen? – Crimson Apr 02 '20 at 06:28
  • You don't have to 'add a form of blocking feature'. Blocking is the normal state of sockets. If you don't think it 'states that the client remains connected', why did you say it does? Twice? And of course it doesn't connect while it is already trying to connect. You're not making much sense here. – user207421 Apr 02 '20 at 08:38

1 Answers1

-1

I have found out the reason it would return client.Connected is true is because running the same ConnectAsync/BeginConnect method twice while the client is still trying to connect and hasn't yet timed out will cause the client.Connected value to be true for some reason.

The only way to fix this it to wait for the timeout to complete, or if the timeout is too long, to dispose the client and create a new one.

Crimson
  • 16
  • 1
  • 4
  • Not 'for some reason'. For the reason I stated. You connected it. So it shows as connected. The real problem here appears to be that you are trying to *re*-connect a socket. You can't do that, even if the connect failed. You have to close it and create a new one. – user207421 Apr 02 '20 at 08:39
  • @user207421 Yes I know that, that's not what I'm saying... It's saying connection successful when I put in some random IPs and ports. I'm saying it's declaring the client got connected when it shouldn't. And I know this for a fact because trying to send packets to the IP and port results in an exception saying that the client isn't even connected to the port. The reason I'm using random IPs and ports was to show to myself that client.Connected value responds with true even on non-existing ports. The client was never connected to the port, yet the client keeps saying it is. – Crimson Apr 02 '20 at 08:58
  • There was never a successful connection between the client and server, this issue would occur trying to run the connection function (connectasync or beginconnect) before the connection attempt has reached timeout. I'm trying to figure out why it would set client.connected to true when there was never even a single connection. – Crimson Apr 02 '20 at 09:17