2

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] + " ");} 
V4R15
  • 97
  • 1
  • 7

2 Answers2

3

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.

Community
  • 1
  • 1
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
0

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/

Yazan Jaber
  • 2,068
  • 25
  • 36