import java.net.*;
import java.io.*;

class dgreceive{
    public static void main(String[] args){
	String srcadr = null;
	int rtn = '\n';
	String fname = null;
	if(args.length == 0){
	    fname = "dgreceive.log";
	}else{
	    fname = args[0];
	}
	try{
	    FileOutputStream fos = new FileOutputStream(fname);
	    byte[] buf = new byte[500];
	    try{
		DatagramSocket dgs = new DatagramSocket(58323);
		DatagramPacket dgp = new DatagramPacket(buf, buf.length);
		while(true){
		    // Wait for packet...
		    dgs.receive(dgp);
		    srcadr = dgp.getAddress().toString();
		    // got one, now deal with it
		    fos.write(dgp.getData(), 0, dgp.getLength());
		    fos.write(rtn);
		    String dat = new String(dgp.getData(), 0, dgp.getLength());
		    System.out.println(srcadr + " " + dat);
		}
	    }catch(IOException e){
	    }
	}catch(IOException e){
	    System.out.println("Can't open output file");
	}
    }
}
