Java套接字编程视频聊天数据输入输出流错误

时间:2019-03-22 15:35:54

标签: java sockets networking client-server

我正在制作一个Java网络套接字程序,该程序可让您进行无线视频聊天。我同时运行了服务器和客户端代码,但始终会出现诸如负数组长度之类的错误,或者jframe无法正确显示数据。我怀疑这是网络部分的问题。这是代码。

ServerWorker.java

package ServerSide;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;


import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class ServerWorker extends Thread {
    private final OutputStream output;
    private final DataInputStream input;


    public ServerWorker(Socket client) throws IOException {
        output = new DataOutputStream(client.getOutputStream());
        input = new DataInputStream(client.getInputStream());
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    }

    @Override
    public void run() {
        byte[] data;
        Mat capture;
        JFrame frame = new JFrame();
        try {

            while(true){

                int cols = input.readInt();
                int rows = input.readInt();
                int length = cols * rows * 3;
                System.out.println(cols);
                System.out.println(rows);
                System.out.println(length);
                data = new byte[length];

                input.read(data);

                capture = new Mat(640,480,16);
                capture.put(0,0,data);
                showResult(capture,frame);
                Thread.sleep(100);









            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void showResult(Mat img,JFrame frame) {
        Imgproc.resize(img, img, new Size(640, 480));
        MatOfByte matOfByte = new MatOfByte();
        Imgcodecs.imencode(".jpg", img, matOfByte);
        byte[] byteArray = matOfByte.toArray();
        BufferedImage bufImage = null;
        try {
            InputStream in = new ByteArrayInputStream(byteArray);
            bufImage = ImageIO.read(in);

            frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
            frame.pack();
            frame.setVisible(true);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}

Server.java

package ServerSide;


import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;


public class Server {

    private final int port;
    private ArrayList<ServerWorker> clients = new ArrayList<ServerWorker>();

    public ArrayList<ServerWorker> getClients() {
        return clients;
    }


    public ArrayList<String> getOnlineClients() {
        return onlineClients;
    }


    private ArrayList<String> onlineClients;

    Server(int port) throws IOException {
        this.port = port;
        ServerSocket socket = new ServerSocket(port);
        InetAddress address = InetAddress.getLocalHost();

        onlineClients = new ArrayList<>();

        System.out.println("New Server Succesfully at "+address.getHostAddress() + " at port " + port );
        System.out.println("Ready for connections");
            while(true){
            Socket client = socket.accept();
            System.out.println("Connected");
            ServerWorker worker = new ServerWorker(client);
            clients.add(worker);
            worker.run();

        }
    }
}

Client.java

package ClientSide;


import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;

import java.io.*;
import java.net.Socket;

public class Client {

    private final DataInputStream in;
    private final DataOutputStream out;
    private int port;
    private String host;
    String name;
    VideoCapture camera ;
    Mat capture;
    Client(String name, String host, int port) throws IOException, InterruptedException {

        this.name = name;
        this.host = host;
        this.port = port;
        camera = new VideoCapture();
        capture = new Mat();

        Socket socket = new Socket(host,port);
        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());

        camera.open(0);
        byte[] b;
        while(true){

            camera.read(capture);
            b = new byte[capture.channels() * capture.cols() * capture.rows()];
            capture.get(0,0,b);
            //System.out.println(capture.type());
            out.writeInt(capture.cols());
            out.writeInt(capture.rows());
            //out.writeInt(b.length);
            out.write(b);
            Thread.sleep(1000);
            //System.out.println(camera.isOpened());
        }



    }
}

我已经对图像的大小进行了一些调试System.out.println,但这是不正确的。服务器端的输出

New Server Succesfully at 192.168.135.1 at port 8888
Ready for connections
Connected
640
480
921600
134414604
117574920
934866208

我希望这个问题可以解决。

0 个答案:

没有答案