-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
52 lines (44 loc) · 1.75 KB
/
Client.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
package Java_Socket_VoWifi;
import java.io.IOException;
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 Client {
private static final int PORT = 9999;
private static final String SERVER_IP = "192.168.6.231"; //To be replaced with the actual IP of the server
public static void main(String[] args) {
try {
Socket socket = new Socket(SERVER_IP, PORT);
System.out.println("Connected to server.");
receiveAndDisplayVideo(socket);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Error: Failed to connect to the server.");
}
}
private static void receiveAndDisplayVideo(Socket socket) throws IOException {
try {
DataInputStream dis = new DataInputStream(socket.getInputStream());
JFrame frame = new JFrame("Video Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel();
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
while (true) {
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) {
e.printStackTrace();
System.err.println("Error: Failed to receive video from the server.");
}
}
}