AkirusG

Untitled

Dec 5th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. package edu.lmu.cs.networking;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.Date;
  8.  
  9. /**
  10.  * A TCP server that runs on port 9090.  When a client connects, it
  11.  * sends the client the current date and time, then closes the
  12.  * connection with that client.  Arguably just about the simplest
  13.  * server you can write.
  14.  */
  15. public class DateServer {
  16.  
  17.     /**
  18.      * Runs the server.
  19.      */
  20.     public static void main(String[] args) throws IOException {
  21.         ServerSocket listener = new ServerSocket(9090);
  22.         try {
  23.             while (true) {
  24.                 Socket socket = listener.accept();
  25.                 try {
  26.                     PrintWriter out =
  27.                         new PrintWriter(socket.getOutputStream(), true);
  28.                     out.println(new Date().toString());
  29.                 } finally {
  30.                     socket.close();
  31.                 }
  32.             }
  33.         }
  34.         finally {
  35.             listener.close();
  36.         }
  37.     }
  38. }
Add Comment
Please, Sign In to add comment