-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientViewer.java
69 lines (58 loc) · 2.44 KB
/
ClientViewer.java
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
//package cn_project;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.io.DataInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
public class ClientViewer {
// should not be between 0 to 1023 (well known port), 1024- 49151 user/registered port
private static final int PORT = 9998;
private static final String SERVER_IP = "IP_ADDRESS"; // Replace with the actual IP address of the server
public static void main(String[] args) {
while(true) {
try {
Socket socket = new Socket(SERVER_IP, PORT);
System.out.println("Connected to server.");
receiveAndDisplayVideo(socket);
} catch (ConnectException ce) {
// Handle connection timeout exception
System.out.println("Connection timed out. Retrying...");
// Wait for a while before attempting to reconnect
try {
Thread.sleep(5000); // Adjust the sleep duration as needed
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void receiveAndDisplayVideo(Socket socket) throws IOException {
DataInputStream dis = new DataInputStream(socket.getInputStream());
JFrame frame = new JFrame("Video Viewer");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel();
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
while (true) {
try {
int imageSize = dis.readInt();
byte[] imageBytes = new byte[imageSize];
dis.readFully(imageBytes, 0, imageSize);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
label.setIcon(new ImageIcon(image));
frame.repaint();
} catch (IOException e) {
frame.setVisible(false);
// Connection closed by the server
System.out.println("Connection closed by the server");
break;
}
}
}
}