Skip to content

Commit ccde7c1

Browse files
authored
Create Client.java
Creating Quiz System Java project
1 parent ee0d9ac commit ccde7c1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Quiz project/Client.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)