-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
82 lines (78 loc) · 3.53 KB
/
Client.java
File metadata and controls
82 lines (78 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.*;
import java.net.*;
import java.awt.Point;
import java.util.List;
public class Client {
public static void main(String[] args) {
Socket socket = null;
ObjectInputStream objectInputStream = null;
// Retry logic: Try to connect to the server a few times before failing
int maxRetries = 5; // Maximum number of retry attempts
int retryInterval = 2000; // Wait time between retries in milliseconds (2 seconds)
int attempts = 0;
while (attempts < maxRetries) {
try {
// Try to connect to the server
socket = new Socket("localhost", 12345); // Connect to server on port 12345
System.out.println("Connected to server.");
objectInputStream = new ObjectInputStream(socket.getInputStream());
break; // If connected, exit the loop
} catch (ConnectException e) {
attempts++;
System.err.println("Failed to connect to the server. Attempt " + attempts + " of " + maxRetries);
if (attempts < maxRetries) {
System.err.println("Retrying in " + (retryInterval / 1000) + " seconds...");
try {
Thread.sleep(retryInterval); // Wait before retrying
} catch (InterruptedException ie) {
System.err.println("Retry interrupted.");
}
} else {
System.err.println("Could not connect to the server after " + maxRetries + " attempts. Exiting.");
return; // Exit the program after max retries
}
} catch (IOException e) {
System.err.println("An I/O error occurred: " + e.getMessage());
e.printStackTrace();
return; // Exit if there's an I/O error
}
}
try {
// Process incoming data from the server
while (true) {
try {
// Read the list of click positions from the server
List<Point> clicks = (List<Point>) objectInputStream.readObject();
System.out.println("Received clicks from server: " + clicks);
} catch (EOFException eof) {
// Server has closed the connection
System.out.println("Server closed connection. Exiting.");
break;
} catch (SocketException se) {
// Handle the case when the connection is reset (e.g., server closes the socket)
System.err.println("Connection with the server was reset. Exiting.");
break;
} catch (ClassNotFoundException e) {
System.err.println("Class not found while reading data from server.");
e.printStackTrace();
}
}
} catch (IOException e) {
System.err.println("Error while communicating with the server: " + e.getMessage());
e.printStackTrace();
} finally {
// Clean up resources
try {
if (objectInputStream != null) {
objectInputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
System.err.println("Error while closing resources: " + e.getMessage());
e.printStackTrace();
}
}
}
}