Advertisement
HansMckenzie

Untitled

Apr 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. // This is the Server code, save as DateServer.java
  2. import java.net.*;
  3. import java.io.*;
  4. import java.util.Date;
  5.  
  6. public class DateServer {
  7. public static void main(String[] args) throws IOException {
  8.  
  9. int n=0;
  10. WorkerThread work;
  11. try {
  12. // This creates a listener socket
  13. ServerSocket sock = new ServerSocket(6013);
  14. while (true) {
  15. n++;
  16. Socket client = sock.accept();
  17. //WorkerThread work = new WorkerThread(client, n);
  18. new WorkerThread(client, n).start();
  19. }
  20. }
  21. catch (IOException ioe) {
  22. System.err.println(ioe);
  23. }
  24. }
  25. }
  26.  
  27. class WorkerThread extends Thread{
  28.  
  29. private Socket client;
  30. int numberOfConnections = 0;
  31.  
  32. public WorkerThread(Socket client, int numberOfConnections){
  33.  
  34. this.client = client;
  35. this.numberOfConnections = numberOfConnections;
  36.  
  37. }
  38.  
  39. public void run(){
  40.  
  41. try{
  42. PrintWriter pout = new
  43. PrintWriter(client.getOutputStream(),true);
  44.  
  45. pout.println(new java.util.Date().toString());
  46. System.out.println("finishing processing client " + numberOfConnections);
  47.  
  48. client.close();
  49.  
  50. }catch(IOException ioe){}
  51.  
  52. }
  53. }
  54.  
  55.  
  56. //LOOK BELOW
  57.  
  58.  
  59.  
  60. /* replace DateServer.java with this code.
  61.  
  62. javac DateServer.java
  63. javac DateClient.java
  64.  
  65. java DateServer & java DateClient
  66.  
  67. java DateServer
  68. java DateServer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement