简体   繁体   English

Android Java Server套接字无法连接

[英]Android Java Server Socket does not connect

I am writing an app which needs to receive a string from a server. 我正在编写一个需要从服务器接收字符串的应用程序。 The following code works if the IP Adress connected to is "127.0.0.1" (The Client and the server are on the same phone, just for testing purpose), but not if it is the "real" IP Adress of the phone. 如果连接的IP地址是“ 127.0.0.1”(客户端和服务器在同一台电话上,仅用于测试目的),则以下代码有效,但如果它是电话的“真实” IP地址,则以下代码有效。

Server: 服务器:

ServerSocket echoServer = null;
        String line;
        DataInputStream is;
        PrintStream os;
        Socket clientSocket = null;

        // Try to open a server socket on port 9999
        try {
            echoServer = new ServerSocket(1109);
        } catch (IOException e) {
            System.out.println(e);
        }
        // Create a socket object from the ServerSocket to listen and
        // accept
        // connections.
        // Open input and output streams

        try {
            clientSocket = echoServer.accept();
            is = new DataInputStream(clientSocket.getInputStream());
            os = new PrintStream(clientSocket.getOutputStream());

            // As long as we receive data, echo that data back to the
            // client.

                os.println("Das ist ein Test immernoch");
                publish("Fertig");
        } catch (IOException e) {
            publish("Fertig");
        } catch (Exception e) {
            publish("Fertig");
        }

Client: 客户:

Socket smtpSocket = null;
    DataOutputStream os = null;
    DataInputStream is = null;

    try {
        smtpSocket = new Socket();
        smtpSocket.connect(new InetSocketAddress("46.114.153.58", 1109), 10000); //That is the critcal line, if the IP is "127.0.0.1" everything works perfectly fine
        os = new DataOutputStream(smtpSocket.getOutputStream());
        is = new DataInputStream(smtpSocket.getInputStream());
    } catch (UnknownHostException e) {
        return "Fehler";
    } catch (IOException e) {
        return "Fehler";
    }

    if (smtpSocket != null && os != null && is != null) {
        try {

            os.writeBytes("HELO\n");
            String s = is.readLine();
            os.close();
            is.close();
            smtpSocket.close();
            return s;
        } catch (UnknownHostException e) {
            //System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            //System.err.println("IOException:  " + e);
        }
    }
    return "Fehler";
}

EDIT: Hence this is an app for a mobile device, there is no router I can configure. 编辑:因此,这是一个用于移动设备的应用程序,没有我可以配置的路由器。

Add this to your code 将此添加到您的代码

if (Build.VERSION.SDK_INT >= 9) {  
                StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder()
                        .permitAll().build();
                StrictMode.setThreadPolicy(policy);

            }

如果打算将ServerSocket与外部连接一起使用,则可能必须转发路由器上正在使用的端口。

If this is the ip address of your router and you are connecting your android mobile to the router through wifi , then you have to port forward the port 1109 in your router to your mobile. 如果这是路由器的ip地址,并且您要通过wifi将android移动设备连接到路由器,则必须将路由器中的端口1109转发到移动设备。

If this is the ip address of your android mobile connected through data connection , then there will be some restrictions on your data provider blocking ports for security . 如果这是通过数据连接连接的安卓手机的IP地址,则数据提供者出于安全性的要求会限制端口的某些限制。

46.114.153.58 is this a static ip address or dynamic ? 46.114.153.58这是静态IP地址还是动态IP地址?

If its a dynamic ip address , first check the availability of the ip address by pinging it. 如果它是动态IP地址,请先通过ping通检查IP地址的可用性。

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

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