socket:用计时器获取二进制图像

时间:2011-05-08 10:28:40

标签: java image sockets

我正在尝试从具有客户端的服务器获取多个图像。

我的最终目标是通过套接字(在同一台计算机上)从Java程序中使用Ogre从C ++程序生成的电影中获取图像,因为JNI对我来说似乎很难。

为了尝试它,我在java下创建了一个客户端/服务器,仅用于测试,但它不能正常工作。事实上,结果是相当随机的,我有时会得到2个,有时是4个图像,但绝不是全部。

我认为我的流没有很好地同步,我也想知道UDP是不是更合适,但我不知道该怎么做。

以下是我使用的代码:

public class Client {

static final int port = 3334;
static final String host = "aluminod";
public static final double STEP = 2000.0;
public static final int DELAY = 0;

private Socket socket;
private PrintWriter printWriter;
private BufferedReader input;
private int imgNumber = 0;
private static MoviePanel pane;
private Timer timer;

public Client() throws UnknownHostException, IOException{
    this.socket = new Socket(host, port);
    System.out.println("SOCKET = " + socket);
    this.printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
    this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    start();
}

public static void main(String[] args){
    JFrame frame = new JFrame("test");
    pane = new MoviePanel();
    frame.setPreferredSize(new Dimension(600,400));
    frame.setSize(new Dimension(600,400));
    frame.setVisible(true);
    frame.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-300, Toolkit.getDefaultToolkit().getScreenSize().height/2-200);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.add(pane);


    try {
        new Client();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void start(){
    this.timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            try {
                sendParamsToVC();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, DELAY, (long) STEP);
}

private void sendParamsToVC() throws IOException {
    System.out.println("---sending---");
    String mess = buildMessage();


    //envoi du message au serveur
    printWriter.println(mess);

    // lecture de la réponse du serveur
    InputStream is = socket.getInputStream();
    int taille = Integer.parseInt(input.readLine());
    byte[] mybytearray = new byte[taille];
    is.read(mybytearray, 0, taille);    
    BufferedImage img = ImageIO.read(new ByteArrayInputStream(mybytearray));

    pane.change(img);
    imgNumber++;
}

private String buildMessage(){
    String mess;
    if(imgNumber <10)mess = "000"+imgNumber;
    else if(imgNumber<=13)mess = "00"+imgNumber;
    else {
        mess = "NO_IMG";
        timer.cancel();
    }
    return mess;

}

}

public class Server {
static final int port = 3334;

public static void main(String[] args) throws Exception {
    ServerSocket servsocket = new ServerSocket(port); 
    while (true) {
        Socket client = servsocket.accept();
        System.out.println("connection accepted");

        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        BufferedOutputStream output = new BufferedOutputStream(client.getOutputStream());

        while (true) {
            String str = reader.readLine();          // lecture du message
            if (str.equals("NO_IMG")){
                System.out.println("ECHO = " + str);
                System.out.println("fermeture");
                break;
            }
            System.out.println("ECHO = " + str);   // trace locale

            //renvoi de l'image
            OutputStream os = client.getOutputStream();
            byte[]data = getByteFromImage(str);
            PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
            writer.println(data.length);
            writer.flush();
            os.write(data, 0, data.length);
            os.flush();

        }
        output.close();
        reader.close();
        client.close();
    }
}

public static byte[] getByteFromImage(String numImage) {        

    BufferedImage img = ImageLoader.createBufferedImage(numImage);

    /** On crée la nouvelle image */
    BufferedImage bufferedImage = new BufferedImage(
                img.getWidth(null),
                img.getHeight(null),
                BufferedImage.TYPE_INT_BGR );
    Graphics g = bufferedImage.createGraphics();
    g.drawImage(img,0,0,null);
    g.dispose();             
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        ImageIO.write(bufferedImage, "jpeg", out);
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    byte buffer[] = out.toByteArray();

    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return (buffer);
}

}

1 个答案:

答案 0 :(得分:0)

读取器/写入器流用于字符数据。不要将它们用于二进制数据。使用(缓冲)InputStream / OutputStream

编辑:忘记我上面说的话。我检查了你的其余代码,看起来你正在混合流,并且客户端在发送命令时不会刷新流。可以在客户端缓冲的方法。

Edit2:另一个错误是您没有检查读取的内容。它返回一个int,表示你已收到许多字节。这并不总是与缓冲区大小相同。它可以更少。你需要处理它。