首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过TCP/IP传输文件在Windows下工作,但不使用Ubuntu

通过TCP/IP传输文件在Windows下工作,但不使用Ubuntu
EN

Stack Overflow用户
提问于 2016-12-15 04:04:50
回答 1查看 58关注 0票数 0

本指南的帮助下,我尝试使用TCP/IP连接发送多个文件。

当我从Ubuntu14.04执行我的testClient代码时,文件在Windows7中被传输到testServer。这里收到的.txt文件格式正确。

但是,当我从Windows7执行testClients代码和从Ubuntu14.04执行testServer时,Ubuntu14.04中接收到的文件就被搞乱了。( txt#2内容的内容溢出到txt#1。)

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

我很困惑。为什么这些代码在Windows 7中工作得很好,而在Ubuntu中却不行呢?我的密码有问题吗?我希望能在这方面提供任何帮助。

TestServer.java

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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();
}
EN

回答 1

Stack Overflow用户

发布于 2016-12-15 04:21:15

这段代码根本没用。

代码语言:javascript
运行
复制
int fileSize = clientData.read();

在这里,您正在以byte的形式读取文件的数量。

代码语言:javascript
运行
复制
dos.writeInt(files.length)

在这里,您写的金额,作为一个int。因此,您的作者已经比您的读者领先三个字节。

将上面的read()更改为readInt()

其他说明:

接收:

代码语言:javascript
运行
复制
 byte[] buffer = new byte[1024];  

这是可以的,但是更大的缓冲区会更有效,比如8192。

代码语言:javascript
运行
复制
 bos.write(buffer, 0, buffer.length); 

这是个窃听器。把它移开。您正在文件的开头写入1024个空字节。

代码语言:javascript
运行
复制
 while (len > 0 && (smblen = clientData.read(buffer)) > 0) { 
     dos.write(buffer, 0, smblen); 
     len = len - smblen;
     dos.flush();

不要冲进圈内。

代码语言:javascript
运行
复制
 }  

发送:

代码语言:javascript
运行
复制
    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);   

不需要浪费文件大小的缓冲区。使用与接收上述代码相同的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41156250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档