尝试通过同一个sockt发送文件和字符串(在java中)

发布于 2024-11-10 00:20:10 字数 1405 浏览 0 评论 0原文

我需要通过一些sockt(假设端口8478)将文件从服务器发送到客户端,并且还需要按摩(在文件传输的中间)(诸如“嗨”,或“你达到你的极限”或“你达到你的50” %限制”)。

现在只发送文件很容易使用 客户端和服务器端的BufferedInputStrear和BufferedOutputStream。 现在我怎样才能在同一端口(8478)的文件传输中发送消息。

谢谢大家..

这是我传输文件

服务器端的方式:

         BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));

        BufferedOutputStream outStream = new         BufferedOutputStream(cs.getOutputStream());
        ObjectOutputStream msgoutStream = new ObjectOutputStream(cs.getOutputStream());
        byte buffer[] = new byte[1024];


        int read;
        while((read = d.read(buffer))!=-1)
        {

            //msgoutStream.writeUTF("hjlhkhjk");


            outStream.write(buffer, 0, read);

            outStream.flush();
        }

客户端:

         byte buffer[] = new byte[1024];
        int read;
        int f=0;

        while((read = d.read(buffer))!=-1)
        {
            if(ifContinun)
            {
                System.out.println("strat write to file...");
            }

        //String s1=msgInPutStream.readLine();
        //String s2=msgInPutStream.readUTF();

        outStream.write(buffer, 0, read);
            outStream.flush();
            if(ifContinun)
            {
                System.out.println("after write to file...");
                ifContinun=false;
            }
        }

i need to send file from server to client through some sockt (lets say port 8478) and also massage(in middel of file transfer ) (somthing like "hi", or "you reach to your limt"or "you reach to your 50% limt").

now to send only file it's easy im using
BufferedInputStrear and BufferedOutputStream in the client and server side.
now how can i send also massage in middel of file transfer in same port (8478).

thank you all..

this how i transfer the file

server side:

         BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));

        BufferedOutputStream outStream = new         BufferedOutputStream(cs.getOutputStream());
        ObjectOutputStream msgoutStream = new ObjectOutputStream(cs.getOutputStream());
        byte buffer[] = new byte[1024];


        int read;
        while((read = d.read(buffer))!=-1)
        {

            //msgoutStream.writeUTF("hjlhkhjk");


            outStream.write(buffer, 0, read);

            outStream.flush();
        }

client side:

         byte buffer[] = new byte[1024];
        int read;
        int f=0;

        while((read = d.read(buffer))!=-1)
        {
            if(ifContinun)
            {
                System.out.println("strat write to file...");
            }

        //String s1=msgInPutStream.readLine();
        //String s2=msgInPutStream.readUTF();

        outStream.write(buffer, 0, read);
            outStream.flush();
            if(ifContinun)
            {
                System.out.println("after write to file...");
                ifContinun=false;
            }
        }

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

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

发布评论

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

评论(3

无人问我粥可暖 2024-11-17 00:20:10

您需要分部分发送文件。您可以发明一个像这样的协议,

 short stream-id
 short length of message
 bytes of the message

这将允许您在同一个套接字中交错多个数据流,并让另一端分解不同的流。

然而,打开两个连接可能要简单得多,这样就不需要这样的协议。例如 FTP 就是这样做的。 ;)

You need to send the file in parts. You can invent a protocol like

 short stream-id
 short length of message
 bytes of the message

This will allow you to interleave multiple streams of data in the same socket and have the other end break up the different streams.

However, its likely to be much simpler to open two connections which avoids the need for a protocol like this. e.g. FTP does this. ;)

找回味觉 2024-11-17 00:20:10

为此,您需要在 TCP 之上定义一个协议。例如,协议可以是:

  1. 有一系列消息
  2. 每条消息都有一个类型
  3. 每条消息前面有 4 个字节,携带下一条消息的大小
  4. 每条消息以类型字节开头
  5. 类型为: 1 -- StartFile, 2 -- NextFileChunk, 3 -- TextMessage
  6. 第二个字节开始包含消息正文
  7. 对于 StartFile,其余字节构成文件名以及您要发送的任何其他属性。 (您可以选择使用常规 Java 序列化。)
  8. 对于 NextFileChunk,您只需传输文件的接下来的 n 个字节
  9. 对于 TestMessage,其余字节将携带文本消息

In order to do this, you need to define a protocol on top of TCP. For example, the protocol can be:

  1. There are a series of messages
  2. Each message has a type
  3. Each message is preceded by 4 bytes that carry the size of the next message
  4. Each message starts with a type byte
  5. The types are: 1 -- StartFile, 2 -- NextFileChunk, 3 -- TextMessage
  6. The the second byte onwards contains the body of the message
  7. For StartFile, the rest of the bytes constitute the filename and whatever other properties you want to send. (You can choose to use regular Java serialization.)
  8. For NextFileChunk, you just have the next n bytes of the file being transferred
  9. For TestMessage, the rest of the bytes would carry the text message
昔日梦未散 2024-11-17 00:20:10

一种方法是使用一些转义代码来指示何时从文件传输更改为文本传输,反之亦然。由于二进制文件可能包含转义码,因此您必须以某种方式传递这些代码。

但消息协议是更好的选择。

One way is to use some escape code to indicate when changing from file transfer to text transefer, and vice versa. Because a binary file may contain your escape codes, you must hand those some how.

But message protocol is preferable.

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