Java套接字编程头

发布于 2024-09-03 06:01:28 字数 748 浏览 4 评论 0原文

我正在尝试通过 java 套接字发送文件并通过另一个套接字接收它。然而,这种情况发生了:

发送内容:

/* 这只是一个要传输的文件 */

已收到:

所以基本上我无法在堆栈溢出时转义收到的内容。它基本上是一堆不可读的字节(大约 32 个字节),然后是我发送的消息。

OutputStream os = sock.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(mybytearray);
oos.flush();
oos.close();

对于客户来说:

BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
FileOutputStream fos = new FileOutputStream("newfile.java");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
bos.write(mybytearray, 0 , current);
bos.flush();
bos.close();
sock.close();

I am trying to send a file through a java socket and receive it through another. However, this happens:

Send Content:

/*
This is simply a file to transfer
*/

Received:

so basically I cannot escape the received content on stack overflow. It is basically a bunch of unreadable bytes (about 32 bytes worth) and then the message I sent.

OutputStream os = sock.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(mybytearray);
oos.flush();
oos.close();

And for the client:

BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
FileOutputStream fos = new FileOutputStream("newfile.java");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
bos.write(mybytearray, 0 , current);
bos.flush();
bos.close();
sock.close();

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

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

发布评论

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

评论(3

少女的英雄梦 2024-09-10 06:01:28

为什么要通过ObjectOutputStream?可以直接通过 write 方法使用 OutputStream,也可以选择例如 DataOutputStream(如果您认为有必要)。

此外,您不应该使用Reader类来传输二进制数据。从Reader的api:

用于读取字符流的抽象类。


完整的“通过套接字发送文件”示例可以在这里找到:

通过套接字传输文件

关键服务器端代码:

  OutputStream os = sock.getOutputStream();
  System.out.println("Sending...");
  os.write(mybytearray,0,mybytearray.length);
  os.flush();
  sock.close();

关键客户端代码:

bytesRead = is.read(mybytearray,0,mybytearray.length);

Why go through ObjectOutputStream? Either use the OutputStream directly through the write method, or choose for instance DataOutputStream if you find it neccessary.

Besides, you should not use the Reader classes for transferring binary data. From the api of Reader:

Abstract class for reading character streams.


A complete "send file over socket" example can be found here:

Transfer a file via Socket.

Key server-side code:

  OutputStream os = sock.getOutputStream();
  System.out.println("Sending...");
  os.write(mybytearray,0,mybytearray.length);
  os.flush();
  sock.close();

Key client-side code:

bytesRead = is.read(mybytearray,0,mybytearray.length);
╄→承喏 2024-09-10 06:01:28

查看代码会有所帮助,但你得到的看起来像是基本的随机数据。需要检查的一些事项:

  1. 接收缓冲区是什么类型?
  2. 您似乎正在发送和接收多少个字符?
  3. 您确定接收端的打印语句指向正确的位置吗?

It would help to see the code, but what you've got there looks like basic random data. Some things to check:

  1. What type is the receive buffer?
  2. How many characters do you seem to be sending and receiving?
  3. Are you sure your print statement on the receive end is pointing at the right place?
地狱即天堂 2024-09-10 06:01:28

这没有任何意义。如果您使用 ObjectOutputStream,则必须使用 ObjectInputStream 来读取它。如果您想使用 Reader 来读取它,则必须使用 Writer 来写入它。

This doesn't make any sense. If you use an ObjectOutputStream you have to use an ObjectInputStream to read it. If you want to use a Reader to read it you have to use a Writer to write it.

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