Skip to content

Instantly share code, notes, and snippets.

@fado
Created October 23, 2013 14:12
Show Gist options
  • Save fado/7119550 to your computer and use it in GitHub Desktop.
Save fado/7119550 to your computer and use it in GitHub Desktop.
Basic sever that echoes inputs from a client application...
package uk.ac.qub.networking;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
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 EchoServer extends JFrame {
public static final String PAURICISM = "wee wet";
// public static final String PAURICISM = "handsome";
// public static final String PAURICISM = "";
private JTextField enterField;
private JTextArea displayArea;
private ServerSocket server;
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
private int counter = 1; // number of connections
public EchoServer() {
super( "Server" );
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 startServer() {
try {
server = new ServerSocket (12321, 100);
while (true) {
try {
waitForConnection();
getStreams();
processConnection();
} // end try
catch (EOFException eofException) {
displayMessage("\nYour "+ PAURICISM + "connection was terminted by the server.");
}
finally {
closeConnection();
++counter;
}
}
}
catch (IOException ioException) {
ioException.printStackTrace();
}
}
private void waitForConnection() throws IOException {
displayMessage("Waiting for a "+ PAURICISM +" connection...");
connection = server.accept();
displayMessage("\nA "+ PAURICISM +" "+"connection ("+ counter + ") recieved from: "+
connection.getInetAddress().getHostName() );
}
private void getStreams() throws IOException {
output = new ObjectOutputStream( connection.getOutputStream() );
output.flush();
input = new ObjectInputStream ( connection.getInputStream() );
displayMessage("\nSome "+ PAURICISM + " I/O streams established.\n");
}
private void processConnection() throws IOException {
String message = "Your "+ PAURICISM +" connection was successful.";
sendData (message);
do {
try {
message = ( String ) input.readObject();
displayMessage("\n"+ message + "\n");
sendData (message.substring(6));
}
catch (ClassNotFoundException classNotFoundException)
{
displayMessage("\nUnknown object type received.");
}
} while ( !message.equals( "CLIENT> TERMINATE"));
}
private void closeConnection() {
displayMessage("\nYour "+ PAURICISM + " connection was terminated.");
setTextFieldEditable( false );
try {
output.close();
input.close();
connection.close();
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
}
private void sendData ( String message) {
try {
output.writeObject("SERVER> "+ message);
output.flush();
displayMessage("SERVER> "+ 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