|
| 1 | +import java.io.DataInputStream; |
| 2 | +import java.io.DataOutputStream; |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.Socket; |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +class Client { |
| 8 | + public static void main(String[] args) { |
| 9 | + int port = 999; |
| 10 | + System.out.print("Enter your Participant ID (1/2/3): "); |
| 11 | + Scanner scanner = new Scanner(System.in); |
| 12 | + int id = scanner.nextInt(); |
| 13 | + scanner.nextLine(); |
| 14 | + port += id; |
| 15 | + Socket socket = null; |
| 16 | + try { |
| 17 | + socket = new Socket("localhost", port); |
| 18 | + } catch (Exception e) { |
| 19 | + System.out.println("!! ERROR while Connecting to the Server !!"); |
| 20 | + System.out.println("** Host has not yet created the Quiz, Host should connect first **"); |
| 21 | + System.exit(1); |
| 22 | + } |
| 23 | + System.out.println("Connected to server.."); |
| 24 | + System.out.print("Enter your name: "); |
| 25 | + String name = scanner.nextLine(); |
| 26 | + try { |
| 27 | + DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); |
| 28 | + DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); |
| 29 | + dataOutputStream.writeUTF(name); |
| 30 | + int total_ques = dataInputStream.readInt(); |
| 31 | + int time_per_ques = dataInputStream.readInt(); |
| 32 | + for (int i = 0; i < total_ques; i++) { |
| 33 | + String ques = dataInputStream.readUTF(); |
| 34 | + String options = dataInputStream.readUTF(); |
| 35 | + System.out.println(ques); |
| 36 | + System.out.println(options); |
| 37 | + timer t = new timer(); |
| 38 | + String ans = t.getInput(time_per_ques); |
| 39 | + dataOutputStream.writeUTF(ans); |
| 40 | + } |
| 41 | + |
| 42 | + System.out.println("!! You had successfully completed the Quiz !!"); |
| 43 | + System.out.println("***Contact the owner for Final Score***"); |
| 44 | + socket.close(); |
| 45 | + } catch (IOException e) { |
| 46 | + System.out.println(e); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments