如何在Java中实现基于线程的UDP服务器? [英] How can I implement a threaded UDP based server in Java?

查看:108
本文介绍了如何在Java中实现基于线程的UDP服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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();

在这种情况下,服务器将生成新线程,直到内存不足为止。
这是ServerThread的代码(我想我需要一种机制,在客户端尝试连接之前停止创建ServerThread。

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();
}

所以Java编程的父亲请帮忙。

So fathers of Java programming please help.

推荐答案

这种设计在一定程度上取决于每个完整的UDP对话框是否只需要一个请求和立即响应,无论是单个请求还是带有重传的响应,或者是否需要为每个客户端处理大量数据包。

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.

我写的RADIUS服务器有单个请求+重传模型,并为每个传入数据包生成一个线程。

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

当收到每个 DatagramPacket 时,它被传递给一个新线程,然后该线程负责se回复回应。这是因为生成每个响应所涉及的计算和数据库访问可能需要相对较长的时间,并且产生一个线程比使用其他机制来处理在旧数据包仍处理时到达的新数据包更容易。

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服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆