xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.42k stars 507 forks source link

`mlaunch --tcp-tunnel` with TcpListener and TcpClient works only once #20598

Closed JaneySprings closed 1 month ago

JaneySprings commented 1 month ago

Hi! I have a simple app with TcpListener and TcpClient for working with iOS device. I use mlaunch --tcp-tunnel for the communication. For the first time it works perfectly. But for the second time it gets the incorrect stream(?) and hangs at ReadLine() (client and listener).

Example

TcpListener (iOS device side):

var tcpListener = new TcpListener(IPAddress.Loopback, 9988);
tcpListener.Start();

while (true) {
     using var client = await tcpListener.AcceptTcpClientAsync();
     using var stream = client.GetStream();
     using var reader = new StreamReader(stream);
     using var writer = new StreamWriter(stream) { AutoFlush = true };

     var request = await reader.ReadLineAsync(); // hangs here
     await writer.WriteLineAsync("message from device");
}

dotnet console app:

using var client = new TcpClient("localhost", port);
using var stream = client.GetStream();
using var writer = new StreamWriter(stream) { AutoFlush = true };
using var reader = new StreamReader(stream);

writer.WriteLine("message to device");
var response = reader.ReadLine();  // hangs here

Steps to Reproduce

1) run mlaunch --tcp-tunnel=9988:9988 --devname=<udid> 2) run the console app from example -> OK 3) run the console app from example -> hanging on ReadLine()

Expected Behavior

works every run

Actual Behavior

Hangs after first try

Environment

9.0.100-preview.3.24204.13 Xamarin.Launcher 1.0.83 (refs/heads/main: 699ed5eaf2)

maui-maccatalyst           9.0.0-preview.3.10457/9.0.100-preview.3      SDK 9.0.100-preview.3
maui-ios                   9.0.0-preview.3.10457/9.0.100-preview.3      SDK 9.0.100-preview.3

Xcode 17.2 MacOS 14.4.1 iOS 16.7.7

rolfbjarne commented 1 month ago

I'm not entirely sure I understand your scenario, but mlaunch --tcp-tunnel is a single-attempt tunnel. If any side closes the connection, you'll have to run mlaunch again to re-establish a new tunnel.

So as far as I can tell this behaves as expected?

microsoft-github-policy-service[bot] commented 1 month ago

Hi @JaneySprings. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

JaneySprings commented 1 month ago

Yes, thank you!