【问题标题】:Android Server socket connection to pc c# application get no connectionAndroid Server socket connection to pc c# application get no connection
【发布时间】:2019-01-23 18:47:12
【问题描述】:

当 android 设备(模拟器)充当客户端并且 pc 应用程序充当服务器时,我的第一个 tcp 套接字连接工作正常(当您知道在客户端使用 10.0.2.2 作为服务器地址时)。

现在我需要 android 端作为服务器,pc 应用程序作为客户端,但我根本没有连接。

两者都在同一台机器上运行(模拟器中的android)。

安卓端

ServerSocket serverSocket = new ServerSocket(9301);
Socket socket = serverSocket.accept();

PC端

TcpClient  dataClient = new TcpClient();

dataClient.BeginConnect( "127.0.0.1", 9301, Do, MyData);
// 127.0.0.1 just for testing, later there will be the real ip adress of the device

在模拟器 (5554) 的 telnet 会话中,我添加了一个这样的 redir

redir add tcp:9301:9301

老实说,我完全不确定 redir。有没有人知道我做错了什么,我对此很生气。

奇怪的是,使用这段代码,客户端可以连接到任何东西,而服务器却没有连接。

非常感谢

【问题讨论】:

  • 我没有找到它,所以我改变了概念并将其也更改为 Andoid/client 到 PC/Server 的连接,它运行良好。

标签: android sockets server emulation


【解决方案1】:

只是最初的结论之一。似乎 android 模拟器有时会收到比发送更少的字节数。例如,我发送一个 ~30000 字节的文件,我只收到 29987。有时它是可以的。我用 PC/c# 客户端进行了测试,连接工作完美,没有松动。当我测试 android 连接时,几乎 70% 的文件传输损坏,因为最多丢失了 15 个字节。当我将服务器端更改为 socket.SendFile 时,结果要好得多,但也不完美。我更新了我的协议,所以客户端知道应该有多少字节,如果出现接收错误,我可以重新发送文件。我很好奇真实设备和 wlan 的行为。

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2022-10-22
    • 2017-01-13
    • 2021-08-11
    相关资源
    最近更新 更多