通过TCP / IP传输文件适用于Windows,但不适用于Ubuntu

时间:2016-12-15 04:04:50

标签: java sockets ubuntu windows-7 tcp-ip

我在this guide的帮助下尝试使用TCP / IP连接发送多个文件。

当我从Ubuntu 14.04执行testClient代码时,文件将传输到Windows 7中的testServer。此处收到的.txt文件格式正确。

但是,当我从Windows 7执行testClients代码和从Ubuntu 14.04执行testServer时,Ubuntu 14.04中收到的文件搞砸了。 (来自txt#2内容的内容溢出到txt#1。)

在交换期间,除了IP地址和文件位置之外,testServer和testClients中的所有代码都没有被更改。

我很困惑。为什么代码在Windows 7中运行良好但在Ubuntu中运行不正常?我的代码有问题吗?我很感激任何帮助。

TestServer.java

public static void main(String[] args) throws IOException {
    FileOutputStream fos;
    BufferedOutputStream bos;
    OutputStream output;
    DataOutputStream dos;
    int len;
    int smblen; 
    InputStream in;
    boolean flag = true;
    DataInputStream clientData;
    BufferedInputStream clientBuff;

    ServerSocket serverSocket = new ServerSocket(5991);
    serverSocket.setSoTimeout(500000);
    System.out.println("Waiting for client on port " + serverSocket.getLocalPort());

    Socket clientSocket = null;
    clientSocket = serverSocket.accept();
    System.out.println("Just connected to " + clientSocket.getRemoteSocketAddress());

    while (true){
        while(flag==true) {  
            in = clientSocket.getInputStream();   
            clientData = new DataInputStream(in);  
            clientBuff = new BufferedInputStream(in); 

            int fileSize = clientData.read();
            if (fileSize != 0)
                System.out.println("Receiving " + fileSize + " files.\n");  

            //Store filenames and file sizes from client directory
            ArrayList<File>files=new ArrayList<File>(fileSize); 
            ArrayList<Integer>sizes = new ArrayList<Integer>(fileSize); 

            //Server accepts filenames
            for (int count=0; count<fileSize; count ++){
                File ff=new File(clientData.readUTF());
                files.add(ff);
            }

            for (int count=0; count<fileSize; count ++){
                sizes.add(clientData.readInt());
            }

            for (int count=0; count<fileSize; count++) {

                if (fileSize - count == 1) {
                    flag = false;
                }

                len = sizes.get(count);

                output = new FileOutputStream("/home/pp/Desktop/inResources/" + files.get(count));
                dos = new DataOutputStream(output);
                bos = new BufferedOutputStream(output);

                byte[] buffer = new byte[1024];  

                bos.write(buffer, 0, buffer.length); 

                while (len > 0 && (smblen = clientData.read(buffer)) > 0) { 
                    dos.write(buffer, 0, smblen); 
                    len = len - smblen;
                    dos.flush();
                }  
                dos.close();  

                System.out.println("File " + files.get(count) + " with " + sizes.get(count) + " bytes recieved.");
            }

        }

        if (flag == false) {
            System.out.println("\nTransfer completed. Closing socket...");
            serverSocket.close();
            break;
        }
    }
}   

TestClient.java

public static void main(String[] args) throws IOException {
    String serverName = "192.168.1.12"; //IP address
    int port = 5991;

    Socket sock = new Socket(serverName, port);
    System.out.println("Connected to " + serverName + " on port " + port + "\n");  


    File myFile = new File("C:\\Users\\inter2\\Desktop\\noobs\\outResources");
    File[] files = myFile.listFiles();

    OutputStream os = sock.getOutputStream();  
    DataOutputStream dos = new DataOutputStream(os); 

    //Sending total number of files in folder
    dos.writeInt(files.length);

    //Sending file names
    for (int count=0; count<files.length; count++) {
        dos.writeUTF(files[count].getName());
    }

    //Sending file sizes
    for (int count=0; count<files.length; count++) {
        int filesize = (int) files[count].length();
        dos.writeInt(filesize);
    }

    //Sending of files
    for (int count=0; count<files.length; count ++) {
        int filesize = (int) files[count].length();
        byte [] buffer = new byte [filesize];

        FileInputStream fis = new FileInputStream(files[count].toString());  
        BufferedInputStream bis = new BufferedInputStream(fis);  

        //Sending file name and file size to the server  
        bis.read(buffer, 0, buffer.length); 

        dos.write(buffer, 0, buffer.length);   
        dos.flush(); 


        System.out.println("Sending file " + files[count].getName() + " with " + filesize + " bytes.");
    }  

    System.out.println("\n" + files.length + " files successfully transfered.");
    sock.close();
}

1 个答案:

答案 0 :(得分:0)

此代码从未奏效。

int fileSize = clientData.read();

此处您正在阅读文件的金额byte

dos.writeInt(files.length)

这里你写的是int。所以你的作家已经超过你的读者三个字节了。

read()更改为readInt()以上。

其他说明:

接收:

 byte[] buffer = new byte[1024];  

这没关系,但是更大的缓冲区效率更高,比如说8192。

 bos.write(buffer, 0, buffer.length); 

这是一个错误。去掉它。您正在文件的开头写入1024个空字节。

 while (len > 0 && (smblen = clientData.read(buffer)) > 0) { 
     dos.write(buffer, 0, smblen); 
     len = len - smblen;
     dos.flush();

不要在内圈冲洗。

 }  

发送:

    byte [] buffer = new byte [filesize];

    FileInputStream fis = new FileInputStream(files[count].toString());  
    BufferedInputStream bis = new BufferedInputStream(fis);  

    //Sending file name and file size to the server  
    bis.read(buffer, 0, buffer.length); 

    dos.write(buffer, 0, buffer.length);   

无需浪费文件大小的缓冲区。使用上面用于接收的相同代码。