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
具有连续通信的Java套接字_Java_Sockets_Tcp_Javax.imageio - Fatal编程技术网

具有连续通信的Java套接字

具有连续通信的Java套接字,java,sockets,tcp,javax.imageio,Java,Sockets,Tcp,Javax.imageio,我希望能够在客户端执行以下操作: 开放式插座 发送数据 等等 发送更多数据 和服务器端: 打开msocket 接收数据 等等 继续接收(反复循环) 目前我无法循环:( 我有一个客户端: 建造商: s = new Socket(ServerIPAddr, 6100); out = new ObjectOutputStream(s.getOutputStream()); 循环功能: public void sendImage(BufferedImage image){ S

我希望能够在客户端执行以下操作:

  • 开放式插座
  • 发送数据
  • 等等
  • 发送更多数据
和服务器端:

  • 打开msocket
  • 接收数据
  • 等等
  • 继续接收(反复循环)
目前我无法循环:(

我有一个客户端:

建造商:

s = new Socket(ServerIPAddr, 6100);
out = new ObjectOutputStream(s.getOutputStream());
循环功能:

    public void sendImage(BufferedImage image){

    System.out.println("Starting to send");
    try {

        ImageIO.write(image, "PNG", out);
        System.out.println("Sent");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // png is lossless

}
    public void run() {
    int i = 0;
    System.out.println("Connection accepted");
    System.out.println("Threaded spanwed");
    try {ObjectInputStream in = new ObjectInputStream(current.getInputStream());
        while (capturing) {
            ImageIO.write(ImageIO.read(in), "PNG", new File("test" + i++
                    + ".png"));
            in.close();
        }
    } catch (IOException e) {e.printStackTrace();}

}
这可以一个接一个地多次调用

服务器端我有一个constructor:

    public ServerConnectionThread(Socket clientSocket) {current = clientSocket;}
运行功能:

    public void sendImage(BufferedImage image){

    System.out.println("Starting to send");
    try {

        ImageIO.write(image, "PNG", out);
        System.out.println("Sent");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // png is lossless

}
    public void run() {
    int i = 0;
    System.out.println("Connection accepted");
    System.out.println("Threaded spanwed");
    try {ObjectInputStream in = new ObjectInputStream(current.getInputStream());
        while (capturing) {
            ImageIO.write(ImageIO.read(in), "PNG", new File("test" + i++
                    + ".png"));
            in.close();
        }
    } catch (IOException e) {e.printStackTrace();}

}
有人知道为什么它只对发送的第一条数据有效吗?并且拒绝继续发送吗

-更新-

while (capturing) {
            BufferedImage b = ImageIO.read(in);
            if(b == null){
                continue;
            }
            ImageIO.write(b, "PNG", new File("test" + i++
                    + ".png"));

        }

似乎解决了此问题

为什么要关闭inputStream

试试这个-

 while (capturing) {
            ImageIO.write(ImageIO.read(in), "PNG", new File("test" + i++ ".png"));
            //in.close();
      }

当(捕获){ImageIO.write(ImageIO.read(in),“PNG”,新文件(“test”+i++“.PNG”);}在发送第一个图像后,它似乎会说:iamge为null,这很烦人。这是因为它正在循环到fast吗?是否有任何方法让它等待,直到它有数据?为什么在不写入对象时使用ObjectOutputStream?