Skip to content

Instantly share code, notes, and snippets.

@rockwotj
Last active June 29, 2017 08:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rockwotj/2a9804b389a8ec8a789f to your computer and use it in GitHub Desktop.
Save rockwotj/2a9804b389a8ec8a789f to your computer and use it in GitHub Desktop.
This java class is a high level wrapper for the java ServerSocket class and is made to make string communication a lot easier.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* A TCP/IP server that can be connected to and is a high level interface for
* I/O interactions over TCP protocol.
*
* @author rockwotj. Created Nov 13, 2014.
*/
public class Server
{
/**
* The underlying server socket
*/
private ServerSocket server;
/**
* The client socket
*/
private Socket client;
/**
* An internal reader from the client socket
*/
private BufferedReader clientReader;
/**
* An internal writer to the client socket
*/
private PrintWriter clientWriter;
/**
* If the server should be verbose
*/
private boolean verboseMode;
/**
* The port the server listens on.
*/
public static final int PORT = 4774;
/**
*
* Constructs a new high level I/O server.
*
* @throws IOException
*/
public Server() throws IOException
{
this.server = new ServerSocket(PORT);
this.verboseMode = false;
}
public Server(boolean verboseMode) throws IOException
{
this.verboseMode = verboseMode;
this.server = new ServerSocket(PORT);
if (verboseMode)
{
System.out.println(String.format(
"Listening on Address: %s port: %d", Server.getIPAddress(),
new Integer(PORT)));
}
}
/**
*
* This will block until it recieves a connection from a client socket.
*
* @throws IOException
*/
public void acceptConnection() throws IOException
{
this.client = this.server.accept();
this.clientReader = new BufferedReader(new InputStreamReader(
this.client.getInputStream()));
this.clientWriter = new PrintWriter(new OutputStreamWriter(
this.client.getOutputStream()));
if (this.verboseMode)
{
System.out.println("Connected to Client at IPAsddress: "
+ this.client.getInetAddress().getHostAddress());
}
}
/**
*
* Returns the IPAddress of the current machine.
*
* @return
* @throws UnknownHostException
*/
public static String getIPAddress() throws UnknownHostException
{
return InetAddress.getLocalHost().getHostAddress();
}
/**
*
* Returns the Loopback Address of the current machine.
*
* @return
* @throws UnknownHostException
*/
public static String getLoopbackAddress() throws UnknownHostException
{
return InetAddress.getLoopbackAddress().getHostAddress();
}
/**
*
* Reads a string from the client connection. This will block until it
* recieves a newline from the client.
*
* @return
* @throws IOException
*/
public String readLineFromClient() throws IOException
{
// If there is no client throw an error
if (this.client == null)
{
throw new IOException(
"Trying to read while client not established.");
}
// Wait until the client sends something to the server
String clientRequest = this.clientReader.readLine();
if (clientRequest == null)
{
// Client closed the connection
this.closeClient();
throw new IOException(
"Client closed connection while trying to read");
}
if (this.verboseMode)
{
System.out.println("Client sent string: " + clientRequest.trim());
}
return clientRequest.trim();
}
/**
*
* Writes a string to the client connection.
*
* @param output
* @throws IOException
*/
public void writeToClient(String output) throws IOException
{
// If there is no client throw an exception
if (this.client == null)
{
throw new IOException(
"Trying to write while client not established.");
}
if (this.verboseMode)
{
System.out.println("Server sent string: " + output);
}
// Write the output and flush the buffer
this.clientWriter.println(output);
this.clientWriter.flush();
}
/**
*
* Closes the server.
*
* @throws IOException
*/
public void closeServer() throws IOException
{
if (this.verboseMode)
{
System.out.println("Closing server");
}
this.closeClient();
this.server.close();
}
/**
*
* Closes the connection to the client socket. The server is able to accept
* new connections after this.
*
* @throws IOException
*/
public void closeClient() throws IOException
{
if (this.verboseMode)
{
System.out.println("Closing client");
}
// Close this first as there is no exception that can be thrown.
if (this.clientWriter != null)
{
this.clientWriter.close();
this.clientWriter = null;
}
// Close the reader
if (this.clientReader != null)
{
this.clientReader.close();
this.clientReader = null;
}
// Finally close the client
if (this.client != null)
{
this.client.close();
this.client = null;
}
}
public static String getNetworkName() throws UnknownHostException
{
return InetAddress.getLocalHost().getHostName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment