套接字传输的文件:内容为空

发布于 2024-12-18 01:26:07 字数 1563 浏览 4 评论 0原文

我正在努力通过套接字在两台计算机之间传输文件。一切似乎都正常,但是当我查看检索到的文件的内容时,它是空的。我做错了什么?

这是我的服务器端代码。文件 foobar.txt 存在,其内容为“hello world!”。

try{ 
    ServerSocket ssock = new ServerSocket(12345);
    Socket sock = ssock.accept();
    //here I get the filename from the client, but that works fine.
    File myFile = new File("foobar.txt");
    byte[] mybytearray = new byte[(int) myFile.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
    bis.read(mybytearray, 0, mybytearray.length);
    OutputStream os = sock.getOutputStream();
    os.write(mybytearray, 0, mybytearray.length);
    os.flush();
    sock.close();
} catch (Exception e){
    e.printStackTrace();
}

这是我的客户端代码:

try {
    Socket socket = new Socket(host, port);
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    out.print("get foobar.txt\r\n");
    out.flush();
    byte[] streamIn = new byte[1024];
    InputStream in = socket.getInputStream();
    FileOutputStream file_src = new FileOutputStream("foobar.txt"); 
    BufferedOutputStream file_writer = new BufferedOutputStream(file_src);
    int i;
    while ((i = in.read()) != -1) {
        file_writer.write(i);
    }
    file_writer.flush();
    file_writer.close();
    file_src.close();
    socket.close();
} catch (Exception e) {
   e.printStackTrace();
}

已解决

由于我使用多个线程和多个套接字并测试一台计算机上的所有连接,所以我只是遇到了客户端(同时具有客户端和服务器)的问题其中的代码)将与自身连接,而不是与其他客户端连接。更改不同正在运行的客户端的文件传输端口可以使这一切正常工作。感谢所有看过本文并给我一些建议的人。

I am working on transferring a file between two computers over a socket. Everything seems to work, but when I look at the contents of the retrieved file, it is empty. What am I doing wrong?

Here is my server-side code. The file foobar.txt exists, and its contents are "hello world!".

try{ 
    ServerSocket ssock = new ServerSocket(12345);
    Socket sock = ssock.accept();
    //here I get the filename from the client, but that works fine.
    File myFile = new File("foobar.txt");
    byte[] mybytearray = new byte[(int) myFile.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
    bis.read(mybytearray, 0, mybytearray.length);
    OutputStream os = sock.getOutputStream();
    os.write(mybytearray, 0, mybytearray.length);
    os.flush();
    sock.close();
} catch (Exception e){
    e.printStackTrace();
}

And here is my client code:

try {
    Socket socket = new Socket(host, port);
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    out.print("get foobar.txt\r\n");
    out.flush();
    byte[] streamIn = new byte[1024];
    InputStream in = socket.getInputStream();
    FileOutputStream file_src = new FileOutputStream("foobar.txt"); 
    BufferedOutputStream file_writer = new BufferedOutputStream(file_src);
    int i;
    while ((i = in.read()) != -1) {
        file_writer.write(i);
    }
    file_writer.flush();
    file_writer.close();
    file_src.close();
    socket.close();
} catch (Exception e) {
   e.printStackTrace();
}

Solved

Since I am using multiple threads and multiple sockets and testing all connections on one machine, I was simply running into a problem where the client (which has both the client and server code in it) would connect with itself instead of the other client. Changing the file transfer port for the different running clients got this all to work. Thanks for everyone who had a look at this and gave me some suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乞讨 2024-12-25 01:26:07

也许您在客户端上关闭了错误的套接字。当您关闭套接字时,您将关闭类字段 this.socket 而不是局部变量 socket

此外,当您关闭文件的输出流时,不必同时关闭 BufferedOutputStream 和 FileOutputStream。当 BufferedOutputStream 关闭时,FileOutputStream 也会自动关闭。

还有一件事——您不必在关闭输出流之前刷新它。当您调用 close() 时,流会自动刷新。

Maybe you're closing the wrong socket on the client. When you close the socket, you're closing the class field this.socket instead of the local variable socket.

Also, when you close the output stream to the file, you don't have to close both the BufferedOutputStream and the FileOutputStream. The FileOutputStream is automatically closed when the BufferedOutputStream is closed.

One more thing---you don't have to flush an output stream before closing it. When you call close() the stream is automatically flushed.

你怎么敢 2024-12-25 01:26:07

除了其他人所说的之外,您还忽略了 bis.read() 的结果。 它不能保证填充缓冲区。请参阅 Javadoc。

在 Java 中复制流的正确方法(应该在两端都使用)是这样的:

byte[] buffer = new byte[8192]; // or whatever
int count;
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}

In addition to what everyone else has said, you are ignoring the result of bis.read(). It isn't guaranteed to fill the buffer. See the Javadoc.

The correct way to copy streams in Java, which you should use at both ends, is this:

byte[] buffer = new byte[8192]; // or whatever
int count;
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}
〆凄凉。 2024-12-25 01:26:07

我唯一想到的是,您实际上从未开始接收文件,因为服务器端不读取命令(“get foobar.txt”),因此客户端冻结发送命令。

该文件在客户端的存在可能来自之前的测试。

但是,我不确定这就是问题所在。这只是尝试提供帮助。

The only thing I think of that is that you actually never start receiving the file because the server-side doesn't read the command ("get foobar.txt"), so the client-side freezes on sending the command.

The existence of the file at the client-side might be from previous tests.

But, I'm not sure this is the problem. It's just a try to help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文