简体   繁体   English

客户端套接字未连接到服务器套接字

[英]Client socket not connecting to server socket

I'm trying to transfer a message between two android phones over the local network.我正在尝试通过本地网络在两部 android 手机之间传输消息。 I read sockets where a good way to do this.我读过套接字,这是一个很好的方法。 (I cant use bluetooth) (I cant use NFC either) (我不能使用蓝牙)(我也不能使用 NFC)

I have built a server and client application.我已经构建了一个服务器和客户端应用程序。

One app has a server that listens for a connection.一个应用程序有一个服务器来监听连接。
The other app has a client that tries to connect when a button is pressed.另一个应用程序有一个客户端,当按下按钮时会尝试连接。

Both the manifest files contain the correct permissions.两个清单文件都包含正确的权限。 (with the html tags) (带有 html 标签)
uses-permission android:name="android.permission.INTERNET" /使用权限 android:name="android.permission.INTERNET" /
uses-permission >android:name="android.permission.ACCESS_NETWORK_STATE"/使用权限>android:name="android.permission.ACCESS_NETWORK_STATE"/

I put the server online first:我先把服务器上线:

      ServerSocket myServerSocket = new ServerSocket(27024);
      System.out.println("Server is waiting for incoming connection on host=" + InetAddress.getLocalHost().getCanonicalHostName() + ", port=" + myServerSocket.getLocalPort());
      Socket socket = myServerSocket.accept();

Then try and connect with the client.然后尝试与客户端连接。

            String host = "localhost";
            int port = 27024;

            try{
                System.out.println("Client attempting to connect to server at host: " + host + ", port: " + port);
                Socket socket = new Socket(host, port);

                //This below line never gets called :(
                System.out.println("Client socked created! Now trying to send data to server");

               }

In my console:在我的控制台中:
Client attempting to connect to server at host: localhost, port: 27024客户端尝试在主机:localhost,端口:27024 上连接到服务器

The "Client socked created!" “客户端已创建!” line never gets output.线永远不会得到输出。

Both hosts are set to "localhost" and the port number is the same.两台主机都设置为“localhost”,端口号相同。
I've tried various ports, but nothing happens.我尝试了各种端口,但没有任何反应。

String host = "localhost";
int port = 27024;

You realize that your client needs to be given the IP address of the server, right?您意识到您的客户端需要获得服务器的 IP 地址,对吗?

Note you will most likely not be able to, or ever should, bind an app on a non WiFi interface.请注意,您很可能无法或永远不应该在非 WiFi 接口上绑定应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM