Created
May 14, 2012 13:33
-
-
Save normanmaurer/2694018 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
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.net.InetSocketAddress; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.concurrent.CountDownLatch; | |
public class Server { | |
final CountDownLatch latch = new CountDownLatch(1); | |
public void run(int port) throws IOException, InterruptedException { | |
final ServerSocket socket = new ServerSocket(); | |
socket.bind(new InetSocketAddress(port)); | |
Thread t = new Thread(new Runnable() { | |
public void run() { | |
Socket s = null; | |
try { | |
s = socket.accept(); | |
ObjectInputStream in = new ObjectInputStream(s.getInputStream()); | |
System.out.println(in.readObject()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (s != null) { | |
try { | |
s.close(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
latch.countDown(); | |
} | |
} | |
}); | |
t.start(); | |
} | |
public void await() throws InterruptedException { | |
latch.await(); | |
} | |
public static void main(String args[]) throws NumberFormatException, IOException, InterruptedException { | |
Server server = new Server(); | |
server.run(Integer.parseInt(args[0])); | |
server.await(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment