UDP 服务器处理多个客户端

发布于 2024-10-12 07:05:36 字数 619 浏览 2 评论 0原文

我创建了一个 UDP 服务器。这是一个框架

public class UDPserver {
      public static void main(String[] args) throws Exception{
           while(true){
              .... some code ...
              packet = new DatagramPacket ( data , data.length, packet.getAddress(), packet.getPort() );
              .... some code ...
              socket.receive( ... );
           }
      }
}

现在,我想让它处理多个请求,所以我检查了我必须实现 Runnable。

public class UDPserver implements Runnable { }

我读到我还需要一个 run()。但我不明白 run()。我应该将整个 while(true) 语句放在 run() 中吗? main() 呢?谁能展示如何更改上面的代码来处理多个请求? 谢谢

I created a UDP server. Here's a skeleton

public class UDPserver {
      public static void main(String[] args) throws Exception{
           while(true){
              .... some code ...
              packet = new DatagramPacket ( data , data.length, packet.getAddress(), packet.getPort() );
              .... some code ...
              socket.receive( ... );
           }
      }
}

Now, i want to make it handle multiple requests, so i checked out that i have to implement Runnable.

public class UDPserver implements Runnable { }

I read that I also need to have a run(). But i don't understand run(). should i put the whole while(true) statement inside run()? what about main()? Can anyone show how to change my code above to handle multiple requests?
thanks

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

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

发布评论

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

评论(2

随遇而安 2024-10-19 07:05:36

将所有代码移至 UDPServer 的 run 方法中(包括 while(true))

在您的 main 方法中:


UDPServer udpServer = new UDPServer();
udpServer.start();

  • 为了确保没有异常不会破坏您的主循环,请记住捕获并处理可能在 UDPServer 内部引发的所有异常while(true) 循环

move all the code inside the run method of UDPServer (including the while(true))

In your main method :


UDPServer udpServer = new UDPServer();
udpServer.start();

  • To make sure that no excpetion won't break your main loop, remember to catch and handle all exceptions that might be rasied inside the while(true) loop
謸气贵蔟 2024-10-19 07:05:36

您还可以为每个新连接使用新线程来执行。例如:

 class PacketPerforming extends Thread {
 DatagramPacket pak; 
 PacketPerforming(DatagramPacket pak) {
  super();
  this.pak = pak;
 } 

 public void run() {
  // do somethoing with pak
 }

 }

 // in your server thread
 while (true) { // i prefered wirte for (;;)

  DatagramPacket pak; // take pak object form remote socket

  PacketPerforming perform = new PacketPerforming(pak);
  perform.start();

 }

You can also use new thread for each new connection for performing. For example:

 class PacketPerforming extends Thread {
 DatagramPacket pak; 
 PacketPerforming(DatagramPacket pak) {
  super();
  this.pak = pak;
 } 

 public void run() {
  // do somethoing with pak
 }

 }

 // in your server thread
 while (true) { // i prefered wirte for (;;)

  DatagramPacket pak; // take pak object form remote socket

  PacketPerforming perform = new PacketPerforming(pak);
  perform.start();

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