import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.Date; public class Server implements ActionListener { String WINDOW_NAME = "Server Chat Window"; int PORT= 2380; int MAX_USERS = 2; //Includes Server String WELCOME_MSG = "Welcome to the chat!"; File LOG_PATH = new File("logs\\log.doc"); Socket clientSocket = null; Socket clientSocket2 = null; ServerSocket serverSocket = null; String errorMsg = null; JFrame mainFrame; JPanel labelPanel; JPanel historyPanel; JPanel textPanel; JPanel actionPanel; JLabel status; JTextArea history; JTextField input; JButton send, exit; String Received[] = {"Welcome to " + WINDOW_NAME + ". (Type \"\\help\" of \"\\h\" for help.)\n","\n","\n","\n","\n","\n","\n","\n","\n","\n"}; String in; String messages = "\n"; String userList = "No users online(error)"; boolean userSend = false; String addresses[] = new String[MAX_USERS]; int addrCnt = 0; boolean newAddr = true; String kicked[] = new String[100]; int kickCnt = 0; boolean kickedUsr = false; boolean ban = false; int count=0; boolean error = false, mError = false; boolean action = false; boolean fileIn = false; boolean fileOut = false; FileOutputStream logFile; byte[] c = ("Server Start Up!").getBytes(); public Server() throws IOException { start(); mainFrame = new JFrame(WINDOW_NAME); Container content = mainFrame.getContentPane(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); labelPanel = new JPanel(); historyPanel = new JPanel(); textPanel = new JPanel(); actionPanel = new JPanel(); mainFrame.setSize(600, 100); mainFrame.setLocation(200, 200); history = new JTextArea(9,50); inPanels(); addPanels(); content.add(labelPanel); content.add(historyPanel); content.add(textPanel); content.add(actionPanel); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.pack(); mainFrame.setVisible(true); if(error) dispMsg(); input.grabFocus(); msgIn(); } public void inPanels() { status = new JLabel(errorMsg); labelPanel.add(status); messages = ""; for(int i=9; i>=0; i--) messages = messages + Received[i]; history.setText(messages); history.setEditable(false); historyPanel.add(history); } public void addPanels() { input = new JTextField(in,50); input.addActionListener(this); textPanel.add(input); send = new JButton("Send"); exit = new JButton("Exit"); send.addActionListener(this); exit.addActionListener(this); actionPanel.add(send); actionPanel.add(exit); } public void start() { try { serverSocket = new ServerSocket(PORT); } catch (IOException e) { errorMsg = "Could not listen on port: " + PORT; error = true; } if(error == false) errorMsg = "Listening on: " + PORT; } public void dispMsg() { byte[] c=Received[0].getBytes(); int m; for(m=0; m<2;) { if(Received[0].length() > 100) { for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[1] = Received[0].substring(0,100) + "\n"; Received[0] = Received[0].substring(100); if(Received[0].length() > 100) { for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[1] = Received[0].substring(0,100) + "\n"; Received[0] = Received[0].substring(100); } } try { logFile = new FileOutputStream(LOG_PATH,true); logFile.write(c); logFile.close(); m=3; } catch (FileNotFoundException e) { File tempFile = new File(LOG_PATH.getPath()); String path = tempFile.toString(); boolean newDir = false; for(int i=0; i0; i--) Received[i] = Received[i-1]; Received[0] = "Can't Write to Log File!\n"; m=3; } } if(m==2) { for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[0] = "Log File Not Found!\n"; } Container content = mainFrame.getContentPane(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); labelPanel.removeAll(); historyPanel.removeAll(); inPanels(); if(action) { textPanel.removeAll(); actionPanel.removeAll(); addPanels(); } action = false; content.add(labelPanel); content.add(historyPanel); content.add(textPanel); content.add(actionPanel); labelPanel.repaint(); historyPanel.repaint(); textPanel.repaint(); actionPanel.repaint(); mainFrame.setVisible(true); input.grabFocus(); } public void msgIn() throws IOException { for(int j=0; j<1; ) { String mErrorMsg = ""; boolean maxusers = false; newAddr = true; boolean nameMatch = false; try { clientSocket = serverSocket.accept(); InetAddress address = clientSocket.getInetAddress(); String address2 = ""; for(int k=1; k MAX_USERS && newAddr) { PrintWriter out = null; try { mError = false; clientSocket2 = new Socket(address2,PORT+1); out = new PrintWriter(clientSocket2.getOutputStream(), true); out.println("Too many users"); clientSocket2.close(); errorMsg = "Listening on: " + PORT; } catch (UnknownHostException e) { mErrorMsg=errorMsg = "Couldn't Send Chat Full Message!"; mError = true; } catch (IOException e) { mErrorMsg=errorMsg = "Couldn't Send Chat Full Message!"; mError = true; } maxusers = true; } else if(newAddr) { addresses[addrCnt] = address2; if(!address2.equals("127.0.0.1")) { PrintWriter out = null; try { mError = false; clientSocket2 = new Socket(address2,PORT+1); out = new PrintWriter(clientSocket2.getOutputStream(), true); out.println(WELCOME_MSG); clientSocket2.close(); errorMsg = "Listening on: " + PORT; clientSocket2.close(); } catch (UnknownHostException e) { mErrorMsg=errorMsg = "Couldn't Send Welcome Message!"; mError = true; } catch (IOException e) { mErrorMsg=errorMsg = "Couldn't Send Welcome Message!"; mError = true; } } addrCnt++; } if(!maxusers && !kickedUsr) { if(count >= 9) count=0; BufferedReader incoming = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[0] = incoming.readLine(); int addrLgth = address2.length(); if(Received[0].equals("User Signed off...")) { Received[0] = "Signed off..."; for(int k=0; k0; i--) Received[i] = Received[i-1]; Received[0] = "All kicked users have been allowed back\n"; } else { send(); sending=true; } } catch (StringIndexOutOfBoundsException e) { send(); sending=true; } in=""; action = true; if(!sending) dispMsg(); } public void send() { if(!in.equals("")) { try { clientSocket2 = new Socket("127.0.0.1",PORT); PrintWriter out = new PrintWriter(clientSocket2.getOutputStream(), true); out.println(in); clientSocket2.close(); } catch (UnknownHostException err) { errorMsg = "Error Sending Message!"; } catch (IOException err) { errorMsg = "Error Sending Message!"; } } } public void help() { Received[9] = "Help for " + WINDOW_NAME + ".\n"; Received[8] = "\\welcome - Changes the message sent when users sign on.\n"; Received[7] = "\\port - Changes the port.\n"; Received[6] = "\\max - Changes the number of users who can connect.\n"; Received[5] = "\\kick -h - brings up the kick user help\n"; Received[4] = "\\ip - Finds an IP address.\n"; Received[3] = "\\users - lists all users.\t\t\t\\exit - exits the program.\n"; Received[2] = "\\file - Sends a file to a user.\t\t\\about - Program Info\n"; Received[1] = "\\w - Whispers a message to a user.\n"; Received[0] = "\\log - Changes the location and name of log file.\n"; } public void kickHelp() { for(int i=9; i>6; i--) Received[i] = Received[i-7]; Received[6] = "Help for kicking users.\n"; Received[5] = "\\kick -a - Kicks all users\n"; Received[4] = "\\kick -b - kicks and bans a user\n"; Received[3] = "\\kick - kicks the user (doesn't ban)\n"; Received[2] = "\\unkick - allows kicked user to sign back on(IP only).\n"; Received[1] = "\\unkick -a - clears the kicked list.\n"; Received[0] = "\\kicked - lists all kicked users.\n"; } public void welcome() { WELCOME_MSG = in.substring(9); for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[0] = "Welcome Message changed to \"" + WELCOME_MSG + "\"\n"; } public void port() { try { serverSocket.close(); } catch (IOException e) { } addrCnt = 0; PORT = Integer.parseInt(in.substring(6)); start(); for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[0] = "Port changed to " + PORT + "\n"; } public void max() { String inT= in; in = "Max users changed to " + Integer.parseInt(inT.substring(5)) + ". Kicking off users.\n"; send(); MAX_USERS = Integer.parseInt(inT.substring(5)); addrCnt = 0; } public void kick() { PrintWriter out = null; int addrBrk = 0; int line=6; if(ban) line = 9; try { Socket clientSocket3 = new Socket(in.substring(line),PORT+1); InetAddress address = clientSocket3.getInetAddress(); out = new PrintWriter(clientSocket3.getOutputStream(), true); out.println("You have been kicked off!"); clientSocket3.close(); for(int i=0; i0; i--) Received[i] = Received[i-1]; for(int k=0; k0; i--) Received[i] = Received[i-1]; clientSocket2 = new Socket(in.substring(4),PORT+1); InetAddress address = clientSocket.getInetAddress(); Received[0] = address.toString() + "\n"; clientSocket2.close(); } catch (UnknownHostException e) { Received[0] = "Couldn't Resolve IP\n"; } catch (IOException e) { Received[0] = "Couldn't Resolve IP\n"; } } public void kicked() { for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[0] = ""; if(kickCnt == 0) Received[0] = "No users kicked"; else for(int i=0; i0; i--) Received[i] = Received[i-1]; userList = ""; if(addrCnt == 0) userList = "No users online"; else for(int i=0; i0; i--) Received[i] = Received[i-1]; Received[0] = "Sending file to " + in.substring(6) + ", Please wait\n"; FileTrans filetrans = new FileTrans((in.substring(6)),(PORT+2),false); } public void fileIN() { for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[0] = "Receiving file, Please wait\n"; FileTrans filetrans = new FileTrans(PORT+2); fileIn = false; } public void log() { LOG_PATH = new File(in.substring(5)); for(int i=9; i>0; i--) Received[i] = Received[i-1]; Received[0] = "Log path changed to " + LOG_PATH + "\n"; } public void whisper() { int line = in.substring(3).indexOf(' '); String ipAddr = in.substring(3,3+line); for(int i=9; i>0; i--) Received[i] = Received[i-1]; try { Socket whisp = new Socket(ipAddr, PORT+1); PrintWriter out = new PrintWriter(whisp.getOutputStream(), true); out.println("Admin whispers: " + in.substring(3+line)); whisp.close(); Received[0] = "Whispering to " + ipAddr + ": " + in.substring(3+line) + "\n"; } catch (UnknownHostException e) { Received[0] = "Couldn't Whisper to " + ipAddr + "!\n"; } catch (IOException e) { Received[0] = "Couldn't Whisper to " + ipAddr + "!\n"; } } public void about() { JOptionPane.showMessageDialog(null,"About " + WINDOW_NAME + ":\nVersion 1.0b\nWritten by Benjamin Zastovnik\nMarch 9, 2004", "About " + WINDOW_NAME, JOptionPane.PLAIN_MESSAGE); } public void actionPerformed(ActionEvent e) { if(e.getSource() == exit) { System.exit(0); } if(e.getSource() == send) { in = input.getText(); if(in.equals("")); else msgOut(); } if(e.getSource() == input) { in = input.getText(); if(in.equals("")); else msgOut(); } } public static void main(String args[]) throws IOException { Server window = new Server(); } }