Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Server.java
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server implements Runnable{
- static ServerSocket ss;
- Socket s = null;
- Server(Socket s){
- this.s = s;
- }
- public void run(){
- try{
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- while(true)
- System.out.println(br.readLine());
- } catch(Exception e){e.printStackTrace();}
- }
- public static void main(String[] args) throws IOException{
- ss = new ServerSocket(9812);
- System.out.println("Server running on port 5555");
- while(true)
- new Thread(new Server(ss.accept())).start();
- }
- }
- //Client1.java
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.net.Socket;
- import java.net.ServerSocket;
- public class Client1 {
- public static void main(String[] args) throws IOException {
- Socket server = new Socket("localhost", 9812);
- Socket client2 = new ServerSocket(4576).accept();
- PrintStream toServer = new PrintStream(server.getOutputStream());
- PrintStream toClient2 = new PrintStream(client2.getOutputStream());
- BufferedReader fromClient2 = new BufferedReader(new InputStreamReader(client2.getInputStream()));
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str = "Token";
- while(true){
- if(str.equals("Token")){
- System.out.print("Do you wan to send a message to the server?");
- if(br.readLine().equals("Yes")){
- System.out.print("Enter your message: ");
- toServer.println(br.readLine());
- }
- toClient2.println("Token");
- }
- System.out.println("Waiting for the token.");
- str = fromClient2.readLine();
- }
- }
- }
- //Client2.java
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.net.Socket;
- public class Client2 {
- public static void main(String[] args) throws IOException {
- Socket server = new Socket("localhost", 9812);
- Socket client1 = new Socket("localhost", 4576);
- PrintStream toServer = new PrintStream(server.getOutputStream());
- PrintStream toClient1 = new PrintStream(client1.getOutputStream());
- BufferedReader fromClient1 = new BufferedReader(new InputStreamReader(client1.getInputStream()));
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str = "";
- while(true){
- if(str.equals("Token")){
- System.out.print("Do you wan to send a message to the server?");
- if(br.readLine().equals("Yes")){
- System.out.print("Enter your message: ");
- toServer.println(br.readLine());
- }
- toClient1.println("Token");
- }
- System.out.println("Waiting for the token.");
- str = fromClient1.readLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement