import java.net.DatagramPacket;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class evo_udp{
    Thread thread;
    DatagramSocket socket;
    
    public static void main(String[] args) {
        evo_udp u = new evo_udp();
    }
   

    public evo_udp(){
        new StartThread();
    }
    public class StartThread implements Runnable{
        StartThread(){
            thread = new Thread(this);
            thread.start();
        }
        public void StopThread() {
            thread.interrupted();
            socket.close();
        }
        
        public void run(){
            try{
                // receive buffer
                byte[] buffer = new byte[20];
                // send buffer
                byte[] sbuffer = new byte[20];
                // listen to this port
                int port = 1234;
                try{
                    socket = new DatagramSocket(port);
                    
                    while(true){
                        try{
                            for (int i=0; i<11; i++ ) { 
                                buffer[i]=0; 
                            }
                            DatagramPacket packet = new DatagramPacket(buffer, buffer.length );
                            socket.receive(packet);
                            InetAddress client = packet.getAddress();
                            byte[] iByte = null;
                            iByte=client.getAddress();
                            int client_port = packet.getPort();
                            
                            // uncoment next few lines to see what is received and from who
                            //String r = String.format("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", buffer[0], buffer[1],buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11]);
                            //String from = new String();
                            
                            String test = new String(buffer);
                            
                            //from = "From "+client +" " +client_port;
                            test = test.substring(0,7);
                            
                            //System.out.println(from);
                            
                            // display to screen data received in hex
                            //System.out.println(r);
                            //System.out.println(rd);
                            
                            // check for END sent from client to exit application
                            if (test.equals("END UDP")) {
                                System.out.println("UDP Listener Stopped");
                                StopThread();
                            }
                            
                            //set up send back what was received except first 4 bytes
                            int temp = 0;
                            temp=0x000000FF & ((int)buffer[4]);
                            sbuffer[0] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[5]);
                            sbuffer[1] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[6]);
                            sbuffer[2] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[7]);
                            sbuffer[3] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[8]);
                            sbuffer[4] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[9]);
                            sbuffer[5] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[10]);
                            sbuffer[6] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[11]);
                            sbuffer[7] = (byte)temp;
                            
                            //sent data has from ip
                            sbuffer[8] = iByte[0];
                            sbuffer[9] = iByte[1];
                            sbuffer[10] = iByte[2];
                            sbuffer[11] = iByte[3];
                                                        
                            //sent data has from port here
                            sbuffer[12]=(byte)((client_port & 0x0000ff00)>>>8);  //buf[2] = 51;
                            sbuffer[13]=(byte)((client_port & 0x000000ff));      //buf[3] = -97;  bytes are signed.

                            //pad some junk
                            temp=0x000000FF & ((int)0);
                            sbuffer[14] = (byte)temp;
                            temp=0x000000FF & ((int)66);
                            sbuffer[15] = (byte)temp;
                            
                            //# of users all with 255 will send ? to users for number of users on server
                            temp=0x000000FF & ((int)255);
                            sbuffer[16] = (byte)temp;
                            temp=0x000000FF & ((int)255);
                            sbuffer[17] = (byte)temp;
                            temp=0x000000FF & ((int)255);
                            sbuffer[18] = (byte)temp;
                            temp=0x000000FF & ((int)255);
                            sbuffer[19] = (byte)temp;
                            
                            DatagramPacket sndPkg = new DatagramPacket(sbuffer, 20, client, client_port);
                            // send the data back
                            socket.send(sndPkg);
                            // uncomment next few lines to see what is sent
                            //String to = new String();
                            //to = "To "+client+" "+client_port;
                            //String s = String.format("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", sbuffer[0], sbuffer[1],sbuffer[2], sbuffer[3], sbuffer[4], sbuffer[5], sbuffer[6], sbuffer[7], sbuffer[8], sbuffer[9], sbuffer[10], sbuffer[11], sbuffer[12], sbuffer[13], sbuffer[14], sbuffer[15], sbuffer[16], sbuffer[17], sbuffer[18], sbuffer[19]);
                            //System.out.println(to);
                            //System.out.println(s);
                        } 
                        catch(UnknownHostException ue){}
                    }
                } 
                catch(java.net.BindException b){}
            } 
            catch (IOException e){
                System.err.println(e);
            }
        }
    }
}

