Created
January 27, 2012 14:32
-
-
Save mathieuancelin/1689044 to your computer and use it in GitHub Desktop.
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 com.foo.loic; | |
import java.io.*; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.Socket; | |
import java.net.SocketAddress; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.*; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class App { | |
public static final int NBR_CLIENT = 10; | |
public static final int PORT = 9000; | |
public static final String HOST = "192.168.86.187"; | |
public static final int NBR_REQUEST = 100; | |
public static ExecutorService service; | |
public static class ClientThread extends Thread { | |
private final String host; | |
private final int port; | |
private CountDownLatch latch = new CountDownLatch(NBR_REQUEST); | |
public ClientThread(String host, int port) { | |
this.host = host; | |
this.port = port; | |
} | |
@Override | |
public void run() { | |
int nbr = 0; | |
while(latch.getCount() > 0) { | |
Socket socket = openSocket(host, port); | |
readFromSocket(socket); | |
latch.countDown(); | |
nbr++; | |
if (nbr % 10 == 0) { | |
System.out.println("[" + Thread.currentThread().getName() + "] REQUEST " + nbr); | |
} | |
} | |
} | |
public void stopClient() { | |
while(latch.getCount() > 0) { | |
latch.countDown(); | |
} | |
} | |
} | |
private static final List<ClientThread> threads = new ArrayList<ClientThread>(); | |
private static final List<Future> ends = new ArrayList<Future>(); | |
public static void main(String[] args) { | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public void run() { | |
for (ClientThread t : threads) { | |
t.stopClient(); | |
} | |
service.shutdownNow(); | |
} | |
}); | |
service = Executors.newFixedThreadPool(NBR_CLIENT); | |
for (int i = 0; i < NBR_CLIENT; i++) { | |
threads.add(new ClientThread(HOST, PORT)); | |
} | |
for (ClientThread t : threads) { | |
ends.add(service.submit(t)); | |
} | |
for (Future f : ends) { | |
try { | |
f.get(); | |
} catch (Exception ex) { | |
} | |
} | |
System.out.println("END"); | |
System.exit(0); | |
} | |
private static Socket openSocket(String server, int port) { | |
Socket socket; | |
try { | |
InetAddress inteAddress = InetAddress.getByName(server); | |
SocketAddress socketAddress = new InetSocketAddress(inteAddress, port); | |
socket = new Socket(); | |
int timeoutInMs = 10 * 1000; // 10 seconds | |
socket.connect(socketAddress, timeoutInMs); | |
return socket; | |
} catch (Exception ste) { | |
ste.printStackTrace(); | |
return null; | |
} | |
} | |
public static String REQUEST = "GET /available HTTP/1.0\r\nHost: 1" + HOST + "\r\nContent-length: 0\r\n\r\n"; | |
private static void readFromSocket(Socket socket) { | |
BufferedReader bufferedReader = null; | |
BufferedWriter bufferedWriter = null; | |
try { | |
bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); | |
bufferedWriter.write(REQUEST); | |
bufferedWriter.flush(); | |
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
StringBuilder sb = new StringBuilder(); | |
String str; | |
while ((str = bufferedReader.readLine()) != null) { | |
sb.append(str).append("\n"); | |
} | |
bufferedReader.close(); | |
bufferedWriter.close(); | |
} catch (IOException ex) { | |
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); | |
} finally { | |
try { | |
bufferedReader.close(); | |
bufferedWriter.close(); | |
} catch (IOException ex) { | |
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment