Exemple #1
0
 /** Prijme spojeni od klienta */
 public ServerThread accept() throws IOException {
   ServerThread st;
   st = new ServerThread(getServerSocket().accept());
   st.setServer(this);
   st.start();
   return st;
 }
  /** Waits for incoming connections and spins off threads to deal with them. */
  private void listen() {
    Socket client = new Socket();
    ServerThread st;

    /* Should only do this when it hasn't been told to shutdown. */
    while (!shutdown) {
      /* Try to accept an incoming connection. */
      try {
        client = serversock.accept();
        if (debug)
          /* Output info about the client */
          System.out.println(
              "Client on machine "
                  + client.getInetAddress().getHostAddress()
                  + " has connected on port "
                  + client.getLocalPort()
                  + ".");

        /*
         * Create a new thread to deal with the client, add it to the
         * vector of open connections. Finally, start the thread's
         * execution. Start method makes the threads go by calling their
         * run() methods.
         */
        st = new ServerThread(client, this, clientcounter++, debug);
        serverthreads.add(st);
        st.start();
      } catch (IOException e) {
        /*
         * Server Socket is closed, probably because a client told the
         * server to shutdown
         */
      }
    }
  }
 /**
  * Listen for incoming and outgoing requests. If the <code>serverEnabled</code> member is <code>
  * false</code> the server for incoming requests is not started. This starts the internal server
  * thread that processes messages.
  *
  * @throws SocketException when the transport is already listening for incoming/outgoing messages.
  * @throws IOException
  */
 public synchronized void listen() throws java.io.IOException {
   if (server != null) {
     throw new SocketException("Port already listening");
   }
   server = new ServerThread();
   if (connectionTimeout > 0) {
     socketCleaner = new Timer(true); // run as daemon
   }
   server.setDaemon(true);
   server.start();
 }