如何在 Java 中实现基于线程的 UDP 服务器?

编程入门 行业动态 更新时间:2024-10-26 18:16:12
本文介绍了如何在 Java 中实现基于线程的 UDP 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

How can I implement a threaded UDP based server in Java ?

Basically what I want, is to connect multiple clients to the server, and let each client have his own thread. The only problem is, that I don't know how to check if a client is trying to connect to the server and spawn a new thread for it.

boolean listening = true; System.out.println("Server started."); while (listening) new ServerThread().start();

In this case the server will spawn new threads until it runs out of memory. Here's the code for the ServerThread ( I think I need here a mechanism that stalls the creation of the ServerThread until a client tries to connect.

public ServerThread(String name) throws IOException { super(name); socket = new DatagramSocket(); }

So fathers of Java programming please help.

解决方案

The design for this to a certain extent depends on whether each complete UDP "dialog" just requires a single request and immediate response, whether it's a single request or response with retransmissions, or whether there'll be a need to process lots of packets for each client.

The RADIUS server I wrote had the single request + retransmit model and spawned a thread for each incoming packet.

As each DatagramPacket was received it was passed to a new thread, and then that thread was responsible for sending back the response. This was because the computation and database accesses involved in generating each response could take a relatively long time and it's easier to spawn a thread than to have some other mechanism to handle new packets that arrive whilst old packets are still being processed.

public class Server implements Runnable { public void run() { while (true) { DatagramPacket packet = socket.receive(); new Thread(new Responder(socket, packet)).start(); } } } public class Responder implements Runnable { Socket socket = null; DatagramPacket packet = null; public Responder(Socket socket, DatagramPacket packet) { this.socket = socket; this.packet = packet; } public void run() { byte[] data = makeResponse(); // code not shown DatagramPacket response = new DatagramPacket(data, data.length, packet.getAddress(), packet.getPort()); socket.send(response); } }

更多推荐

如何在 Java 中实现基于线程的 UDP 服务器?

本文发布于:2023-11-23 10:37:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   服务器   如何在   Java   UDP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!