001    package org.protocol;
002    
003    import java.util.*;
004    import java.io.*;
005    import java.net.*;
006    import java.awt.*;
007    import java.awt.event.*;
008    import javax.swing.*;
009    import javax.swing.event.*;
010    
011    public class UDPServerSocket {
012    
013            private DatagramPacket packet_;
014            private DatagramSocket socket_;
015            private int port_;
016            private int timeout_;
017            private int buf_size_;
018            private ArrayList<SimpleUDPServerConnection> connection_list_
019                                                             = new ArrayList<SimpleUDPServerConnection>();
020            volatile private boolean stop_ = false;
021    
022            public UDPServerSocket(int port, int timeout, int buf_size) throws Exception {
023                    port_ = port;
024                    timeout_ = timeout;
025                    buf_size_ = buf_size;
026                    init();
027            }
028    
029            private void init() throws Exception {
030                    byte[] buf_ = new byte[buf_size_];
031                    packet_ = new DatagramPacket(buf_, buf_.length);
032                    socket_ = new DatagramSocket(port_);
033            }
034    
035            public void start() throws Exception {
036                    alert("now waiting...");
037    
038                    long total = 0;
039                    int counter=0;
040                    while(!stop_&&counter++<999) {
041                            long start = System.currentTimeMillis();
042                            socket_.receive(packet_); // <block>
043    //                      alert(address);
044                            receivePacket(packet_);
045                            long end = System.currentTimeMillis();
046                            if(counter>0)
047                            total += (end-start);
048                    }
049                    System.out.println("counter: "+counter);
050                    System.out.println("total time: "+total);
051                    System.out.println("average time: "+total/998);
052            }
053    
054            public void receivePacket(DatagramPacket packet) throws Exception {
055                    InetSocketAddress address = (InetSocketAddress)packet.getSocketAddress();
056                    for(int i=0; i<connection_list_.size(); i++)
057                            if(address.equals(connection_list_.get(i).getRemoteAddress())) {
058                                    connection_list_.get(i).received(packet);
059                                    return ;
060                            }
061                    SimpleUDPServerConnection connection = new SimpleUDPServerConnection(packet);
062                    connection_list_.add(connection);
063            }
064            
065            private Connection createConnection(final InetSocketAddress address) {
066                    return new Connection() {
067                            public InetSocketAddress getRemoteAddress() {
068                                    return address;
069                            }
070                            public void send(String message) throws Exception {
071                                    alert(message);
072                            }
073                            public String receive() throws Exception {
074                                    return null;
075                            }
076                    };
077            }
078    
079            public static void main(String[] args) throws Exception {
080                    UDPServerSocket server = new UDPServerSocket(3310, 0, 1024);
081                    server.start();
082    //              alert(connection);
083            }
084    
085            public static void alert(Object message) {
086                    System.out.println(message);
087            }
088    }