java - android中的多线程服务器套接字从连接的客户端接收图像 - 不工作

标签 java android multithreading sockets android-asynctask

当客户端连接到服务器时,它们应该能够将图像发送到服务器,这样服务器才能显示接收到的图像。我在服务器端使用 AsyncTask 来完成任务。

当一个客户端连接到服务器时,一切似乎都很好,图像正在从该客户端接收,但是当下一个客户端连接时它不工作,有时第一个客户端断开连接或者图像没有从一个客户端或两个客户端接收。

有人可以帮我弄这个吗?我做错了什么吗?

服务端异步任务

  public static class FileServerAsyncTask extends AsyncTask {

private Context context; private TextView statusText; public FileServerAsyncTask(Context context, View statusText) { this.context = context; this.statusText = (TextView) statusText; } @Override protected String doInBackground(Void... params) { class ClientWorker implements Runnable { private Socket client; private final File f; //Constructor ClientWorker(Socket client, File f) { this.client = client; this.f = f; } public void run(){ try{ File dirs = new File(f.getParent()); if (!dirs.exists()) dirs.mkdirs(); f.createNewFile(); Log.d(WiFiDirectActivity.TAG, "server: copying files " + f.toString()); InputStream inputstream = client.getInputStream(); copyFile(inputstream, new FileOutputStream(f)); } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); } } } try { ServerSocket serverSocket = new ServerSocket(8988); Log.d(WiFiDirectActivity.TAG, "Server: Socket opened"); boolean check = true; while(check){ ClientWorker w; try{ //server.accept returns a client connection final File f = new File(Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis() + ".jpg"); w = new ClientWorker(serverSocket.accept(),f); Thread t = new Thread(w); t.start(); return f.getAbsolutePath(); } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); } } serverSocket.close(); return null; } catch (IOException e) { Log.e(WiFiDirectActivity.TAG, e.getMessage()); return null; } } @Override protected void onPostExecute(String result) { if (result != null) { statusText.setText("File copied - " + result); Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + result), "image/*"); context.startActivity(intent); } } @Override protected void onPreExecute() { statusText.setText("Opening a server socket"); } } public static boolean copyFile(InputStream inputStream, OutputStream out) { byte buf[] = new byte[1024]; int len; try { while ((len = inputStream.read(buf)) != -1) { out.write(buf, 0, len); } out.close(); inputStream.close(); } catch (IOException e) { Log.d(WiFiDirectActivity.TAG, e.toString()); return false; } return true; }

最佳答案

我想你只需要改变这些行:

 w = new ClientWorker(serverSocket.accept(),f);
             Thread t = new Thread(w);
              t.start();
              return f.getAbsolutePath();

您的问题是在第一个客户端连接后您退出了 doInbackground。您必须使用 onProgressPublish() 发送中间结果并保持循环并且不要关闭服务器。

所以在你连接到第一个客户端并将他发送到你创建的套接字之后,你通过 return f.getAbsolutePath(); 退出主循环;从服务器出去。除了发送中间结果使用函数(onProgressPublish())之外,您的所有代码都是正确的。

关于java - android中的多线程服务器套接字从连接的客户端接收图像 - 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25106880/

相关文章:

java - 有什么方法可以将 JodaTime Period 转换为十进制小时数?

java - 具有相同值的 Int 到 Byte

安卓网页 View 。键盘隐藏

android - PercentFrameLayout 作为 Recycler View 中的一个项目不起作用

Java PrintStream 重定向行为异常

java - 在构造函数中设置线程的名称

multithreading - 如何在多线程程序上启用反向调试?

java - 我在 MongoDB 中使用 MapReduce 时遇到错误

java - 对类数组进行排序?

android - 如何创建可滚动的选项卡?