通过套接字传输时出现随机错误

时间:2014-04-24 19:31:15

标签: java sockets reset filenotfoundexception eofexception

好的,所以我有一段代码应该通过套接字发送多个文件。我在for循环中这样做的方式我打开一个套接字 - >传输文件 - >关闭套接字然后重复其他文件。下面的代码如下:

for (int i = 0; i < fname.size(); i++) {
            Socket sok = new Socket("localhost",4444);
            PrintStream oos = new PrintStream(sok.getOutputStream());
            oos.println("1");
            try {

                System.out.println(fname.get(i));
                File myFile = new File(path+fname.get(i));
                byte[] mybytearray = new byte[(int) myFile.length()];

                FileInputStream fis = new FileInputStream(myFile);
                BufferedInputStream bis = new BufferedInputStream(fis);
                // bis.read(mybytearray, 0, mybytearray.length);

                DataInputStream dis = new DataInputStream(bis);
                dis.readFully(mybytearray, 0, mybytearray.length);

                OutputStream os = sok.getOutputStream();

                // Sending file name and file size to the server
                DataOutputStream doss = new DataOutputStream(os);
                doss.writeUTF(myFile.getName());
                doss.writeLong(mybytearray.length);
                doss.write(mybytearray, 0, mybytearray.length);
                doss.flush();
                sok.close();
                System.out.println("File " + fname.get(i) + " sent to Server.");

            } catch (Exception e) {
                System.err.println("File does not exist! (May not be true)   Generated Error: "+e);
            } 


            //sendFile(path+fname.get(i));
            //sock.close();
        }
    } catch (Exception e) {
        System.err.println(e);
    }

现在发生的事情是客户端没有吐出任何错误,事实上,它实际上说文件是发送到服务器的。现在服务器吐出随机错误。有时服务器会收到一些文件(绝不是所有文件),有时它不会收到任何文件,而且出来的错误也是随机的。错误是以下之一或组合:

java.io.EOFException
java.io.FileNotFoundException
java.net.SocketException: Connection reset
java.io.UTFDataFormatException

尝试传输时,所有这些错误都在服务器端。我不确定这里发生了什么= /

服务器代码:

public void receiveFile() {


    try {
        int bytesRead;

        DataInputStream clientData = new DataInputStream(
                clientSocket.getInputStream());

        String fileName = clientData.readUTF();
        OutputStream output = new FileOutputStream((fileName));
        long size = clientData.readLong();
        byte[] buffer = new byte[100000];
        while (size > 0
                && (bytesRead = clientData.read(buffer, 0,
                        (int) Math.min(buffer.length, size))) != -1) {
            output.write(buffer, 0, bytesRead);
            size -= bytesRead;
        }

        output.close();
        clientData.close();

        System.out.println("File " + fileName + " received from client.");
    } catch (IOException ex) {
        System.err.println("Client error. Connection closed.     " +ex);
    }
}

堆栈跟踪:

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at ClientConnection.receiveFile(ClientConnection.java:80)
    at ClientConnection.run(ClientConnection.java:46)
    at java.lang.Thread.run(Unknown Source)

1
Accepted connection : Socket[addr=/127.0.0.1,port=60653,localport=4444]
File LF-statistikkboka(Myers).pdf received from client.
1
java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at ClientConnection.receiveFile(ClientConnection.java:80)
    at ClientConnection.run(ClientConnection.java:46)
    at java.lang.Thread.run(Unknown Source)

File WHAT IS LEFT TO DO.docx received from client.

根据请求从客户端接收命令的代码

in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
        String clientSelection;
        clientSelection = in.readLine();
        System.out.println(clientSelection);
        while ((clientSelection) != null) {
            switch (clientSelection) {
            case "1":
                receiveFile();
                break;
            case "2":
                String outGoingFileName;
                while ((outGoingFileName = in.readLine()) != null) {
                    sendFile(outGoingFileName);
                }
                break;
            case "3":
                sync();
                break;
            default:
                System.out.println("Incorrect command received.");
                break;
            }
            //in.close();
            break;
        }

2 个答案:

答案 0 :(得分:0)

罪魁祸首可能是客户代码中的几行:

PrintStream oos = new PrintStream(sok.getOutputStream());
oos.println("1");

字符串&#34; 1&#34;将通过连接发送,但没有服务器代码接收它,因此它与文件的名称混淆,文件的名称被解释为大小,文件数据太短,所以你得到了很多例外。由于该值是常量,因此传输字符串&#34; 1&#34;似乎毫无意义,因此删除这些行至少应该修复它的一部分。

答案 1 :(得分:0)

您必须使用BufferedReader读取“1”行,这将“窃取”以下某些数据。您应该使用DataInputStream.readUTF()从流中读取命令,使用相同的DataInputStream来读取以下文件,并且应该使用DataOutputStream.writeUTF()来编写命令,使用您用来发送的相同DataOutputStream文件。

注意,不需要将整个文件读入内存。它只会浪费内存,增加延迟,并且不会扩展。使用像接收循环一样的复制循环,以及8k的缓冲区。