使用java中的数据报将视频/音频从客户端发送到服务器?

发布于 2024-08-12 03:57:47 字数 1797 浏览 6 评论 0原文

大家好,我在 UDP 和数据报方面遇到了一些问题。我应该创建一个服务器,它将接收客户端的请求以在同一目录中发送文件。然后UDP服务器将获取该文件(视频),将其放入数据报中并发送。我想我知道该怎么做,但我无法将文件放入数据报中。我将其以二进制形式表示,因此请记住这一点。

到目前为止,这是我的代码: 编辑:顺便说一句,这是服务器,我一直在使用 BufferedInputReader 和 OutputReader 时遇到问题,所以请记住这一点:)

   Scanner inFromUser = new Scanner(System.in);
    int port = 12345;
    DatagramSocket server = new DatagramSocket(port);
  // Read name of file supplied by client (must be a line of text):
    Scanner in = new Scanner(new DataInputStream(server.getInputStream()));
    String filename = in.nextLine();
    DatagramSocket request = server.accept();


    // Create buffer, then we're ready to go:
    // Puts file into binary form
        BufferedInputStream inbinary = 
                new BufferedInputStream(new FileInputStream("poop.txt"));
   // Outputs the binary form
        BufferedOutputStream outbinary = 
                new BufferedOutputStream(request.getOutputStream());

    int numbytes;
    int countblocks = 0;
    int countbytes = 0;
    byte[] buf = new byte[1024];
    DatagramPacket packet = new DatagramPacket(buf, buf.length, port);
    server.receive(packet);

    while ((numbytes = inbinary.read(buf,0,1024)) >= 0)
    {
     // receive packet from client, telling it to send the video file
     server.receive(packet);
     InetAddress address = packet.getAddress();
     packet = new DatagramPacket(buf, buf.length, address, port);
     server.send(packet);
     countblocks++;          // keep statistics on file size
     countbytes += numbytes;
     outbinary.write(buf,0,numbytes); // write buffer to socket
    }
      outbinary.flush(); // FLUSH THE BUFFER
      server.close(); // done with the socket
      System.out.println(countblocks + " were read; " + countbytes + " bytes");
    }
  }

Hey everyone, I'm having a bit of a problem with UDP and Datagrams. I'm supposed to make a server that will get a request from the client to send a file in the same directory. The UDP Server will then get this file (a video), put it into a datagram and send it. I think I know how to do it, but I can't put the file in the datagram. I'm putting it in Binary form, so keep that in mind.

Here's my code so far:
edit: This is the server by the way, and I keep having trouble with BufferedInputReader and OutputReader, so keep that in mind :)

   Scanner inFromUser = new Scanner(System.in);
    int port = 12345;
    DatagramSocket server = new DatagramSocket(port);
  // Read name of file supplied by client (must be a line of text):
    Scanner in = new Scanner(new DataInputStream(server.getInputStream()));
    String filename = in.nextLine();
    DatagramSocket request = server.accept();


    // Create buffer, then we're ready to go:
    // Puts file into binary form
        BufferedInputStream inbinary = 
                new BufferedInputStream(new FileInputStream("poop.txt"));
   // Outputs the binary form
        BufferedOutputStream outbinary = 
                new BufferedOutputStream(request.getOutputStream());

    int numbytes;
    int countblocks = 0;
    int countbytes = 0;
    byte[] buf = new byte[1024];
    DatagramPacket packet = new DatagramPacket(buf, buf.length, port);
    server.receive(packet);

    while ((numbytes = inbinary.read(buf,0,1024)) >= 0)
    {
     // receive packet from client, telling it to send the video file
     server.receive(packet);
     InetAddress address = packet.getAddress();
     packet = new DatagramPacket(buf, buf.length, address, port);
     server.send(packet);
     countblocks++;          // keep statistics on file size
     countbytes += numbytes;
     outbinary.write(buf,0,numbytes); // write buffer to socket
    }
      outbinary.flush(); // FLUSH THE BUFFER
      server.close(); // done with the socket
      System.out.println(countblocks + " were read; " + countbytes + " bytes");
    }
  }

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

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

发布评论

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

评论(1

极度宠爱 2024-08-19 03:57:47

我有一段时间没有做过数据报了,但我很确定 accept() 调用是错误的。那是针对 TCP 服务器的。

我建议抄袭 Sun 的优秀教程: http:// java.sun.com/docs/books/tutorial/networking/datagrams/clientServer.html

I haven't done datagrams in a while, but I'm pretty sure the accept() call is wrong. That's for TCP servers.

I'd recommend cribbing from Sun's excellent tutorial: http://java.sun.com/docs/books/tutorial/networking/datagrams/clientServer.html

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