如何创建能够扩展到 10000 个(不相关)连接的 UDP 服务器?
目前我正在试验这段代码(我知道它不符合目的)。 我尝试同时从 3 个源发送(UDP 测试工具),看起来没问题,但我想知道如果 2K 的 10K 可能客户端同时发送,这会如何表现?数据包大小约为 70 字节。我应该对内容进行一些简单的操作并将结果写入数据库。
公开课测试{
public static void main(String [] args){
int PACKETSIZE=1400;
int port=5555;
byte[] bytes = new byte[PACKETSIZE];
//ByteBuffer bb = ByteBuffer.allocate(4);
//Byte lat=null;
try
{
DatagramSocket socket = new DatagramSocket(port);
System.out.println("The server is runing on port " + port +"\n");
while (true)
{
DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
socket.receive(packet);
System.out.println("Packet length = " + packet.getLength());
System.out.println("Sender IP = " + packet.getAddress() + " Port = " + packet.getPort());
for(int i=0; i<=packet.getLength();i++){System.out.print(" "+ packet.getData()[i] + " ");}
Currently I'm experimenting with this code (I know it doesn't fit the purpose).
I tried sending from 3 sources simultaneously (UDP Test Tool) and it seems ok, but I wan't to know how this would behave if form those 10K possible clients 2K are sending at the same time? The packets are approximately 70 bytes in size. I'm supposed do to some simple operations on the contents and write the results to a database.
public class Test{
public static void main(String [] args){
int PACKETSIZE=1400;
int port=5555;
byte[] bytes = new byte[PACKETSIZE];
//ByteBuffer bb = ByteBuffer.allocate(4);
//Byte lat=null;
try
{
DatagramSocket socket = new DatagramSocket(port);
System.out.println("The server is runing on port " + port +"\n");
while (true)
{
DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
socket.receive(packet);
System.out.println("Packet length = " + packet.getLength());
System.out.println("Sender IP = " + packet.getAddress() + " Port = " + packet.getPort());
for(int i=0; i<=packet.getLength();i++){System.out.print(" "+ packet.getData()[i] + " ");}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,UDP套接字不是面向连接的,因此“连接”的数量没有意义。您真正关心的数字是每秒数据报的数量。通常被忽视的另一个问题是数据报是否跨越 IP 数据包,因为这会影响数据包组装时间,并最终影响接收数据报的成本。您的数据包大小为 1,400,可以轻松放入以太网帧中。
现在,您需要做的是使用多线程、队列或其他一些处理方案来限制处理时间。您希望接收线程忙于从线路上拉出数据报并将它们放在其他地方供工作人员处理。这是一种常见的处理习惯用法,已使用多年。如果您可以将数据处理与网络 IO 分开,它应该可以扩展以满足您的需求。
您还可以使用异步或事件驱动的 IO,这样就没有线程负责直接从套接字读取数据报。有关 Java NIO 的讨论,请参阅此问题。
我不确定这是否是作业,但你应该阅读C10K 问题 Dan凯格尔关于这个主题的优秀文章。我认为至少可以说,您可能会发现它很有启发性。
Firstly UDP sockets are not connection oriented so the number of "connections" is meaningless. The number that you actually care about is number of datagrams per second. The other issue that is normally overlooked is whether the datagrams span IP packets or not since that affects packet assembly time and, ultimately, how expensive they are to receive. Your packet size is 1,400 which will fit comfortably in an Ethernet frame.
Now, what you need to do is limit your processing time using multiple threads, queueing, or some other processing scheme. You want the receiving thread busy pulling datagrams off of the wire and putting them somewhere else for workers to process. This is a common processing idiom that has been in use for years. It should scale to meet your needs provided that you can separate the processing of the data from the network IO.
You can also use asynchronous or event-driven IO so that you do not have a thread responsible for reading datagrams from the socket directly. See this question for a discussion of Java NIO.
I'm not sure if this is homework or not, but you should read The C10K problem Dan Kegel's excellent article on this very subject. I think that you will probably find it enlightening to say the least.
查看这两个开源项目:
另请查看此博客文章:
http://urbanairship.com/blog/ 2010/08/24/c500k-in-action-at-urban-airship/
Check out these two open source projects :
Also check this blog post:
http://urbanairship.com/blog/2010/08/24/c500k-in-action-at-urban-airship/