Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有两个Android设备的Socket服务器/客户端_Android_Sockets_Client - Fatal编程技术网

具有两个Android设备的Socket服务器/客户端

具有两个Android设备的Socket服务器/客户端,android,sockets,client,Android,Sockets,Client,所以我有一个平板电脑,我想让它基本上成为一个TCP(或UDP)套接字服务器,这样多部手机可以向它发送数据。我还希望能够将回复发送回手机。到目前为止我所做的都不管用 服务器: Log.i("Server", "Starting server..."); try { ServerSocket ss = new ServerSocket(8080); Log.i("Server", "Ser

所以我有一个平板电脑,我想让它基本上成为一个TCP(或UDP)套接字服务器,这样多部手机可以向它发送数据。我还希望能够将回复发送回手机。到目前为止我所做的都不管用

服务器:

Log.i("Server", "Starting server...");
                try {
                    ServerSocket ss = new ServerSocket(8080);
                    Log.i("Server", "Server status: " + ss.isBound());
                    while(true){
                        //Server is waiting for client here, if needed
                        Socket s = ss.accept();
                        Log.i("Server", "Server is accepting connections: " + s.isConnected());
                        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                        PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
                        Log.i("Server", "Initialized input and output");
                        String st = input.readLine();
                        Log.i("Server", "From client: "+st);
                        output.println("Good bye and thanks for all the fish :)");
                    }

                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
客户(电话):


很可能您的客户没有刷新println

尝试在构造函数中指定autoflush

PrintWriter output = new PrintWriter(out, true);
或者在打印下方手动添加刷新

output.println("Hello Android!");
output.flush();

定义“不起作用”?堆栈跟踪,特定行为?在我完全关闭手机上的应用程序之前,消息实际上不会发送到服务器。能否从logcat添加堆栈跟踪?虽然服务器端已指定“自动刷新”,但客户端尚未指定。
output.println("Hello Android!");
output.flush();