📄 server.java
字号:
package Howdy;/* This file is part of Howdy. Howdy is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Howdy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Foobar; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/import java.io.*;import java.net.*;import java.util.*;public class Server // The big cheese.{ int version; int port; Vector connections; // All the clients, made of ServerConnections. ServerConnection client; PrintWriter out; Date date; long startTime; String startDate; FileReader settingsReader; FileWriter settingsWriter; String settingsFile; public Server(int version) { this.version = version; settingsFile = System.getProperty("user.home") + "/.HowdyServerSettings"; port = 6989; readSettings(); connections = new Vector(); new Thread( new ServerInterface(this) ).start(); new Thread( new ServerConnectionListener(this) ).start(); Date date = new Date(); startTime = date.getTime(); startDate = date.toString(); } boolean checkInt(String c) { if( c.startsWith("0") || c.startsWith("1") || c.startsWith("2") || c.startsWith("3") || c.startsWith("4") || c.startsWith("5") || c.startsWith("6") || c.startsWith("7") || c.startsWith("8") || c.startsWith("9") ) return true; else return false; } void distributeChattersList() // Sends the Client a list of who's connected to the server. { ServerConnection currentClient; /*try {*/ for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); //out = new PrintWriter(client.socket.getOutputStream(), true); client.out.println("CHATTERS: BEGIN LIST"); for(int j = 0; j < connections.size(); j++) { currentClient = (ServerConnection)connections.elementAt(j); client.out.println("CHATTERS: BEGIN CHATTER"); client.out.println("CHATTERS: ALIAS: " + currentClient.alias); client.out.println("CHATTERS: END CHATTER"); } client.out.println("CHATTERS: END LIST"); } /*} catch(Exception e) {e.printStackTrace();}*/ } void alertLogout(String alias) { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); client.out.println("MSG: {SERVER}==> " + alias + " has disconnected."); } } void alertKick(String alias) { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); client.out.println("MSG: {SERVER}==> " + alias + " has been kicked out!"); } } void exit() // Stops Howdy. { try { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); out = new PrintWriter(client.socket.getOutputStream(), true); out.println("MSG: {SERVER}==> HOWDY SERVER IS SHUTTING DOWN! Goodbye!"); client.socket.close(); } System.out.println("Howdy is halted."); System.exit(0); } catch(Exception e) {e.printStackTrace();} } void kick(String arg) // Disconnects a user from the server. { try { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); if( arg.equals(client.alias) || client.socket.getInetAddress().toString().equals("/" + arg) ) { out = new PrintWriter(client.socket.getOutputStream(), true); out.println("MSG: {SERVER}==> You've been kicked out!"); client.socket.close(); connections.remove(client); alertKick(arg); distributeChattersList(); } } } catch(SocketException se) {} catch(Exception e) {e.printStackTrace();} } boolean loginPass(String alias, String preAlias) // Makes sure a connection is authorized. { if(alias.equals(preAlias) || preAlias.equals("null") || preAlias.equals("noname") || checkInt(preAlias) == true) return false; for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); if( preAlias./*toLowerCase().*/equals( client.alias/*.toLowerCase()*/ ) ) return false; else return true; } System.out.println("The program has run on to a line of code that should be impossible to reach in Server.aliasPass(). This should not happen!"); return false; // This line should never be executed. } void printWho() // Shows who's connected. { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); System.out.println(getEllapsedTime(client.loginTime) + "\t\t" + client.socket.getInetAddress() + ":" + client.socket.getPort() + "\t\t" + client.alias); } } String getUptime() { return "Up since " + startDate + " for " + getEllapsedTime(startTime) + ". There are currently " + connections.size() + " users connected."; } String getEllapsedTime(long first) { date = new Date(); long ellapsed = date.getTime() - first; long seconds = ellapsed / 1000; long minutes = seconds / 60; long hours = minutes / 60; long smod = seconds % 60; long mmod = minutes % 60; return hours + "h" + mmod + "m" + smod + "s"; } void renamedAlias(String oldAlias, String newAlias, InetAddress host) { try { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); out = new PrintWriter(client.socket.getOutputStream(), true); out.println("MSG: {SERVER}==> " + oldAlias + host + " is now known as " + newAlias + "."); //out.println("MSG: {SERVER}==> " + host + " has joined the chat as " + newAlias + "."); } } catch(Exception e) {e.printStackTrace();} } void sendAdminMessage(String message) // Sends message to all clients from the administrator. { try { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); out = new PrintWriter(client.socket.getOutputStream(), true); out.println("MSG: {{{ADMINISTRATOR}}}==> " + message); } } catch(Exception e) {e.printStackTrace();} } boolean sendMessage(String sender, String message) // Sends message from client to client. { try { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); out = new PrintWriter(client.socket.getOutputStream(), true); out.println("MSG: [" + sender + "]==> " + message); } return true; } catch(Exception e) { e.printStackTrace(); return false; } } void sendWhisper(String sender, String recipient, String message) // Don't let the secret out! { try { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); if( client.alias.equals(recipient) ) { out = new PrintWriter(client.socket.getOutputStream(), true); out.println("MSG: (((" + sender + ")))==> " + message); } else if( client.alias.equals(sender) ) { out = new PrintWriter(client.socket.getOutputStream(), true); out.println("MSG: (Whisper to " + recipient + ")==> " + message); } } } catch(Exception e) {e.printStackTrace();} } void primeTheHitman(String hitman, String target) /* hitman is person who requested info. target is who hitman wants to snoop up on this method pings the target and then lets taht serverconnection know who wanted the info in the first place so that the server knows who to send info to after lag time is calculated*/ { for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); if( client.alias.equals(target) ) { client.waitingForInfoAlias = hitman; date = new Date(); client.lagPingStart = date.getTime(); client.out.println("PING"); } } } void shootTheTarget(String hitman, String target) // send the info to the requesting hitman { ServerConnection targetClient; for(int i = 0; i < connections.size(); i++) { client = (ServerConnection)connections.elementAt(i); if( client.alias.equals(hitman) ) { for(int j = 0; j < connections.size(); j++) { targetClient = (ServerConnection)connections.elementAt(j); if( targetClient.alias.equals(target) ) { client.out.println("INFO: BEGIN"); client.out.println("INFO: ALIAS: " + targetClient.alias); client.out.println("INFO: IP: " + targetClient.socket.getInetAddress() ); client.out.println("INFO: CONNECTED: " + getEllapsedTime(targetClient.loginTime) ); date = new Date(); long lag = date.getTime() - targetClient.lagPingStart; client.out.println("INFO: LAG: " + lag); client.out.println("INFO: END"); } } } } } void readSettings() { try { settingsReader = new FileReader(settingsFile); BufferedReader fileReader = new BufferedReader(settingsReader); String line; while( ( line = fileReader.readLine() ) != null) { if( line.startsWith("port=") ) { String stringPort = line.substring(5); try { port = Integer.parseInt(stringPort); } catch(NumberFormatException nfe) { System.out.println("The settings file does not have valid integer for a port. " + "Please edit the file appropriately or remove it and let me regenerate it. " + "For now, I will bind to the default port, 6989."); } } } settingsReader.close(); } catch(IOException ioe) { System.out.println("Could not open settings file to load settings!"); } } void writeSettings() { try { settingsWriter = new FileWriter(settingsFile); settingsWriter.write("port=" + port + "\n"); settingsWriter.close(); } catch(IOException ioe) { System.out.println("I couldn't open the settings file to write it!"); } } void changePort(String sport) { try { port = Integer.parseInt(sport); writeSettings(); System.out.println("Settings have been saved. You must restart Howdy Server for changes to take effect."); } catch(NumberFormatException nfe) { System.out.println("Invalid port."); } }}// Copyright (C) 2003-2004 Serban Giuroiu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -