AkirusG

Untitled

Dec 5th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 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.                     DataOutputStream data = new DataOutputStream(file);  
  27.                     data.writeInt(0xAAAA); // магическое число что-бы отеделять пакеты
  28.                     String json = "{text: \"hello world\"}";
  29.                     data.writeUTF(json);
  30.                     data.close();
  31.                 } finally {
  32.                     socket.close();
  33.                 }
  34.             }
  35.         }
  36.         finally {
  37.             listener.close();
  38.         }
  39.     }
  40. }
Add Comment
Please, Sign In to add comment