通过套接字传输多个文件时出错

时间:2016-01-17 20:40:30

标签: java android sockets

我创建了一个应用,其中设备必须将2个txt文件发送到另一个设备。

客户端代码段(发送者代码):

downloadButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startDownload();

            setReceivedFileFromServer("1.Received file from server with success...");

            setSendFileSuccess("2.Sending to Client.....");
            final ScheduledExecutorService exec = Executors.newScheduledThreadPool(5);
            exec.schedule(new Runnable() {
                @Override
                public void run() {
                    try {
                        Log.d("Activity", "calling sendRequestedFile()");
                        sendRequestedFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.d("FileSendService", "Error sending file to: " + getClientInetAddress().toString());
                    }

                    try {
                        Log.d("Activity", "calling sendRequestedFile()");
                        sendRequestedFile2();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.d("FileSendService", "Error sending file to: " + getClientInetAddress().toString());
                    }
                }
            }, 5, TimeUnit.SECONDS);

            setWaitingLabel("");
        }
    });

    private void sendRequestedFile() throws IOException {
        Socket socket2 = null;
        String host = getClientInetAddress().toString();

        host = host.substring(1);

        socket2 = new Socket(host, 8383);
        File file = new File(Environment.getExternalStorageDirectory() + "/" + getFileName());
        long length = file.length();

        Log.d("FileSendService", "File length: " + length);

        byte[] bytes = new byte[16 * 1024];
        InputStream in = new FileInputStream(file);
        OutputStream out = socket2.getOutputStream();

        int count;
        while ((count = in.read(bytes)) > 0) {
            out.write(bytes, 0, count);
        }

        out.close();
        in.close();
        socket2.close();

    }

    private void sendRequestedFile2() throws IOException
    {
        Socket socket2 = null;
        String host = getClientInetAddress().toString();

        host = host.substring(1);

        socket2 = new Socket(host, 8484);
        File file = new File(Environment.getExternalStorageDirectory() + "/skc" + getFileName());
        long length = file.length();

        Log.d("FileSendService", "File length: " + length);

        byte[] bytes = new byte[16 * 1024];
        InputStream in = new FileInputStream(file);
        OutputStream out = socket2.getOutputStream();

        int count;
        while ((count = in.read(bytes)) > 0) {
            out.write(bytes, 0, count);
        }

        out.close();
        in.close();
        socket2.close();

        setSendFileSuccess("2.File was sent to client: "+getClientInetAddress().toString());
    }

接收器/服务器片段:

private class ReceiveRequestedFileAsync extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        ServerSocket serverSocket = null;
        Socket client = null;

        InputStream in = null;
        OutputStream out = null;

        try {

            Log.v("RequestedFileReceiveService", "Creating server socket");
            serverSocket = new ServerSocket(8383);

            client = serverSocket.accept();


            in = client.getInputStream();
            out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+retrieveFileNameFromURL(getInsertedUrl()));


            byte[] bytes = new byte[16*1024];


            int count;
            while ((count = in.read(bytes)) > 0)
            {
                out.write(bytes, 0, count);
            }

            out.close();
            in.close();
            client.close();
            serverSocket.close();

            return null;
        } catch (IOException e) {}

        return null;

    }

    @Override
    protected void onPostExecute(String result)
    {

        _ReceiveRequestedFileAsync2 = new ReceiveRequestedFileAsync2();
        _ReceiveRequestedFileAsync2.execute();

    }
}

private class ReceiveRequestedFileAsync2 extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        ServerSocket serverSocket = null;
        Socket client = null;

        InputStream in = null;
        OutputStream out = null;

        try {

            Log.v("RequestedFileReceiveService", "Creating server socket");
            serverSocket = new ServerSocket(8484);

            client = serverSocket.accept();


            in = client.getInputStream();
            out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/skc"+retrieveFileNameFromURL(getInsertedUrl()));


            byte[] bytes = new byte[16*1024];


            int count;
            while ((count = in.read(bytes)) > 0)
            {
                out.write(bytes, 0, count);
            }

            out.close();
            in.close();
            client.close();
            serverSocket.close();

            return null;
        } catch (IOException e) {}

        return null;

    }
}


第一个文件成功转移,但当我尝试转移第二个文件时,我收到此错误:

      01-17 22:23:44.882 25187-27460/com.android.python27 W/System.err: java.net.ConnectException: failed to connect to /192.168.49.10 (port 8484): connect failed: ECONNREFUSED (Connection refused)
01-17 22:23:44.892 25187-27460/com.android.python27 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
01-17 22:23:44.902 25187-27460/com.android.python27 W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
01-17 22:23:44.902 25187-27460/com.android.python27 W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
01-17 22:23:44.902 25187-27460/com.android.python27 W/System.err:     at java.net.Socket.startupSocket(Socket.java:590)
01-17 22:23:44.902 25187-27460/com.android.python27 W/System.err:     at java.net.Socket.tryAllAddresses(Socket.java:128)
01-17 22:23:44.902 25187-27460/com.android.python27 W/System.err:     at java.net.Socket.<init>(Socket.java:178)
01-17 22:23:44.902 25187-27460/com.android.python27 W/System.err:     at java.net.Socket.<init>(Socket.java:150)

1 个答案:

答案 0 :(得分:0)

您创建两个服务器(在两个不同的端口上)。但是,只有在第一台服务器收到文件时才创建第二台服务器。

在客户端,您可以创建直接发送两个文件的rwo客户端。我认为第二个客户端无法连接,因为第二个服务器尚未启动。

话虽如此,我想知道你为什么要使用两个端口。一台服务器可以处理多个cllients和文件传输。