26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 Networking 1001<br />

132 // read message and display it<br />

133 try {<br />

134 message = ( String ) input.readObject();<br />

135 displayArea.append( "\n" + message );<br />

136 displayArea.setCaretPosition(<br />

137 displayArea.getText().length() );<br />

138 }<br />

139<br />

140 // catch problems reading from server<br />

141 catch ( ClassNotFoundException classNotFoundException ) {<br />

142 displayArea.append( "\nUnknown object type received" );<br />

143 }<br />

144<br />

145 } while ( !message.equals( "SERVER>>> TERMINATE" ) );<br />

146<br />

147 } // end method process connection<br />

148<br />

149 // close streams and socket<br />

150 private void closeConnection() throws IOException<br />

151 {<br />

152 displayArea.append( "\nClosing connection" );<br />

153 output.close();<br />

154 input.close();<br />

155 client.close();<br />

156 }<br />

157<br />

158 // send message <strong>to</strong> server<br />

159 private void sendData( String message )<br />

160 {<br />

161 // send object <strong>to</strong> server<br />

162 try {<br />

163 output.writeObject( "CLIENT>>> " + message );<br />

164 output.flush();<br />

165 displayArea.append( "\nCLIENT>>>" + message );<br />

166 }<br />

167<br />

168 // process problems sending object<br />

169 catch ( IOException ioException ) {<br />

170 displayArea.append( "\nError writing object" );<br />

171 }<br />

172 }<br />

173<br />

174 // execute application<br />

175 public static void main( String args[] )<br />

176 {<br />

177 Client application;<br />

178<br />

179 if ( args.length == 0 )<br />

180 application = new Client( "127.0.0.1" );<br />

181 else<br />

182 application = new Client( args[ 0 ] );<br />

183<br />

Fig. Fig. 17.5 17.5 Demonstrating the client portion of a stream-socket connection between a<br />

client and a server (part 4 of 5).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!