public class Server extends JFrame
1: // Fig. 24.5: Server.java
2: // Set up a Server that will receive a connection from a client, send
3: // a string to the client, and close the connection.
4: import java.io.EOFException;
5: import java.io.IOException;
6: import java.io.ObjectInputStream;
7: import java.io.ObjectOutputStream;
8: import java.net.ServerSocket;
9: import java.net.Socket;
10: import java.awt.BorderLayout;
11: import java.awt.event.ActionEvent;
12: import java.awt.event.ActionListener;
13: import javax.swing.JFrame;
14: import javax.swing.JScrollPane;
15: import javax.swing.JTextArea;
16: import javax.swing.JTextField;
17: import javax.swing.SwingUtilities;
18:
19: public class Server extends JFrame
20: {
21: private JTextField enterField; // inputs message from user
22: private JTextArea displayArea; // display information to user
23: private ObjectOutputStream output; // output stream to client
24: private ObjectInputStream input; // input stream from client
25: private ServerSocket server; // server socket
26: private Socket connection; // connection to client
27: private int counter = 1; // counter of number of connections
28:
29: // set up GUI
30: public Server()
31: {
32: super( "Server" );
33:
34: enterField = new JTextField(); // create enterField
35: enterField.setEditable( false );
36: enterField.addActionListener(
37: new ActionListener()
38: {
39: // send message to client
40: public void actionPerformed( ActionEvent event )
41: {
42: sendData( event.getActionCommand() );
43: enterField.setText( "" );
44: } // end method actionPerformed
45: } // end anonymous inner class
46: ); // end call to addActionListener
47:
48: add( enterField, BorderLayout.NORTH );
49:
50: displayArea = new JTextArea(); // create displayArea
51: add( new JScrollPane( displayArea ), BorderLayout.CENTER );
52:
53: setSize( 300, 150 ); // set size of window
54: setVisible( true ); // show window
55: } // end Server constructor
56:
57: // set up and run server
58: public void runServer()
59: {
60: try // set up server to receive connections; process connections
61: {
62: server = new ServerSocket( 12345, 100 ); // create ServerSocket
63:
64: while ( true )
65: {
66: try
67: {
68: waitForConnection(); // wait for a connection
69: getStreams(); // get input & output streams
70: processConnection(); // process connection
71: } // end try
72: catch ( EOFException eofException )
73: {
74: displayMessage( "\nServer terminated connection" );
75: } // end catch
76: finally
77: {
78: closeConnection(); // close connection
79: counter++;
80: } // end finally
81: } // end while
82: } // end try
83: catch ( IOException ioException )
84: {
85: ioException.printStackTrace();
86: } // end catch
87: } // end method runServer
88:
89: // wait for connection to arrive, then display connection info
90: private void waitForConnection() throws IOException
91: {
92: displayMessage( "Waiting for connection\n" );
93: connection = server.accept(); // allow server to accept connection
94: displayMessage( "Connection " + counter + " received from: " +
95: connection.getInetAddress().getHostName() );
96: } // end method waitForConnection
97:
98: // get streams to send and receive data
99: private void getStreams() throws IOException
100: {
101: // set up output stream for objects
102: output = new ObjectOutputStream( connection.getOutputStream() );
103: output.flush(); // flush output buffer to send header information
104:
105: // set up input stream for objects
106: input = new ObjectInputStream( connection.getInputStream() );
107:
108: displayMessage( "\nGot I/O streams\n" );
109: } // end method getStreams
110:
111: // process connection with client
112: private void processConnection() throws IOException
113: {
114: String message = "Connection successful";
115: sendData( message ); // send connection successful message
116:
117: // enable enterField so server user can send messages
118: setTextFieldEditable( true );
119:
120: do // process messages sent from client
121: {
122: try // read message and display it
123: {
124: message = ( String ) input.readObject(); // read new message
125: displayMessage( "\n" + message ); // display message
126: } // end try
127: catch ( ClassNotFoundException classNotFoundException )
128: {
129: displayMessage( "\nUnknown object type received" );
130: } // end catch
131:
132: } while ( !message.equals( "CLIENT>>> TERMINATE" ) );
133: } // end method processConnection
134:
135: // close streams and socket
136: private void closeConnection()
137: {
138: displayMessage( "\nTerminating connection\n" );
139: setTextFieldEditable( false ); // disable enterField
140:
141: try
142: {
143: output.close(); // close output stream
144: input.close(); // close input stream
145: connection.close(); // close socket
146: } // end try
147: catch ( IOException ioException )
148: {
149: ioException.printStackTrace();
150: } // end catch
151: } // end method closeConnection
152:
153: // send message to client
154: private void sendData( String message )
155: {
156: try // send object to client
157: {
158: output.writeObject( "SERVER>>> " + message );
159: output.flush(); // flush output to client
160: displayMessage( "\nSERVER>>> " + message );
161: } // end try
162: catch ( IOException ioException )
163: {
164: displayArea.append( "\nError writing object" );
165: } // end catch
166: } // end method sendData
167:
168: // manipulates displayArea in the event-dispatch thread
169: private void displayMessage( final String messageToDisplay )
170: {
171: SwingUtilities.invokeLater(
172: new Runnable()
173: {
174: public void run() // updates displayArea
175: {
176: displayArea.append( messageToDisplay ); // append message
177: } // end method run
178: } // end anonymous inner class
179: ); // end call to SwingUtilities.invokeLater
180: } // end method displayMessage
181:
182: // manipulates enterField in the event-dispatch thread
183: private void setTextFieldEditable( final boolean editable )
184: {
185: SwingUtilities.invokeLater(
186: new Runnable()
187: {
188: public void run() // sets enterField's editability
189: {
190: enterField.setEditable( editable );
191: } // end method run
192: } // end inner class
193: ); // end call to SwingUtilities.invokeLater
194: } // end method setTextFieldEditable
195: } // end class Server
196:
197: /**************************************************************************
198: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
199: * Pearson Education, Inc. All Rights Reserved. *
200: * *
201: * DISCLAIMER: The authors and publisher of this book have used their *
202: * best efforts in preparing the book. These efforts include the *
203: * development, research, and testing of the theories and programs *
204: * to determine their effectiveness. The authors and publisher make *
205: * no warranty of any kind, expressed or implied, with regard to these *
206: * programs or to the documentation contained in these books. The authors *
207: * and publisher shall not be liable in any event for incidental or *
208: * consequential damages in connection with, or arising out of, the *
209: * furnishing, performance, or use of these programs. *
210: *************************************************************************/