java.net.SocketException:在

时间:2017-05-20 10:15:41

标签: android sockets wifi-direct

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

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

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

   if (mBoundService != null) {
            mSOcket = mBoundService.getSocket();

            //fetching all connected client socket list to broadcast all
            mSocketArraylist = mBoundService.getSocketArrayList();

            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");
                Thread.interrupted();

            }

        }

public static boolean copyFile(InputStream inputStream, DataOutputStream 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(TAG, e.toString());
        return false;
    }
    return true;
}

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

 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);
        }
       //            out.close();
      //            inputStream.close();
    } catch (IOException e) {
        Log.d(TAG, e.toString());
        return false;
    }
    return true;
}

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

0 个答案:

没有答案