简体   繁体   English

Android Socket连接到服务器但未写入任何内容

[英]Android Socket connects to server but doesn't write anything

I'm currently runing a server on Eclipse(local IP 192.168.1.255, listening to port 4567). 我目前正在Eclipse(本地IP 192.168.1.255,正在侦听端口4567)上运行服务器。 A client can connect trought sockets and send messages, that will be printed on the terminal by the server. 客户端可以连接服务器套接字并发送消息,这些消息将由服务器打印在终端上。 Part of the server code is the following: 服务器代码的一部分如下:

    System.out.println("Client connected: " + clientName);
    String line;

    while (true){
        line = in.nextLine();
        System.out.println("STRING RECEIVED: " + line + " FROM " + clientName);
    }

where in is the input stream of the client socket. 其中in是客户端套接字的输入流。

Part of client code, instead, is: 客户端代码的一部分是:

    while(true) {
        System.out.print("\nEnter your input: ");
        line = stdin.next();
        socketOut.println(line);
        socketOut.flush();
    }

So, in example, a possible output on server terminal with two clients connected is the following: 因此,例如,连接两个客户端的服务器终端上的可能输出如下:

    Client connected: Socket[addr=/192.168.1.225,port=54852,localport=4567]
    STRING RECEIVED: Hello FROM Socket[addr=/192.168.1.225,port=54852,localport=4567]
    STRING RECEIVED: World FROM Socket[addr=/192.168.1.225,port=54852,localport=4567]
    Client connected: Socket[addr=/192.168.1.225,port=54945,localport=4567]
    STRING RECEIVED: Hello2 FROM Socket[addr=/192.168.1.225,port=54945,localport=4567]

Everything works well, so i'm now trying to access server trough sockets on a simple app developed on Android Studio. 一切正常,因此我现在尝试在Android Studio开发的简单应用程序上访问服务器槽式插槽。 The code is: 代码是:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new BackgroundTask().execute();

    }

    private class BackgroundTask extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            try {
                Socket socket = new Socket("10.0.2.2", 4567);
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                out.println(new String("Hi from Android!"));

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }
}

But the output is just 但是输出只是

    Client connected: Socket[addr=/192.168.1.225,port=55001,localport=4567]

and nothing else. 没别的。

Any advice about the println doesn't send anything? 关于println的任何建议都不会发送任何东西吗? The program works perfectly on Eclipse on both client/server side, so i guess the problem is on Android. 该程序在Eclipse的客户端/服务器端均可完美运行,因此我猜问题出在Android上。 Also, i enabled the Android network permissions, so the connection should work. 另外,我启用了Android网络权限,因此连接应该可以正常工作。

Thanks in advance to everybody. 在此先感谢大家。

EDIT : solved, i just changed Android client code to: 编辑 :解决,我只是将Android客户端代码更改为:

            try {
            Socket socket = new Socket("10.0.2.2", 4567);
            if (socket.isConnected()) {
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream())), true);
                String line = new String("Hi from Android!");
                out.println(line);
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

You need to either flush the PrintWriter or construct it to auto-flush. 您需要刷新PrintWriter或将其构造为自动刷新。 It doesn't by default. 默认情况下不是。

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

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