Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:32:13+00:00 2026-05-22T01:32:13+00:00

I’m trying to get multiple images from a server with a client. My ultimate

  • 0

I’m trying to get multiple images from a server with a client.

My ultimate goal is to get images from a movie generated by a C++ program with Ogre in a Java program by sockets (on the same computer), because JNI seems very difficult to me.

To try it, i made a client/server both under java, just for test, but it doesn’t work well. In fact, the result is pretty random and i get sometimes 2, sometimes 4 images, but never all.

I think my streams are not well synchronized, and i was wondering too if UDP couldn’t be more appropriated, but i don’t know how to do it.

Here are the codes i use :

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:32:14+00:00Added an answer on May 22, 2026 at 1:32 am

    The reader/writer streams are for character data. Don’t use them for binary data. Use (Buffered)InputStream/OutputStream

    Edit: Forget what I said above. I checked the rest of your code, and it looks like you are mixing streams, and the client isn’t flushing the stream when it sends commands. The means that is can be buffered on the client.

    Edit2: Another error is that you don’t check what read returns. It returns an int that says home many bytes you have received. That isn’t always the same as your buffer size. It can be less. You need to handle that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.