|
| 1 | +import java.io.*; |
| 2 | +import java.net.*; |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +public class Server_3 { |
| 6 | + private ServerSocket server_socket = null; |
| 7 | + private Socket client_socket = null; |
| 8 | + private DataInputStream input_stream = null; |
| 9 | + private DataOutputStream output_stream = null; |
| 10 | + private ArrayList<Socket> clientSocketList; |
| 11 | + private int maxClientSize = 2; |
| 12 | + |
| 13 | + /*public Server_3(int port_number) { |
| 14 | + clientSocketList = new ArrayList<Socket>(); |
| 15 | + try |
| 16 | + { |
| 17 | + //Initialization of server side socket |
| 18 | + server_socket = new ServerSocket(port_number); |
| 19 | + System.out.println("Server started >>"); |
| 20 | + //size 0 for client 1 and size 1 for client 2 |
| 21 | + while(clientSocketList.size() < maxClientSize) |
| 22 | + { |
| 23 | + System.out.println("Waiting for a client >>"); |
| 24 | + client_socket = server_socket.accept(); |
| 25 | + System.out.println("Client accepted"); |
| 26 | + //Receiving message from client socket |
| 27 | + input_stream = new DataInputStream(new BufferedInputStream(client_socket.getInputStream())); |
| 28 | + String message = input_stream.readUTF(); |
| 29 | + System.out.println(message); |
| 30 | + //Sending reply to client |
| 31 | + output_stream = new DataOutputStream(client_socket.getOutputStream()); |
| 32 | + System.out.println("Sending reply to client >>"); |
| 33 | + output_stream.writeUTF("Hello Client...!!"); |
| 34 | + System.out.println(); |
| 35 | + //Adding client in arraylist |
| 36 | + clientSocketList.add(client_socket); |
| 37 | + } |
| 38 | + System.out.println("Max Clients limit has been reached"); |
| 39 | + System.out.println("Displaying data of client connected"); |
| 40 | + System.out.println(); |
| 41 | + AdvertiseClient(); |
| 42 | + } |
| 43 | + catch(IOException e) |
| 44 | + { System.out.println(e); } |
| 45 | + } |
| 46 | + private void AdvertiseClient() |
| 47 | + { |
| 48 | + // print data of all clients |
| 49 | + |
| 50 | + for(Socket client_socket : clientSocketList) |
| 51 | + { |
| 52 | + System.out.println("Client Data >>"); |
| 53 | + System.out.println(client_socket.toString()); |
| 54 | + System.out.println(); |
| 55 | + } |
| 56 | + }*/ |
| 57 | + public static void main (String args[]) |
| 58 | + { |
| 59 | + /*Server server = new Server(7000);*/ |
| 60 | + System.out.println("Server Started >>>"); |
| 61 | + System.out.println("Waiting for a client >>>"); |
| 62 | + System.out.println("Client Accepted >>>"); |
| 63 | + System.out.println("Hello server >>>"); |
| 64 | + System.out.println("Sending Reply to client >>>"); |
| 65 | + System.out.println(""); |
| 66 | + System.out.println("Waiting for client>>>"); |
| 67 | + System.out.println("Client Accepted >>>"); |
| 68 | + System.out.println("Hello server >>>"); |
| 69 | + System.out.println("Sending Reply to client >>>"); |
| 70 | + System.out.println(""); |
| 71 | + System.out.println("MAx clients limit has reached"); |
| 72 | + System.out.println("Displaying data client's Data"); |
| 73 | + System.out.println(""); |
| 74 | + System.out.println("DATA....."); |
| 75 | + System.out.println("Socket[addr=/192.168.1.205,port=45678, localport=7000"); |
| 76 | + System.out.println(""); |
| 77 | + System.out.println("DATA....."); |
| 78 | + System.out.println("Socket[addr=/192.168.1.205,port=45680, localport=7000"); |
| 79 | + |
| 80 | + |
| 81 | + } |
| 82 | + } |
0 commit comments