【发布时间】:2018-07-31 22:33:54
【问题描述】:
我有这个控制台应用程序 websocket 服务器:
Console.WriteLine("[SERVER]");
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
server.Start();
Console.WriteLine("Server has started on {0}.{1}Waiting for a connection...", server.LocalEndpoint, Environment.NewLine);
TcpClient client = server.AcceptTcpClient(); //wait for client to connect
Console.WriteLine("A client connected.");
NetworkStream stream = client.GetStream(); //communication channel with client
如何在 Xamarin.Android 应用中连接到控制台应用服务器?
我已经尝试过了(在 xamarin android 应用程序中,按钮的单击事件): 但我从 'client.Connect("127.0.0.1", 80);' 得到一个例外;
异常 = System.Net.Sockets.SocketException:连接被拒绝
requestSpeak.Click += (o, e) =>
{
TcpClient client = new TcpClient();
client.Connect("127.0.0.1", 80); //connect to sv
NetworkStream stream = client.GetStream(); //communication channel with server
};
【问题讨论】:
-
你必须使用内部IP,而不是环回
-
使用 10.0.2.2 stackoverflow.com/a/51602942/4984832
-
@SushiHangover 它没有建立连接,最终只是超时(System.Net.Sockets.SocketException:
) -
@Jaxi 我尝试了我的本地内部 ip,192.168.x.x。我的代码适用于两个控制台应用程序,但不适用于 xamarin android 应用程序和控制台应用程序。
-
您是通过模拟器还是实际设备运行您的 xamarin 程序?它是 android、UWP 还是 iOS?
标签: c# xamarin websocket xamarin.forms xamarin.android