lantcp协议:主机不可访问

mdfafbf1  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(352)

我有一个用qt c++编写的基本tcpserver。
我有一个带有tcpsocket客户端(java)的基本android应用程序。
我的服务器运行在一个树莓皮4。
android应用程序在我的手机上运行。
似乎,我无法连接客户端和服务器。

  1. W/System.err: java.net.NoRouteToHostException: Host unreachable
  2. W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:132)
  3. at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
  4. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
  5. W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
  6. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
  7. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
  8. at java.net.Socket.connect(Socket.java:621)
  9. at java.net.Socket.connect(Socket.java:570)
  10. at java.net.Socket.<init>(Socket.java:450)
  11. W/System.err: at java.net.Socket.<init>(Socket.java:218)
  12. at Connection.doInBackground(Connection.java:19)
  13. at Connection.doInBackground(Connection.java:11)

我尝试了几个端口(50000、55000、6969),但没有任何变化。
在我的服务器上,如果我运行“telnet”,就建立了连接。但是在devandroid env上,如果我运行相同的命令,就无法建立连接。
我试图配置我的路由器(通过端口Map),但没有任何变化。
有人对我的问题有想法吗?谢谢
android java应用程序代码:

  1. @Override
  2. protected Integer doInBackground(Integer... strings) {
  3. try {
  4. Socket socket = new Socket(ip, 6969);
  5. BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  6. System.out.println("First create 1.1");
  7. return 0;
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. //Toast.makeText(getApplicationContext(), "Connection not done", Toast.LENGTH_SHORT).show();
  11. System.out.println("First create 1.2 : " + e.getStackTrace());
  12. return -1;
  13. }

服务器:

  1. Server::Server(int32_t port, QObject *parent) : QObject(parent)
  2. {
  3. server = new QTcpServer(parent);
  4. // whenever a user connects, it will emit signal
  5. connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
  6. quint16 portQt = static_cast<quint16>(port);
  7. if(!server->listen(QHostAddress::Any, portQt)) {
  8. qDebug() << "Server could not start";
  9. throw;
  10. }
  11. else {
  12. qDebug() << "Server started on port " << port << " " << portQt;
  13. }
  14. }
  15. Server::~Server() {
  16. }
  17. void Server::newConnection()
  18. {
  19. // need to grab the socket
  20. QTcpSocket *socket = server->nextPendingConnection();
  21. qDebug() << "YES";
  22. socket->write("Hello client\r\n");
  23. socket->flush();
  24. socket->waitForBytesWritten(3000);
  25. socket->close();
  26. }
eulz3vhy

eulz3vhy1#

  1. I have tried several ports (50000, 55000, 6969), but nothing changes.

您必须知道您的服务器正在侦听哪个端口号,并且只需要尝试连接到该端口

  1. On my server, if I run "telnet ", the connection is established. But on the dev Android env, if I run the same command, the connection cannot be established.

很可能你的树莓有telnet服务器 telnetd 已经在运行,这就是为什么你可以连接

  1. I have tried to configure my router (via the port mapping), but nothing changes.

要进行测试,您应该通过wifi连接手机和同一局域网上的服务器。我提醒您,在路由器上打开广域网的端口会带来安全风险

相关问题