java.net.SocketException:socket关闭

时间:2017-05-22 05:23:13

标签: java android sockets

我使用套接字将图像从服务器发送到多个客户端但是当我尝试发送另一个图像时再次向每个客户端发送一个图像后,它说套接字已关闭但我甚至没有关闭套接字。

所以我希望保持我的服务器和客户端套接字处于活动状态,直到活动可见。

我正在使用服务来运行套接字服务器,之后我将所有套接字列表发送到活动,所以下面是我的服务器端代码

if (mSocketArraylist != null) {

            for (int i = 0; i < mSocketArraylist.size(); i++) {
                Socket mSocket = mSocketArraylist.get(i);
                try {
                    DataOutputStream out = new DataOutputStream(mSocket.getOutputStream());
                    ContentResolver cr = mContext.getContentResolver();
                    InputStream is = null;
                    try {
                        is = cr.openInputStream(Uri.parse(data.getData().toString()));
                    } catch (FileNotFoundException e) {
                        Log.d(TAG, e.toString());
                    }
                    copyFile(is, out);

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

        } else {
            Log.e("Socket value", "Socket is null");
        }

对于客户端,我也在其实施之下使用服务

 class clientThread implements Runnable {
    @Override
    public void run() {
        if (wifiInfo != null) {
            if (!wifiInfo.isGroupOwner) {
                String host = wifiInfo.groupOwnerAddress.getHostAddress();
                Socket clientSocket = new Socket();
                OutputStream os = null;

                try {
                    clientSocket.bind(null);
                    clientSocket.setKeepAlive(true);
                    clientSocket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);

                    os = clientSocket.getOutputStream();
                    PrintWriter pw = new PrintWriter(os);

                    final File f = new File(Environment.getExternalStorageDirectory() + "/"
                            + SocketClientService.this.getPackageName() + "/VR-" + System.currentTimeMillis()
                            + ".jpg");


                    File dirs = new File(f.getParent());
                    if (!dirs.exists())
                        dirs.mkdirs();

                    f.createNewFile();

                    while (true) {
                        Log.d(TAG, "server: copying files " + f.toString());
                        InputStream inputstream = clientSocket.getInputStream();
                        copyFile(inputstream, new FileOutputStream(f));
                        break;
                    }
                    signalActivity(f.getAbsolutePath());

                } catch (IOException e) {
                    Log.e(TAG, e.getMessage());
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());

                }

            } else {
                Log.e(TAG, "This device is a group owner, therefore the IP address of the " +
                        "target device cannot be determined. File transfer cannot continue");
            }

        }
    }
}


 public static boolean copyFile(InputStream inputStream, OutputStream out) {
    byte buf[] = new byte[8192 * 8192];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
    } catch (IOException e) {
        Log.d(TAG, e.toString());
        return false;
    }
    return true;
}

所以任何人都可以帮助我,帮助我们将不胜感激。

signalActivity方法实现

 public void signalActivity(String message) {
    Bundle b = new Bundle();
    b.putString("message", message);
    clientResult.send(port, b);
}

0 个答案:

没有答案