Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image java中如何通过TCP套接字发送图像_Image_Sockets_Tcp - Fatal编程技术网

Image java中如何通过TCP套接字发送图像

Image java中如何通过TCP套接字发送图像,image,sockets,tcp,Image,Sockets,Tcp,朋友们! 我想在我的电脑上安装远程电脑屏幕!我曾尝试用UDP编写,现在我正尝试用TCP协议编写,但出现了一些问题!如果有人能告诉我问题出在哪里,我将不胜感激。。。这是我的代码: Server.java public Server(int port){ this.G_PORT = port; } void sendMessage() throws IOException, AWTException{ servSocket = new ServerSocket(G_PORT);

朋友们! 我想在我的电脑上安装远程电脑屏幕!我曾尝试用UDP编写,现在我正尝试用TCP协议编写,但出现了一些问题!如果有人能告诉我问题出在哪里,我将不胜感激。。。这是我的代码: Server.java

public Server(int port){
    this.G_PORT = port;
}
void sendMessage() throws IOException, AWTException{
    servSocket = new ServerSocket(G_PORT);
    System.out.println("Waiting...");
    cSocket = servSocket.accept();
    System.out.println("Server has connected to " + cSocket.getRemoteSocketAddress() + " on " + cSocket.getPort() +" port");

        BufferedOutputStream buffOS = new BufferedOutputStream(cSocket.getOutputStream());
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle r = new Rectangle(d);

        KeyStroke keyStr = KeyStroke.getKeyStroke("ESCAPE");

        FileInputStream fileIN = null;
        OutputStream out = null;

    //while(keyStr.getKeyCode() != 27){
        bot = new Robot();
        buffIMG = bot.createScreenCapture(r);
        ImageIO.write(buffIMG, "jpeg", new File("D:\\ScreenShots\\test.jpeg"));
        fileIN = new FileInputStream(new File("D:\\ScreenShots\\test.jpeg"));
        out = cSocket.getOutputStream();
        int read, readTotal = 0;
        byteToIMG = new byte[BUFF_SIZE];

        while((read =  fileIN.read())!= -1){
            out.write(byteToIMG,0,read);
            System.out.println("IMG sended");
        }
    //}
    out.close();
    fileIN.close();
    cSocket.close();        
} 
和Client.java

void recievMessage(long starTime) throws UnknownHostException, IOException{
    cSocket = new Socket(InetAddress.getByName(G_HOST), G_PORT);
    System.out.println("Connected");
    JFrame frame = new JFrame("My frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    JLabel label = new JLabel();
    ImageIcon icon;

    InputStream in = cSocket.getInputStream();
    byteToIMG = new byte[BYTE_SIZE];
    int read;
    int readTotal = 0;
        while((read = in.read(byteToIMG)) != -1){
            readTotal += read;
            InputStream imgIN = new ByteArrayInputStream(byteToIMG);
            buffIMG = ImageIO.read(imgIN);
            icon = new ImageIcon();
            icon.setImage(buffIMG);
            label.setIcon(icon);
            frame.getContentPane().add(label, BorderLayout.CENTER);
            frame.pack();
            System.out.println("IMG recieved");
        }
} 

请帮忙解决这个问题!如果您有其他解决方案,建议!谢谢

您假设套接字上的每次读取都会传递一个完整的图像。你不能这样假设。它可能只传递一个字节

幸运的是,这要简单得多:

buffImg = Imageio.read(in);
它在发送方也简单得多。你不需要这个文件。就打电话

ImageIO.write(buffImg, "jpeg", out);