@Override
 public void run() {
   Log.i(TAG, "Server " + addr.getHostName() + " started");
   while (true) {
     try {
       Socket socket = serverSocket.accept();
       int flag = socket.getInputStream().read();
       if (flag == FLAG_REQUEST) {
         DataInputStream in = new DataInputStream(socket.getInputStream());
         String text = in.readUTF();
         Log.i(TAG, text);
         playText(text, service);
         in.close();
       } else if (flag == FLAG_STOP_ALL) {
         Log.i(
             TAG, "Detected \"stop all\" signal from " + socket.getInetAddress().getHostName());
         for (ServerThread st : serverThreads) {
           st.stop();
         }
         break;
       }
     } catch (IOException ex) {
       Log.e(TAG, "", ex);
     }
   }
   Log.i(TAG, "Server " + addr.getHostName() + " finished");
   if (!destroyed) {
     stop();
   }
 }
 /** Closes all open sockets and stops the internal server thread that processes messages. */
 public void close() {
   ServerThread st = server;
   if (st != null) {
     st.close();
     try {
       st.join();
     } catch (InterruptedException ex) {
       logger.warn(ex);
     }
     server = null;
     for (Iterator it = sockets.values().iterator(); it.hasNext(); ) {
       SocketEntry entry = (SocketEntry) it.next();
       try {
         synchronized (entry) {
           entry.getSocket().close();
         }
         logger.debug("Socket to " + entry.getPeerAddress() + " closed");
       } catch (IOException iox) {
         // ingore
         logger.debug(iox);
       }
     }
     if (socketCleaner != null) {
       socketCleaner.cancel();
     }
     socketCleaner = null;
   }
 }
 public void close() {
   stop = true;
   ServerThread st = server;
   if (st != null) {
     st.interrupt();
   }
 }
示例#4
0
文件: Server.java 项目: Pitel/dZama
 /** Prijme spojeni od klienta */
 public ServerThread accept() throws IOException {
   ServerThread st;
   st = new ServerThread(getServerSocket().accept());
   st.setServer(this);
   st.start();
   return st;
 }
示例#5
0
  /** 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
         */
      }
    }
  }
 /**
  * Returns the name of the listen thread.
  *
  * @return the thread name if in listening mode, otherwise <code>null</code>.
  * @since 1.6
  */
 public String getThreadName() {
   ServerThread st = server;
   if (st != null) {
     return st.getName();
   } else {
     return null;
   }
 }
 /**
  * Returns the priority of the internal listen thread.
  *
  * @return a value between {@link Thread#MIN_PRIORITY} and {@link Thread#MAX_PRIORITY}.
  * @since 1.2.2
  */
 public int getPriority() {
   ServerThread st = server;
   if (st != null) {
     return st.getPriority();
   } else {
     return Thread.NORM_PRIORITY;
   }
 }
示例#8
0
文件: Server.java 项目: Pitel/dZama
 /**
  * Nastavi server game protihraci
  *
  * @param login Login protihrace
  * @param serverGame Server game
  */
 public synchronized boolean serverGame(String login, ServerGame serverGame) {
   ServerThread st = getClients().get(login.toLowerCase());
   if (st == null) {
     return false;
   } else {
     st.setServerGame(serverGame);
     return true;
   }
 }
示例#9
0
文件: Server.java 项目: Pitel/dZama
 /**
  * Odesle konec hry
  *
  * @param login Login protihrace
  */
 public synchronized boolean gameEnd(String login) {
   ServerThread st = getClients().get(login.toLowerCase());
   if (st == null) {
     return false;
   } else {
     st.gameEnd();
     return true;
   }
 }
示例#10
0
文件: Server.java 项目: Pitel/dZama
 /**
  * Provede tah figurkou
  *
  * @param x Zdrojova souradnice X
  * @param y Zdrojova souradnice Y
  * @param toX Cilova souradnice X
  * @param toY Cilova souradnice Y
  * @return True pokud se tah povedl
  */
 public synchronized boolean turn(String login, int x, int y, int toX, int toY) {
   ServerThread st = getClients().get(login.toLowerCase());
   if (st == null) {
     return false;
   } else {
     st.turn(x, y, toX, toY);
     return true;
   }
 }
示例#11
0
文件: Server.java 项目: Pitel/dZama
 /**
  * Odesle informaci ostatnim hracum, ze je tento hrac uz neni ve hre
  *
  * @param login Login hrace
  */
 public synchronized void userOutGame(String login) {
   Enumeration<ServerThread> e = getClients().elements();
   while (e.hasMoreElements()) {
     ServerThread serverThread = e.nextElement();
     if (!serverThread.getLogin().toLowerCase().equals(login.toLowerCase())) {
       serverThread.userOutGame(login);
     }
   }
 }
示例#12
0
文件: Server.java 项目: Pitel/dZama
 /**
  * Provede odhlaseni uzivatele Odesle informaci o odhlaseni ostatnim uzivatelum
  *
  * @param serverThread Vlakno serveru
  */
 public synchronized void logout(ServerThread serverThread) {
   if (serverThread.getLogin() != null) {
     clients.remove(serverThread.getLogin().toLowerCase());
     Enumeration<ServerThread> e = getClients().elements();
     while (e.hasMoreElements()) {
       e.nextElement().userLogout(serverThread.getLogin());
     }
   }
 }
示例#13
0
文件: Server.java 项目: Pitel/dZama
  /**
   * Provede prihlaseni uzivatele Odesle mu seznam aktualnich uzivatelu a r ozesle informaci o
   * prihlaseni ostatnim uzivatelum
   *
   * @param serverThread Vlakno serveru
   */
  public synchronized boolean login(ServerThread serverThread) {
    if (clients.containsKey(serverThread.getLogin().toLowerCase())) {
      return false;
    }

    // Send login info to other clients
    Enumeration<ServerThread> e = getClients().elements();
    while (e.hasMoreElements()) {
      e.nextElement().userLogin(serverThread.getLogin());
    }

    // Send users list to client
    Enumeration<ServerThread> e2 = getClients().elements();
    while (e2.hasMoreElements()) {
      ServerThread serverThread2 = e2.nextElement();
      serverThread.userLogin(serverThread2.getLogin());
      if (serverThread2.getServerGame() != null) {
        serverThread.userInGame(serverThread2.getLogin());
      }
    }

    // Append client to list
    clients.put(serverThread.getLogin().toLowerCase(), serverThread);

    return true;
  }
 /**
  * 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();
 }
示例#15
0
  /**
   * Called by a thread who's client has asked to exit. Gets rid of the thread.
   *
   * @param st The ServerThread to remove from the vector of active connections.
   */
  public void kill(ServerThread st) {
    if (debug) System.out.println("Killing Client " + st.getID() + ".");

    /* Find the thread in the vector and remove it. */
    for (int i = 0; i < serverthreads.size(); i++) {
      if (serverthreads.elementAt(i) == st) serverthreads.remove(i);
    }
  }
示例#16
0
文件: Server.java 项目: Delivior/Lera
  public Server() {
    try {
      serverSocket = new ServerSocket(1210);
      // window.setText("Сервер запущен. Адрес: " + serverSocket.getInetAddress().toString());
      System.out.println("Сервер запущен. Адрес: " + serverSocket.getInetAddress().toString());
      while (true) {
        // System.out.println("eee");
        klientSocket = serverSocket.accept();
        // window.setText("Подключён клиент. Адрес: " + klientSocket.getInetAddress().toString());
        System.out.println("Подключён клиент. Адрес: " + klientSocket.getInetAddress().toString());
        ServerThread server = new ServerThread(klientSocket);

        server.run(); // запуск потока
        // window.setText(server.getPlane().toString());
      }
    } catch (IOException e) {
      System.err.println(e);
    }
  }
示例#17
0
文件: Server.java 项目: w4-pwr/studia
 public synchronized void sendList() throws IOException {
   ClientList l = new ClientList();
   for (ServerThread st : sList) {
     if (st.life() && st != null) l.add(st.getName());
   }
   for (ServerThread st : sList) {
     if (st.life() && st != null) st.getOut().writeObject(l);
   }
 }
 /**
  * Changes the priority of the server thread for this TCP transport mapping. This method has no
  * effect, if called before {@link #listen()} has been called for this transport mapping.
  *
  * @param newPriority the new priority.
  * @see Thread#setPriority
  * @since 1.2.2
  */
 public void setPriority(int newPriority) {
   ServerThread st = server;
   if (st != null) {
     st.setPriority(newPriority);
   }
 }
 /**
  * Sets the name of the listen thread for this UDP transport mapping. This method has no effect,
  * if called before {@link #listen()} has been called for this transport mapping.
  *
  * @param name the new thread name.
  * @since 1.6
  */
 public void setThreadName(String name) {
   ServerThread st = server;
   if (st != null) {
     st.setName(name);
   }
 }
 /**
  * Sends a SNMP message to the supplied address.
  *
  * @param address an <code>TcpAddress</code>. A <code>ClassCastException</code> is thrown if
  *     <code>address</code> is not a <code>TcpAddress</code> instance.
  * @param message byte[] the message to sent.
  * @throws IOException
  */
 public void sendMessage(Address address, byte[] message) throws java.io.IOException {
   if (server == null) {
     listen();
   }
   server.sendMessage(address, message);
 }