当前位置: 首页 > 知识库问答 >
问题:

通过套接字双向文件传输中的输入流干扰

吴腾
2023-03-14

在用你们的建议编辑我的代码之后,以及将代码压缩到我可以查明导致问题的代码行的位置。服务器代码:'

公共类服务器 2 {

public static void main(String args[]) throws Exception {

    ServerSocket servsock = null;
    Socket sock = null;
    int SOCKET_PORT = 12362;

    InputStream is = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;

    OutputStream os = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;

    String FILE_TO_SEND = "SFileToBeSent.txt";
    String FILE_TO_RECEIVED = "CFileReceived.txt";
    int FILE_SIZE = 6022386;

    try {

        System.out.println("Server created.");
        servsock = new ServerSocket(SOCKET_PORT); // creates servsock with socket port
        while (true) {

            System.out.println("Waiting for connection...");
            try {

                sock = servsock.accept(); // accepts a socket connection
                System.out.println("Accepted connection : " + sock);
                System.out.println();

                os = sock.getOutputStream(); // sets Output Stream using the sockets output stream
                is = sock.getInputStream(); // get input stream from socket

                // ----------------------------------------------------------------------------------------------------
                // ----------------------------------------------------------------------------------------------------

                // send file
                File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
                byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile

                try {

                    // reads the contents of FILE_TO_SEND into a BufferedInputStream
                    fis = new FileInputStream(myFile); // creates new FIS from myFile
                    bis = new BufferedInputStream(fis); // creates BIS with the FIS
                    bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array

                    // prints to console filename and size
                    System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
                    System.out.println("byte array from file to send:" + bytearraySent);

                    // copies the byte array into the output stream therefore sending it through the
                    // socket
                    os.write(bytearraySent, 0, bytearraySent.length);
                    os.flush(); // flush/clears the output stream
                } finally {

                    fis.close();
                    bis.close();
                }

                System.out.println();
                System.out.println("sendFile complete");
                System.out.println();

                // ----------------------------------------------------------------------------------------------------
                // ----------------------------------------------------------------------------------------------------

                // receive file
                int bytesRead;

                byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
                fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
                bos = new BufferedOutputStream(fos); // creates BOS from FOS
                int current = 0; // equals the two integers for comparisons later
                try {

                    System.out.println(String.format("Bytes available from received file:  %d", is.available()));

                    System.out.println("byte array from file to receive:  " + bytearrayReceived); // debug purposes

                    while ((bytesRead = is.read(bytearrayReceived)) != -1) {

                        System.out.println("amount of bytes that was read for while:  " + bytesRead);

                        bos.write(bytearrayReceived, 0, bytesRead);
                        System.out.println("bos.write");

                        current += bytesRead;
                        System.out.println("current += bytesRead;");
                    }
                    System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
                } finally {

                    fos.close();
                    bos.close();
                }

                System.out.println();
                System.out.println("receiveFile complete");
                System.out.println();

                // ----------------------------------------------------------------------------------------------------
                // ----------------------------------------------------------------------------------------------------

            } // end of try
            finally {

                // if any streams or sockets are not null, the close them
                if (os != null)
                    os.close();
                if (is != null)
                    is.close();
                if (sock != null)
                    sock.close();
            } // end of finally
        } // end of while
    } // end of try
    finally {

        if (servsock != null)
            servsock.close();
    } // end of finally
}

}

公共类Client2 {

public static void main(String args[]) throws Exception {

    Socket sock = null; // used in main
    String SERVER = "localhost"; // local host
    int SOCKET_PORT = 12362; // you may change this

    InputStream is = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;

    OutputStream os = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;

    String FILE_TO_RECEIVED = "SFileReceived.txt";
    String FILE_TO_SEND = "CFileToBeSent.txt";
    int FILE_SIZE = 6022386;

    try {

        sock = new Socket(SERVER, SOCKET_PORT);
        System.out.println("Connecting...");
        System.out.println();

        // get input and output from socket
        is = sock.getInputStream(); // get input stream from socket
        os = sock.getOutputStream(); // get output stream from socket

        // ----------------------------------------------------------------------------------------------------
        // ----------------------------------------------------------------------------------------------------

        // receive file
        int bytesRead;

        byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
        fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
        bos = new BufferedOutputStream(fos); // creates BOS from FOS
        int current = 0; // equals the two integers for comparisons later
        try {

            System.out.println(String.format("Bytes available from received file:  %d", is.available()));

            System.out.println("byte array from file to receive:  " + bytearrayReceived); // debug purposes

            while ((bytesRead = is.read(bytearrayReceived)) != -1) {
                System.out.println("amount of bytes that was read for while:  " + bytesRead);

                bos.write(bytearrayReceived, 0, bytesRead);
                System.out.println("bos.write");

                current += bytesRead;
                System.out.println("current += bytesRead;");

            }
            System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
        } finally {

            fos.close();
            bos.close();
        }

        System.out.println();
        System.out.println("receiveFile() complete");
        System.out.println();

        // ----------------------------------------------------------------------------------------------------
        // ----------------------------------------------------------------------------------------------------

        // send file
        File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
        byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile

        try {

            // reads the contents of FILE_TO_SEND into a BufferedInputStream
            fis = new FileInputStream(myFile); // creates new FIS from myFile
            bis = new BufferedInputStream(fis); // creates BIS with the FIS
            bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array

            // prints to console filename and size
            System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
            System.out.println("byte array from file to send:" + bytearraySent);

            // copies the byte array into the output stream therefore sending it through the
            // socket
            os.write(bytearraySent, 0, bytearraySent.length);
            os.flush(); // flush/clears the output stream
        } finally {

            fis.close();
            bis.close();
        }

        System.out.println();
        System.out.println("sendFile() complete");
        System.out.println();

        // ----------------------------------------------------------------------------------------------------
        // ----------------------------------------------------------------------------------------------------

    } // end of try
    finally {

        if (sock != null)
            sock.close();
        if (os != null)
            os.close();
        if (is != null)
            is.close();
    } // end of finally
}

}

服务器输出:--------------------

服务器已创建。

等待连接...

接受的连接:Socket[addr=/127.0.0.1, port=51565, localport=12362]

正在发送SFileToBeSent。txt(32字节)

从文件发送的字节数组:[B@4e25154f

发送文件完成

接收到的文件可用字节数:0

要接收的文件中的字节数组:[B@70dea4e

客户端输出--------------------

连接...

从接收的文件中可用的字节数:32

要接收的文件中的字节数组:[B@4e25154f

读取的字节数:32

bos.write

电流=字节读取

我仍然遇到 InputStream 的问题,并且通过调试我发现服务器和客户端都卡在接收部分中的 while() 语句上。对于服务器,它立即停止,而客户端通过 while 循环一次,然后在点击 while 语句时停止。

如果任何人有任何建议或解决方案,那么将不胜感激!

共有1个答案

卞俊贤
2023-03-14

首先,您的

公共静态无效接收文件()抛出IOExc的{

byte[] mybytearray = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later

try {
    while ((bytesRead = is.read(mybytearray)) != -1) {
        bos.write(mybytearray, 0, bytesRead );
        current += bytesRead;
    }
    bos.flush(); // clears the buffer
    System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
  bos.close();
}

}

 类似资料:
  • 问题内容: 当我使用套接字编程传输大文件时,收到的文件不完整,即它是一个mp3文件,当我播放时听起来很奇怪。代码是: 服务器端: 客户端: 在客户端,我只是为了简化而使用(我可以从服务器端发送文件的长度)。 如果客户端和服务器是同一台机器,则此代码可以完美地工作,但是如果它们位于不同的机器上,则文件会失真。 问题答案: 在Java中复制流的规范方法: 适用于大于零的任何缓冲区大小。应避免避免将缓冲

  • Netty TCP Server在端口处运行,接收格式的数据。它使用Marine API库将胡言乱语转换为需要从套接字输入流的有意义的信息。 如何获得正在使用的netty服务器端口的输入流?

  • 问题内容: 我正在尝试在C中创建一个多线程服务器- 客户端文件传输系统。有些客户端将发送或列出或做其他选择(在交换机的情况下,您可以看到),而服务器则存储文件并提供大量服务客户。 就我所知,多线程意识形态确实很困难。它需要太多的经验而不是知识。我已经在该项目上工作了一个多星期,但我一直无法解决这些问题。 有4个选择:第一个是在其目录中列出客户端的本地文件,第二个是在客户端和服务器之间传输的列表文件

  • 这是接收文件的服务器代码 这是发送文件的客户端代码

  • 问题 你希望你的脚本接受任何用户认为最简单的输入方式。包括将命令行的输出通过管道传递给该脚本、 重定向文件到该脚本,或在命令行中传递一个文件名或文件名列表给该脚本。 解决方案 Python内置的 fileinput 模块让这个变得简单。如果你有一个下面这样的脚本: #!/usr/bin/env python3 import fileinput with fileinput.input() as

  • 我试图移动一些文件从存储目录到远程服务器"BOB"使用jcifs SMB.我需要使用jcifs,因为移动通过和Android应用程序,即。我正在将照片从应用程序移动到远程服务器。为了验证为用户,我的公司使用Active Directory。有一段时间,Active Directory和照片所在的服务器是同一个服务器,一切都很好。不幸的是,他们不得不将存储Active Directory的服务器更改