我的服务器运行,客户端可以连接到它,但我不认为客户端正在发送视频。这是客户端的代码。它只打印一次“发送视频”。
我正在尝试通过客户端屏幕的许多屏幕截图发送视频,因为我不知道有更好的方法来做到这一点。
import java.net.Socket;
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.RenderedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
public class Main extends Thread{
private static Socket socket;
private static PrintWriter printWriter;
private static String inputLine;
private static Thread thread;
private static BufferedReader bufferedReader;
private static boolean Connected = true;
public static void main(String[] args) {
// System.out.println("What is the computer's IP address?");
String Ip_addr = JOptionPane
.showInputDialog("Enter the IP number of the server to connect: ");
String IP = Ip_addr;
while (Connected) {
Connected = true;
try {
System.out.println("Client running...");
socket = new Socket(IP, 8080);
Connected = false;
bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
} catch (Exception e) {
}
}
System.out.println("thread running");
thread = new Thread(new Main());
thread.start();
}
public void run() {
while(true) {
RenderedImage screenShot;
try {
if((inputLine = bufferedReader.readLine()) != null){
System.out.println(inputLine);
JOptionPane.showMessageDialog(null, inputLine);
}
screenShot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenShot, "PNG", socket.getOutputStream());
System.out.println("Sending video");
Thread.sleep(10);
} catch (HeadlessException | AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
这可能是由于我缺乏线程知识。然后这里是服务器中处理为视频制作线程的命令的部分,
Thread viewing = new Thread(new View(Main.sockets[Selected]));
viewing.start();
这条线至少第一次工作,我不知道它是否适用于其他任何人,因为它在线程中得到一个空指针:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.Socket;
import javax.imageio.ImageIO;
public class View extends Applet implements Runnable {
private Socket Client;
public boolean Running = true;
public Graphics Buffer;
public Image offscreen;
public BufferedImage image;
public View(Socket client) {
Client = client;
}
public void init() {
setSize(500,500);
offscreen = createImage(500, 500);
Buffer = offscreen.getGraphics();
}
public void paint(Graphics g) {
Buffer.clearRect(0, 0, 500, 500);
Buffer.drawImage(image, 500, 500, null);
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
public void run() {
while(Running) {
try {
try {
image = ImageIO.read(Client.getInputStream());
System.out.println("painting");
Graphics g = image.getGraphics(); //NullPointerException
g.drawImage(image, 500, 500, null);
} catch (IOException e) {
e.printStackTrace();
}
repaint();
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}