在同一套接字上发送文件后的字符串。

huangapple go评论100阅读模式
英文:

Sending a string after a file on the same socket

问题

I'm sending a string over the socket I previously sent a file to, but the recipient reads it as part of the file itself, is there a way to send a sort of EOF before sending the string?

我正在通过之前用于发送文件的套接字发送一个字符串,但接收方将其视为文件的一部分,是否可以在发送字符串之前发送一种EOF(结束符号)?

To send the file I'm using

发送文件时我正在使用以下代码:

byte[] buffer = new byte[1024];
int count;
while ((count = fis.read(buffer)) >= 0) os.write(buffer, 0, count);
os.flush();

(and almost the same to receive it)

(接收文件的代码几乎相同)

To send the string I'm using OutputStreamWriter

发送字符串时,我正在使用OutputStreamWriter

(Here you are my code: hatebin)

(这是我的代码:hatebin)

I've also read here that I should send a SOH character, but which one should I send and how?

我还在这里读到应该发送一个SOH字符,但应该发送哪个字符以及如何发送呢?

Thanks in advance.

提前感谢您。

英文:

I'm sending a string over the socket I previously sent a file to, but the recipient reads it as part of the file itself, is there a way to send a sort of EOF before sending the string?

To send the file I'm using

byte[] buffer = new byte[1024];
int count;
while ((count = fis.read(buffer)) >= 0) os.write(buffer, 0, count);
os.flush();

(and almost the same to receive it)

To send the string I'm using OutputStreamWriter

(Here you are my code: hatebin)

I've also read here that I should send a SOH character, but which one should I send and how?

Thanks in advance.

答案1

得分: 1

I understand your request. Here's the translated content:

现在没有办法发送“eof”,然后在之后发送内容。

如果您不想打开新的连接,基本上有两种方法可以解决这个问题。

  • 您可以修改客户端,使其识别某些特殊的字节序列作为“分隔符”,并在从套接字读取分隔符时停止向文件中写入。在这种情况下,您需要有一些策略来处理文件实际上包含分隔符的可能性。
  • 您可以在发送文件之前发送文件的大小(以字节为单位),并修改客户端,使其计算从套接字读取的字节数。当客户端读取足够数量的字节时,它应该停止向文件中写入。
英文:

No there's no way to send an "eof" and then send something afterwards.

If you don't want to open a new connection, there are basically two ways to solve this.

  • You can modify the client so it recognizes some special byte sequence as a "delimiter", and stops writing to the file when it reads the delimiter from the socket. In this case you need to have some strategy to deal with the possibility that the file actually contains the delimiter.
  • You can send the size of the file in bytes before sending the file, and modify the client so it counts the number of bytes it reads from the socket. When the client has read enough, it should stop writing to the file.

huangapple
  • 本文由 发表于 2020年8月6日 17:34:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63280768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定