Created
June 27, 2014 18:22
-
-
Save joshuakfarrar/398a8ba899e437fde79c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 main; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.io.*; | |
import java.net.Socket; | |
public class ClientTest { | |
private static final int PORT = 1338; | |
private static final int TIMEOUT = 2000; | |
Server server; | |
Thread serverThread; | |
@Before | |
public void startServer() throws Exception { | |
try { | |
server = new Server(PORT, TIMEOUT); | |
serverThread = new Thread(server); | |
serverThread.start(); | |
} catch (Exception e) { | |
e.printStackTrace(System.err); | |
throw e; | |
} | |
} | |
@After | |
public void shutdownServer() throws InterruptedException { | |
if (server != null) { | |
server.stopProcessing(); | |
serverThread.join(); | |
} | |
} | |
class TestClient implements Runnable { | |
@Override | |
public void run() { | |
try { | |
receiveMessage(); | |
} catch (IOException ignore) { | |
ignore.printStackTrace(System.err); | |
} | |
} | |
private void receiveMessage() throws IOException { | |
Socket socket = new Socket("localhost", PORT); | |
System.out.println(StringUtils.getMessage(socket)); | |
socket.close(); | |
} | |
} | |
@Test(timeout = 10000) | |
public void shouldRunInUnder10Seconds() throws Exception { | |
Thread[] threads = new Thread[10]; | |
for (int i = 0; i < threads.length; ++i) { | |
threads[i] = new Thread(new TestClient()); | |
threads[i].start(); | |
} | |
for (int i = 0; i < threads.length; ++i) { | |
threads[i].join(); | |
} | |
} | |
} |
This file contains hidden or 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 main; | |
import java.io.*; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.net.SocketException; | |
import java.util.Date; | |
public class Server implements Runnable { | |
ServerSocket serverSocket; | |
volatile boolean listeningForSocketConnections = true; | |
public Server(int port, int timeout) throws IOException { | |
serverSocket = new ServerSocket(port); | |
serverSocket.setSoTimeout(timeout); | |
} | |
@Override | |
public void run() { | |
System.out.println("Starting server! :D"); | |
while (listeningForSocketConnections) { | |
listenForSocketConnections(); | |
} | |
} | |
private void listenForSocketConnections() { | |
try { | |
final Socket socket = serverSocket.accept(); | |
if (socket == null) | |
return; | |
Runnable clientHandler = new Runnable() { | |
@Override | |
public void run() { | |
try { | |
StringUtils.sendMessage(socket, new Date().toString()); | |
closeIgnoringException(socket); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
Thread clientConnection = new Thread(clientHandler); | |
clientConnection.start(); | |
} catch (IOException e) { | |
handle(e); | |
} | |
} | |
private void handle(IOException e) { | |
if (!(e instanceof SocketException)) | |
e.printStackTrace(); | |
} | |
private void closeIgnoringException(Socket socket) throws IOException { | |
if (socket != null) | |
try { | |
socket.close(); | |
} catch (IOException ignore) { | |
} | |
} | |
public void stopProcessing() { | |
System.out.println("Stopping server! :("); | |
try { | |
listeningForSocketConnections = false; | |
serverSocket.close(); | |
} catch (IOException ignore) { | |
} | |
} | |
} |
This file contains hidden or 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 main; | |
import java.io.*; | |
import java.net.Socket; | |
public class StringUtils { | |
public static String getMessage(Socket socket) throws IOException { | |
InputStream stream = socket.getInputStream(); | |
ObjectInputStream ois = new ObjectInputStream(stream); | |
return ois.readUTF(); | |
} | |
public static void sendMessage(Socket socket, String message) throws IOException { | |
OutputStream os = socket.getOutputStream(); | |
ObjectOutputStream oos = new ObjectOutputStream(os); | |
oos.writeUTF(message); | |
oos.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment