/** Creates the server. */
  public static void main(String args[]) {
    client = null;
    ServerSocket server = null;

    try {
      System.out.print("\nCreating Server...\n");
      // creates the server
      server = new ServerSocket(8008);
      System.out.print("Created\n");
      // get the ip Address and the host name.
      InetAddress localAddr = InetAddress.getLocalHost();
      System.out.println("IP address: " + localAddr.getHostAddress());
      System.out.println("Hostname: " + localAddr.getHostName());

    } catch (IOException e) {
      // sends a
      System.out.println("IO" + e);
    }

    // constantly checks for a new aocket trying to attach itself to the trhead
    while (true) {
      try {
        client = server.accept();
        // create a new thread.
        FinalMultiThread thr = new FinalMultiThread(client);
        System.out.print(client.getInetAddress() + " : " + thr.getUserName() + "\n");
        CliList.add(thr);
        current++;
        thr.start();
      } catch (IOException e) {
        System.out.println(e);
      }
    }
  }
示例#2
0
 protected String getHostName() {
   String res = ConfigManager.getPlatformHostname();
   if (res == null) {
     try {
       InetAddress inet = InetAddress.getLocalHost();
       return inet.getHostName();
     } catch (UnknownHostException e) {
       log.warning("Can't get hostname", e);
       return "unknown";
     }
   }
   return res;
 }
示例#3
0
  /**
   * Returns the name of the localhost. If that cannot be found it will return <code>localhost
   * </code>.
   *
   * @return my host
   */
  public static String myHost() {
    String str = null;
    try {
      InetAddress inetA = InetAddress.getLocalHost();
      str = inetA.getHostName();
    } catch (UnknownHostException exc) {
    }

    if (str == null) {
      str = "localhost";
    }
    return str;
  }
示例#4
0
  public LoginBox() {
    super("VnmrJ Login");

    dolayout("", "", "");
    setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    // setVast();
    DisplayOptions.addChangeListener(this);

    try {
      InetAddress inetAddress = InetAddress.getLocalHost();
      m_strHostname = inetAddress.getHostName();
    } catch (Exception e) {
      m_strHostname = "localhost";
    }

    VNMRFrame vnmrFrame = VNMRFrame.getVNMRFrame();
    Dimension size = vnmrFrame.getSize();
    position = vnmrFrame.getLocationOnScreen();
    AppIF appIF = Util.getAppIF();
    int h = appIF.statusBar.getSize().height;
    width = size.width;
    height = size.height - h;

    // Allow resizing and use the previous size and position
    // To stop resizing, use setResizable(false);
    readPersistence();

    //        setSize(width, height);
    //        setLocation(position);
    //        setResizable(false);

    setBackgroundColor(Util.getBgColor());

    m_trayTimer =
        new javax.swing.Timer(
            6000,
            new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                setTrays();
              }
            });
  }
示例#5
0
    @Override
    public void run() {

      try {
        // Create a server socket
        ServerSocket serverSocket = new ServerSocket(8000);
        textArea.append("TCP connection listener started at " + new Date() + '\n');

        while (true) {

          // Listen for a new connection request
          Socket socket = serverSocket.accept();

          // Display the client number
          textArea.append(
              "Starting thread for TCP client " + clientNo + " at " + new Date() + '\n');

          // Find the client's host name, and IP address
          InetAddress inetAddress = socket.getInetAddress();
          textArea.append(
              "Client " + clientNo + "'s host name is " + inetAddress.getHostName() + "\n");
          textArea.append(
              "Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + "\n");

          // Create a new thread for the TCP connection
          HandleTCPClient task = new HandleTCPClient(socket);

          // Start the new thread
          new Thread(task).start();

          // Increment clientNo
          clientNo++;
        }

      } catch (IOException ex) {
        System.err.println(ex);
      }
    }
  public static void main(String[] args) throws IOException, InterruptedException {

    System.out.println("Auction Server starting");
    try { // Get hostname by textual representation of IP address
      InetAddress addr = InetAddress.getLocalHost(); // = InetAddress.getByName("127.0.0.1");
      String hostname = addr.getHostName(); // Get the host name
      System.out.println("ip address = " + addr.toString() + "\nHost name = " + hostname);
    } catch (UnknownHostException e) {
    }

    // used to let host user control flow of program
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    // System.out.println("How many players are needed?");
    int numPlayersNeeded = 2; // Integer.valueOf(br.readLine());

    // make an auction with which clients interact
    Auction myAuction = new Auction(5, numPlayersNeeded); // auction with 5 slots

    // SELECT TYPE OF AUCTION
    Vector<SimultaneousAscendingAuctionServerThread> myThreads =
        new Vector<SimultaneousAscendingAuctionServerThread>();

    ServerSocket serverSocket = null;
    int portNum = 7; // the luckiest port there is!
    // Try reading in the IP and socket number from the text file...
    try {
      BufferedReader in = new BufferedReader(new FileReader("./src/IP_and_Port.txt"));
      // two lines in this file.  First is hostName/IP address, and second is socket number of host
      String hName = in.readLine(); // use the hostname found above and skip this one
      portNum = Integer.valueOf(in.readLine());
      in.close();
    } catch (IOException e) {
    }

    try {
      serverSocket = new ServerSocket(portNum);
    } catch (IOException e) {
      System.err.println("AuctionServer could not listen on port: " + portNum + ".");
      System.exit(-1);
    }

    System.out.println("Clients should join the auction now...");
    boolean started = false;
    boolean firstThread = true; // used to designate first thread as "reporter"
    while (!started) { // (listening && !myAuction.isOver() ){

      SimultaneousAscendingAuctionServerThread aThread =
          new SimultaneousAscendingAuctionServerThread(
              serverSocket.accept(), myAuction, numPlayersNeeded);

      // set first thread as the "reporter", so only one copy of info printed to cmd prompt
      aThread.start();
      if (firstThread) {
        aThread.setReporter(true);
        firstThread = false;
      }

      myThreads.add(aThread);

      if (myThreads.size() >= numPlayersNeeded) {
        started = true;
        System.out.println("Auction should be ready to go...");
      } else
        System.out.println(
            "need more players.  Thread-count = "
                + myThreads.size()
                + "  , idCount = "
                + myAuction.bidderIDs.size());
      // myAuction was passed as a shallow copy... so only one auction exists
    }

    System.out.println("\nAuction has started with " + myThreads.size() + " agents!\n");

    // The auction is running right now... can you feel it?

    // System.out.println("Press Enter to end host connections and close...");
    // br.readLine();

    // System.out.println("Auction is complete... closing connections");
    for (int i = 0; i < myThreads.size(); i++) myThreads.get(i).join();

    for (int i = 0; i < myThreads.size(); i++)
      if (myThreads.get(i).isAlive()) myThreads.get(i).closeConnection();

    serverSocket.close();
  } // end main()