Пример #1
1
 public synchronized void logout(JSONObject req) {
   Long longseq = new Long((Long) req.get("sequence"));
   Integer seq = longseq.intValue();
   JSONObject res = new JSONObject();
   String name = (String) req.get("params");
   System.out.println("Logout von Client mit Name: " + name);
   Socket t = this.keyList.get(name);
   ServerGUI.getInstance().setClientInactive(name);
   this.keyList.remove(name);
   System.out.println("In KeyList: " + keyList.toString());
   this.names.remove(name);
   this.listWithoutName.remove(t);
   ServerGUI.getInstance().id--;
   res.put("sequence", seq);
   res.put("statuscode", ServerGUI.STATUS_OK);
   res.put("response", "memberLeft");
   res.put("author", "Server");
   res.put("msg", name + " hat den Chat verlassen.");
   for (Socket s : listWithoutName) {
     try {
       PrintWriter p = new PrintWriter(s.getOutputStream(), true);
       p.println(res);
     } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
 }
Пример #2
0
  /**
   * Methode, die die Verbindung vom Spamsockets vom Server trennt.
   *
   * @param s
   */
  public synchronized void removeSpamConnection(Socket s) {
    this.listWithoutName.remove(s);
    if (ServerGUI.getInstance().id > 0) {
      ServerGUI.getInstance().id--;
    }

    System.out.println("ID ist nun nach Remove: " + ServerGUI.getInstance().id);
    JSONObject res = new JSONObject();
    res.put("statuscode", ServerGUI.STATUS_UNAUTHORIZED);
    res.put("response", "Hau ab, du Spammer!");
    try {
      PrintWriter p = new PrintWriter(s.getOutputStream(), true);
      p.println(res);
      s.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Пример #3
0
  public void run() {
    this.setName("ServerThread Nummer:" + ServerThread.threadNo);
    System.out.println(this.getName() + " ist gestartet...");
    System.out.println("Thread Status: " + this.getState());
    ServerThread.threadNo++;
    System.out.println("Beginn Run Methode: ");
    try {

      System.out.println("Serversocket wird angelegt...");
      synchronized (ServerThread.lock) {
        server = new ServerSocket(ServerGUI.getInstance().getPortNumber());
        System.out.println("Serversocket läuft über Port: " + server.getLocalPort());
        server.setReuseAddress(true);
      }

    } catch (IOException ex) {
      ex.printStackTrace();
    }
    while (true) {

      try {

        System.out.println("Nun bin ich vor Server accept...");
        try {

          this.client = server.accept();
          ServerGUI.getInstance().id++;

        } catch (SocketException ex) {
          client.close();
          return;
        }
        System.out.println("Nun bin ich nach dem  Server accept...");
        System.out.println(this.getState());
        synchronized (ServerThread.lock) {
          new ClientHandler(client).start();
          System.out.println(
              "Neuer Client von "
                  + server.getInetAddress()
                  + " auf Client's Port  "
                  + client.getPort());
          if (ServerGUI.getInstance().id <= 5) {
            listWithoutName.add(this.client);
            System.out.println(
                "Client no."
                    + ServerGUI.getInstance().id
                    + "on Port"
                    + client.getPort()
                    + "added to the Clientpool of MailBox Server.");

          } else {
            // TODO Client mit Benachrichtung abweisen.
            Scanner in = new Scanner(this.client.getInputStream());
            out = new PrintWriter(this.client.getOutputStream(), true);
            System.out.println("Lehne Anfrage ab...");
            String s = in.nextLine();
            System.out.println("Input Stream(Request von Client): " + s.toString());
            JSONParser parser = new JSONParser();
            Object obj;
            try {
              obj = parser.parse(s.toString());
              JSONObject req = (JSONObject) obj;
              Long longseq = new Long((Long) req.get("sequence"));
              Integer seq = longseq.intValue();
              JSONObject res = new JSONObject();
              res.put("statuscode", ServerGUI.STATUS_SERVICE_UNAVAILABLE);
              res.put("sequence", seq);
              res.put("response", "server_full");
              out.println(res);
              client.close();

            } catch (ParseException e) {
              // TODO Auto-generated catch block
              client.close();

              ServerGUI.getInstance().id--;
              e.printStackTrace();
            }
          }
        } // hier muss sync
      } catch (IOException e) {

        e.printStackTrace();
      }
    }
  }