/*FortuneServer1.java*/
import java.io.*;
import java.net.*;
class FortuneServer1 extends Thread
{
  DatagramSocket ServerSocket;
  public FortuneServer1()
  {
    super("FortuneServer1");
    try
    {
      ServerSocket=new DatagramSocket(1114);
      System.out.println("FortuneServer up and running...");
    }
    catch(SocketException e)
    {
      System.err.println("Exception:couldn't create datagram sockter");
      System.exit(1);
    }
  }

  public static void main(String args[])
  {
    FortuneServer1 server=new FortuneServer1();
    server.start();
  }
  public void run()
  {
    if(ServerSocket==null)
      return;
    while(true)
    {
      try
      {
        InetAddress address;
        int port;
        DatagramPacket packet;
        byte[] data=new byte[256];
        int num=1;
        packet=new DatagramPacket(data,data.length);
        ServerSocket.receive(packet);
        address=packet.getAddress();
        port=packet.getPort();
        File inFile=new File("Fortunes.txt");
        FileInputStream inStream=new FileInputStream(inFile);
        if(inStream.read(data)<=0)
        {
          System.err.println("Error:couldn't read fortunes");
        }
        packet=new DatagramPacket(data,data.length,address,port);
        ServerSocket.send(packet);
      }
      catch(Exception e)
      {
        System.err.println("Exception:"+e);
        e.printStackTrace();}
      }
   }
}
