java.io.StreamCorruptedException:无效的流标题:70707070

时间:2015-09-30 05:30:26

标签: java sockets io stream

所以我看到了许多不同的问题,但没有明确的帮助,至少对我的理解或个人申请而言。我正在制作一个插座"聊天室"允许用户通过中央服务器向所选用户发送图像的程序。我可以建立连接的客户端,但在发送图像时会发生此错误。这是我的代码:

客户端:

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                s = new Socket("localhost", 4000);

                while (s.isConnected()) {
                    oos = new ObjectOutputStream(s.getOutputStream());
                    if (!initialized) {
                        oos.writeObject(identity);
                        oos.flush();
                        oos.reset();
                        initialized = true;
                    }
                    baos = new ByteArrayOutputStream(1000);
                    // Take screenshot
                    BufferedImage img = new Robot()
                            .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

                    // Write img to baos 
                    ImageIO.write(img, "jpg", baos);

                    // Send image over socket
                    oos.writeObject(baos.toByteArray());
                    oos.flush();
                    oos.reset();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };
    thread.start();

中央服务器主页:

    public Home() {
    initComponents();
    textView = new Terminal(terminal);

    users = new CopyOnWriteArrayList<>();

    Thread startServer = new Thread(new ServerStart());
    startServer.start();
}

public class ServerStart implements Runnable {

    @Override
    public void run() {

        try {
            serverSock = new ServerSocket(4000);

            terminal.append("Server started...\n");

            while (true) {
                // Detect client connection
                Socket clientSock = serverSock.accept();

                Thread thread = new Thread(new ClientHandler(clientSock));
                thread.start();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

public class ClientHandler implements Runnable {

    Socket socket;

    public ClientHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
                User user = new User(socket);
                terminal.append(user.getName() + " connected as " + user.getType() + "...\n");
                if (user.getType().equals(User.TYPE_01)) {
                    users.add(user);
                } else {
                    User client = findUser(user);
                    while(true){
                        user.sendScreen(client.receiveScreen());
                    }
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private User findUser(User user) {
        for (User client : users) {
            if (client.getCompany().equals(user.getCompany())) {
                if (client.getName().equals(user.getName())) {
                    return client;
                }
            }
        }
        return null;
    }
}

中央服务器用户:

    public static final String TYPE_00 = "VIEWER";
public static final String TYPE_01 = "CLIENT";

private byte[] bytes;
private ObjectInputStream in;
private ObjectOutputStream out;
private String company, name, type;

public User(Socket socket) throws IOException {
    this.out = new ObjectOutputStream(socket.getOutputStream());
    this.in = new ObjectInputStream(socket.getInputStream());
    setUserType();
}

public void sendScreen(byte[] bytes) {
    try {
        out.writeObject(bytes);
        out.flush();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public byte[] receiveScreen() {
    byte[] bytes = null;
    try {
        bytes = (byte[]) in.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

public String getCompany() {
    return company;
}

public String getName() {
    return name;
}

public String getType() {
    return type;
}

public void setUserType()
{
    String[] strings = null;
    try{
        strings = (String[])in.readObject();
        type = strings[0];
        company = strings[1];
        name = strings[2];
    } catch(Exception e){
        e.printStackTrace();
    }
}

客户端查看器:

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                Socket s = new Socket("localhost",4000);


                String[] strings = { TYPE, "Vision", "cadams" };


                while (s.isConnected()) {

                    if(!initialized){
                        System.out.println("initialized");
                        oos = new ObjectOutputStream(s.getOutputStream());
                        oos.writeObject(strings);
                        oos.flush();
                        oos.reset();
                        initialized = true;
                    }
                    ois = new ObjectInputStream(s.getInputStream());

                    byte[] bytes = (byte[]) ois.readObject();
                    BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
                    ImageIcon ico = new ImageIcon(
                            img.getScaledInstance(viewer.getWidth(), viewer.getHeight(), Image.SCALE_SMOOTH));
                    viewer.setIcon(ico);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();

}

我做了一些广泛的研究,并且知道这可能来自我的溪流看起来像一碗拉面,但我还没有看到任何关于如何根据来源修复它的建议。我感谢那些可以做出贡献的人,如果我有什么办法可以帮助我进一步了解,请告诉我。

1 个答案:

答案 0 :(得分:0)

不要继续创建新的ObjectInput/OutputStreams.在套接字的生命周期中使用相同的内容。

NB while (s.isConnected()无效。当对等设备断开连接时,它并没有神奇地变为错误。您需要处理流的结束和各种套接字终止异常。