Created
October 23, 2013 14:13
-
-
Save fado/7119569 to your computer and use it in GitHub Desktop.
EchoClient to accompany EchoServer...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.ac.qub.networking; | |
import java.io.EOFException; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.net.InetAddress; | |
import java.net.Socket; | |
import javax.swing.JFrame; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
import javax.swing.JScrollPane; | |
import javax.swing.SwingUtilities; | |
import java.awt.BorderLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
public class EchoClient extends JFrame { | |
private JTextField enterField; | |
private JTextArea displayArea; | |
private ObjectOutputStream output; | |
private ObjectInputStream input; | |
private String message = ""; | |
private String chatServer; | |
private Socket client; | |
public EchoClient ( String host) { | |
super( "Client" ); | |
chatServer = host; | |
enterField = new JTextField(); | |
enterField.setEditable( false ); | |
enterField.addActionListener( | |
new ActionListener() | |
{ | |
public void actionPerformed( ActionEvent event) { | |
sendData( event.getActionCommand() ); | |
enterField.setText( "" ); | |
} | |
} | |
); | |
add( enterField, BorderLayout.NORTH ); | |
displayArea = new JTextArea(); | |
add( new JScrollPane ( displayArea ), BorderLayout.CENTER ); | |
setSize( 600, 300); | |
setVisible( true ); | |
} | |
public void runClient() { | |
try { | |
connectToServer(); | |
getStreams(); | |
processConnection(); | |
} | |
catch (IOException ioException) | |
{ | |
ioException.printStackTrace(); | |
} | |
finally | |
{ | |
closeConnection(); | |
} | |
} | |
private void connectToServer() throws IOException { | |
displayMessage( "Attempting connection\n" ); | |
client = new Socket( InetAddress.getByName( chatServer ), 12321 ); | |
displayMessage( "Connected to: "+ | |
client.getInetAddress().getHostName() ); | |
} | |
private void getStreams() throws IOException { | |
output = new ObjectOutputStream( client.getOutputStream() ); | |
output.flush(); | |
input = new ObjectInputStream( client.getInputStream() ); | |
displayMessage("\nGot I/O streams\n" ); | |
} | |
private void processConnection() throws IOException { | |
setTextFieldEditable( true ); | |
do { | |
try { | |
message = ( String ) input.readObject(); | |
displayMessage( "\n" + message ); | |
} | |
catch ( ClassNotFoundException classNotFoundException ) | |
{ | |
displayMessage( "\nUnknown object type received" ); | |
} | |
} while ( !message.equals( "SERVER> TERMINATE")); | |
} | |
private void closeConnection() { | |
displayMessage("\nClosing connection"); | |
setTextFieldEditable(false); | |
try { | |
output.close(); | |
input.close(); | |
client.close(); | |
} | |
catch (IOException ioException) | |
{ | |
ioException.printStackTrace(); | |
} | |
} | |
private void sendData( String message ) { | |
try { | |
output.writeObject( "CLIENT>" + message); | |
output.flush(); | |
displayMessage("\nCLIENT> " + message); | |
} | |
catch (IOException ioException) | |
{ | |
displayArea.append("\nError writing object"); | |
} | |
} | |
private void displayMessage( final String messageToDisplay ) { | |
SwingUtilities.invokeLater( | |
new Runnable() { | |
public void run() { | |
displayArea.append( messageToDisplay); | |
} | |
} | |
); | |
} | |
private void setTextFieldEditable( final boolean editable ) { | |
SwingUtilities.invokeLater( | |
new Runnable() { | |
public void run() { | |
enterField.setEditable( editable ); | |
} | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment