简体   繁体   English

如何在 Java 中设计一个线程化的 UDP 客户端/服务器?

[英]How to design a threaded UDP client/server in Java?

I'm just looking for some insight into program design, since I'm not that experienced with threads.我只是在寻找对程序设计的一些见解,因为我对线程没有那么丰富的经验。

Currently programming a little client.目前正在编写一个小客户端。 You can specify if that client is the "server" or "client" and it will create the protocols in the correct manner.您可以指定该客户端是“服务器”还是“客户端”,它将以正确的方式创建协议。

I do a little handshake between them (a syn, ack, synack sort of thing) and then start the network thread.我在他们之间做了一个小小的握手(一个syn、ack、synack之类的东西),然后启动网络线程。

I have the main thread, who has an infinite while loop and does two things.我有主线程,它有一个无限的while循环并做两件事。

  1. Adds a message to a "toSend" queue.将消息添加到“toSend”队列。
  2. Pulls in all the messages from the "received" queue and prints them.从“已接收”队列中拉入所有消息并打印它们。

The network thread has an infinite while loop that does two things.网络线程有一个无限的while循环,它做两件事。

  1. Pulls in all the messages from the "toSend" queue and sends them.从“toSend”队列中拉入所有消息并发送它们。
  2. Adds a message to the "received" queue (that was fetched).将消息添加到“已接收”队列(已获取)。

The queues are currently ArrayBlockingQueue<String>(1000)队列当前为ArrayBlockingQueue<String>(1000)

My question is the following:我的问题如下:

Is this towards a good design of a network thread?这是对网络线程的良好设计吗? Are there any gotchas I need to handle?有什么我需要处理的问题吗?

Currently I've had an issue where one of the threads (the main thread) is filling the "toSend" queue even before the network thread has a chance to send even one message.目前,我遇到了一个问题,其中一个线程(主线程)甚至在网络线程有机会发送一条消息之前就填充了“toSend”队列。 I've "handled" this by making the main thread do some work that blocks (I/O).我已经通过让主线程做一些阻塞(I/O)的工作来“处理”这个问题。

It seems like you are re-inventing the wheel and are struggling with issues others have solved (and most probably with an optimized solution) before, please take a look at Netty :似乎您正在重新发明轮子并且正在努力解决其他人已经解决的问题(并且很可能是使用优化的解决方案),请看一下Netty

The Netty project is an effort to provide an asynchronous event-driven network application framework and tools for rapid development of maintainable high performance & high scalability protocol servers & clients. Netty 项目致力于提供异步事件驱动的网络应用程序框架和工具,用于快速开发可维护的高性能和高可扩展性协议服务器和客户端。

There is an example for an broadcast UDP/IP client and server.有一个广播 UDP/IP 客户端和服务器的示例。

Personally I think that handling UDP networking is best done using a single threaded, event driven programming style, rather than using threads.我个人认为处理 UDP 网络最好使用单线程、事件驱动的编程风格,而不是使用线程。 It is stateless anyhow, so it makes little sense trying to track the state in a thread.无论如何它是无状态的,因此尝试在线程中跟踪 state 毫无意义。

Things like timeouts are easily handled with timers adding events to the event queue.使用将事件添加到事件队列的计时器可以轻松处理诸如超时之类的事情。

This effectively sidesteps all the synchronisation issues.这有效地回避了所有同步问题。

I have been working on UDP/TCP real time systems for more than 4 years now.我已经在 UDP/TCP 实时系统上工作了 4 年多了。 Your question has a lot of sense.你的问题很有道理。 In my humble opinion, the Netty framework is a good way to go but a bit heavy.以我的拙见,Netty 框架是 go 的好方法,但有点重。

I am using the exact same design that you mentioned and have seen no issues with it so far.我正在使用您提到的完全相同的设计,到目前为止还没有发现任何问题。

Regards,问候,

The 'toSend' queue is pointless. 'toSend' 队列毫无意义。 Just have everybody send directly via the socket.让每个人直接通过套接字发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM