Advertisement
Shailrshah

Token Ring

Apr 24th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. //Server.java
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7.  
  8. public class Server implements Runnable{
  9.     static ServerSocket ss;
  10.     Socket s = null;
  11.     Server(Socket s){
  12.         this.s = s;
  13.     }
  14.     public void run(){
  15.         try{
  16.             BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  17.             while(true)
  18.                 System.out.println(br.readLine());
  19.         } catch(Exception e){e.printStackTrace();}
  20.     }
  21.     public static void main(String[] args) throws IOException{
  22.         ss = new ServerSocket(9812);
  23.         System.out.println("Server running on port 5555");
  24.         while(true)
  25.             new Thread(new Server(ss.accept())).start();
  26.     }
  27. }
  28.  
  29. //Client1.java
  30. import java.io.BufferedReader;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33. import java.io.PrintStream;
  34. import java.net.Socket;
  35. import java.net.ServerSocket;
  36.  
  37. public class Client1 {
  38.     public static void main(String[] args) throws IOException {
  39.         Socket server = new Socket("localhost", 9812);
  40.         Socket client2 = new ServerSocket(4576).accept();
  41.        
  42.         PrintStream toServer = new PrintStream(server.getOutputStream());
  43.         PrintStream toClient2 = new PrintStream(client2.getOutputStream());
  44.        
  45.         BufferedReader fromClient2 = new BufferedReader(new InputStreamReader(client2.getInputStream()));
  46.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  47.        
  48.         String str = "Token";
  49.         while(true){
  50.             if(str.equals("Token")){
  51.                 System.out.print("Do you wan to send a message to the server?");
  52.                 if(br.readLine().equals("Yes")){
  53.                     System.out.print("Enter your message: ");
  54.                     toServer.println(br.readLine());
  55.                 }
  56.                 toClient2.println("Token");
  57.             }
  58.             System.out.println("Waiting for the token.");
  59.             str = fromClient2.readLine();
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65. //Client2.java
  66. import java.io.BufferedReader;
  67. import java.io.IOException;
  68. import java.io.InputStreamReader;
  69. import java.io.PrintStream;
  70. import java.net.Socket;
  71.  
  72. public class Client2 {
  73.     public static void main(String[] args) throws IOException {
  74.         Socket server = new Socket("localhost", 9812);
  75.         Socket client1 = new Socket("localhost", 4576);
  76.        
  77.         PrintStream toServer = new PrintStream(server.getOutputStream());
  78.         PrintStream toClient1 = new PrintStream(client1.getOutputStream());
  79.        
  80.         BufferedReader fromClient1 = new BufferedReader(new InputStreamReader(client1.getInputStream()));
  81.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  82.        
  83.         String str = "";
  84.        
  85.         while(true){
  86.             if(str.equals("Token")){
  87.                 System.out.print("Do you wan to send a message to the server?");
  88.                 if(br.readLine().equals("Yes")){
  89.                     System.out.print("Enter your message: ");
  90.                     toServer.println(br.readLine());
  91.                 }
  92.                 toClient1.println("Token");
  93.             }
  94.             System.out.println("Waiting for the token.");
  95.             str = fromClient1.readLine();
  96.         }
  97.        
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement