SHARE
TWEET

Untitled

a guest Aug 12th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //libraries are imported
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8.  
  9.  
  10. public class Server1
  11. {
  12.     public static void main(String[] args) throws Exception
  13.     {
  14.         //socket for server is created
  15.         ServerSocket messageSocket1 = null, messageSocket2 = null;
  16.         //Try to set IPv4 instead of 6 and initialize serverSocket to port 5555(this port can be changed if so desired)
  17.         try
  18.         {
  19.             System.setProperty("java.net.preferIPv4Stack", "true");
  20.  
  21.             messageSocket1 = new ServerSocket(5555,1);
  22.             messageSocket2 = new ServerSocket(5556,1);
  23.  
  24.         }
  25.         //catch if couldn't initialize serverSocket on port 5555, tell user and quit program
  26.         catch (IOException e)
  27.         {
  28.             System.out.println("Couldn't listen on port 5555/5556 Check port");
  29.             System.exit(-1);
  30.         }
  31.         //create socket for client
  32.         Socket inSocket1 = null;
  33.         Socket inSocket2 = null;
  34.         //infinite while loop keeps program running until told to quit
  35.         while(true)
  36.         {
  37.             System.out.println("Looking for clients");
  38.             //try to connect server socket to client socket
  39.             try
  40.             {
  41.                 inSocket1 = messageSocket1.accept();
  42.                 inSocket2 = messageSocket2.accept();
  43.             }
  44.             //catch if can't connect, tell user and quit program
  45.             catch (IOException e)
  46.             {
  47.                 System.out.println("didn't find client, check port/client program");
  48.                 System.exit(-1);
  49.             }
  50.             //in takes in the user input from the user
  51.             System.out.println("Connection to clients made");
  52.             BufferedReader in1 = new BufferedReader(new InputStreamReader(inSocket1.getInputStream()));
  53.             BufferedReader in2 = new BufferedReader(new InputStreamReader(inSocket2.getInputStream()));
  54.             //out sends user input to the client
  55.             PrintWriter out1 = new PrintWriter(inSocket1.getOutputStream(),true);
  56.             PrintWriter out2 = new PrintWriter(inSocket2.getOutputStream(),true);
  57.             //String takes input from in
  58.             String userInput1 = "";
  59.             String userInput2 = "";
  60.             //while user does not type "exit"
  61.             while(true)
  62.             {
  63.                 //userInput from client1
  64.                 if(userInput1.equals("Client 1: quit")||userInput2.equals("Client 2: quit"))
  65.                 {
  66.                     //close all sockets
  67.                     out1.close();
  68.                     in1.close();
  69.                     messageSocket1.close();
  70.                     inSocket1.close();
  71.                     out2.close();
  72.                     in2.close();
  73.                     messageSocket2.close();
  74.                     inSocket2.close();
  75.  
  76.                     System.exit(0);
  77.                 }
  78.                 userInput1 = in1.readLine();
  79.                 //sned message to client2
  80.                 out2.println(userInput1);
  81.                 //userinput from client2
  82.                 userInput2 = in2.readLine();
  83.                 //send the message to the client1
  84.                 out1.println(userInput2);
  85.  
  86.             }
  87.  
  88.  
  89.         }
  90.  
  91.     }
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. import java.io.BufferedReader;
  99. import java.io.IOException;
  100. import java.io.InputStreamReader;
  101. import java.io.PrintWriter;
  102. import java.net.Socket;
  103. import java.util.Scanner;
  104.  
  105. public class Client1
  106. {
  107.     public static void main(String args[]) throws IOException
  108.     {
  109.         Socket messageSocket = null;
  110.         PrintWriter out = null;
  111.         BufferedReader in = null;
  112.  
  113.         try
  114.         {
  115.             //get program to use IPv4 instead of 6
  116.             System.setProperty("java.net.preferIPv4Stack", "true");
  117.  
  118.             messageSocket = new Socket("localhost", 5555);
  119.             out = new PrintWriter(messageSocket.getOutputStream(), true);
  120.             in = new BufferedReader(new InputStreamReader(messageSocket.getInputStream()));
  121.  
  122.         }
  123.         catch (IOException e)
  124.         {
  125.             System.out.println("can't open socket on 5555");
  126.             System.exit(-1);
  127.         }
  128.  
  129.         Scanner keyboard = new Scanner(System.in);
  130.  
  131.         System.out.println("Enter your message. type quit to exit");
  132.         String message = ("Client 1: "+keyboard.nextLine());
  133.  
  134.         while(true)
  135.         {
  136.             if(message.contains("quit"))
  137.             {
  138.                 System.exit(0);
  139.             }
  140.  
  141.             out.println(message);
  142.             System.out.println(in.readLine());
  143.             System.out.print("you: ");
  144.             message = ("Client 1: "+keyboard.next());
  145.  
  146.         }
  147.  
  148.  
  149.     }
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. import java.io.BufferedReader;
  157. import java.io.IOException;
  158. import java.io.InputStreamReader;
  159. import java.io.PrintWriter;
  160. import java.net.Socket;
  161. import java.util.Scanner;
  162.  
  163. public class Client2
  164. {
  165.     public static void main(String args[]) throws IOException
  166.     {
  167.         Socket messageSocket = null;
  168.         PrintWriter out = null;
  169.         BufferedReader in = null;
  170.  
  171.         try
  172.         {
  173.             //get program to use IPv4 instead of 6
  174.             System.setProperty("java.net.preferIPv4Stack", "true");
  175.             messageSocket = new Socket("localhost", 5556);
  176.             out = new PrintWriter(messageSocket.getOutputStream(), true);
  177.             in = new BufferedReader(new InputStreamReader(messageSocket.getInputStream()));
  178.  
  179.         }
  180.         catch (IOException e)
  181.         {
  182.             System.out.println("can't open socket on 5555");
  183.             System.exit(-1);
  184.         }
  185.  
  186.         Scanner keyboard = new Scanner(System.in);
  187.  
  188.         String message = "";
  189.  
  190.         while(true)
  191.         {
  192.             if(message.contains("quit"))
  193.             {
  194.                 System.exit(0);
  195.             }
  196.  
  197.             System.out.println(in.readLine());
  198.             System.out.print("you: ");
  199.             message = ("Client 2: "+keyboard.nextLine());
  200.             out.println(message);
  201.         }
  202.  
  203.  
  204.  
  205.     }
  206. }
RAW Paste Data
Top