import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleChatv2 extends JFrame { private ServerSocket connectionSocket = null; private JTextField displayLabel; private final Color colorValues[] = { Color.black, Color.blue, Color.red, Color.green }; // set up GUI public SimpleChatv2() { super( "Using JMenus" ); // set up File menu and its menu items JMenu fileMenu = new JMenu( "File" ); fileMenu.setMnemonic( 'F' ); // set up Activate Server menu item JMenuItem serverItem = new JMenuItem( "Activate Server"); serverItem.setMnemonic( 'S' ); fileMenu.add( serverItem ); serverItem.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { setUpServer(); } } // end anonymous inner class ); // end call to addActionListener // set up Activate Client menu item JMenuItem clientItem = new JMenuItem( "Activate Client"); clientItem.setMnemonic( 'C' ); fileMenu.add( clientItem ); clientItem.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { setUpClient(); } } // end anonymous inner class ); // end call to addActionListener // set up About... menu item JMenuItem aboutItem = new JMenuItem( "About..." ); aboutItem.setMnemonic( 'A' ); fileMenu.add( aboutItem ); aboutItem.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( SimpleChatv2.this, "This is an example\nof using menus", "About", JOptionPane.PLAIN_MESSAGE ); } } // end anonymous inner class ); // end call to addActionListener // set up Exit menu item JMenuItem exitItem = new JMenuItem( "Exit" ); exitItem.setMnemonic( 'x' ); fileMenu.add( exitItem ); exitItem.addActionListener( new ActionListener() { // anonymous inner class // terminate application when user clicks exitItem public void actionPerformed( ActionEvent event ) { System.exit( 0 ); } } // end anonymous inner class ); // end call to addActionListener // create menu bar and attach it to MenuTest window JMenuBar bar = new JMenuBar(); setJMenuBar( bar ); bar.add( fileMenu ); // set up label to display text displayLabel = new JTextField( "Sample Text", SwingConstants.CENTER ); displayLabel.setForeground( colorValues[ 0 ] ); displayLabel.setFont( new Font( "Serif", Font.PLAIN, 72 ) ); getContentPane().setBackground( Color.CYAN ); getContentPane().add( displayLabel, BorderLayout.CENTER ); setSize( 500, 200 ); setVisible( true ); } // end constructor public static void main( String args[] ) { SimpleChatv2 application = new SimpleChatv2(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public void setUpServer() //throws IOException { connectionSocket = null; try { connectionSocket = new ServerSocket(10007); } catch (IOException e) { System.err.println("Could not listen on port: 10007."); System.exit(1); } JOptionPane.showMessageDialog( SimpleChatv2.this, "Server Socket is now activated", "Server", JOptionPane.PLAIN_MESSAGE ); ServerThread st = new ServerThread (connectionSocket, displayLabel); st.start(); } public void setUpClient() { ClientThread st = new ClientThread (); st.start(); } } class ServerThread extends Thread { private ServerSocket connectionSocket; private JTextField display; public ServerThread (ServerSocket servSock, JTextField disp) { connectionSocket = servSock; display = disp; } public void run() { Socket communicationSocket = null; try { communicationSocket = connectionSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } try { PrintWriter out = new PrintWriter(communicationSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( communicationSocket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println ("Server: " + inputLine); out.println(inputLine); display.setText (inputLine); if (inputLine.equals("Bye.")) break; } out.close(); in.close(); communicationSocket.close(); connectionSocket.close(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } } } class ClientThread extends Thread { public void run () { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { // echoSocket = new Socket("taranis", 7); echoSocket = new Socket("127.0.0.1", 10007); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: taranis."); System.exit(1); } try { BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: taranis."); System.exit(1); } } }