Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send images and text on the same Java socket?

I am new to java, and a bit stuck on my media control project. What I need to do is send images and text to a client application. I have found some example code of receiving an image and displaying it (listed below). This works really well, but I need to have a way of sending an image and text down the same socket connection.

Its there a way of deciding what type of data it is? I want it to my program to display it if it is an image, and show a msgbox if it is text.

Should I have two sockets, one for images and one for text?

Unfortunately the variable names in the sample code are not english which makes it a little harder to follow:

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Cliente extends JFrame {

  JTextField tf;
  Lienzo lienzo;
  Socket con;
  ObjectOutputStream salida;
  ObectInputStream entrada;

  public Cliente(){

    super("Cliente");
    Container c = getContentPane();

    tf = new JTextField(10);
    lienzo = new Lienzo();

    c.add(lienzo);
    pack();
    setSize(100,100);
    setVisible(true);
  }

  void ejecutar(){

    try{

      con = new Socket("127.0.0.1",5700);
      salida = new ObjectOutputStream(con.getOutputStream());
      salida.flush();
      entrada = new ObjectInputStream(con.getInputStream());

      procesar();

    }
    catch(IOException e){}

  }

  void procesar() throws IOException {

    try{

      while(true){
        ImageIcon img = (ImageIcon) entrada.readObject();
        escribir(img);
      }
    }catch(ClassNotFoundException e){}

  }

  void escribir(final ImageIcon img){

    SwingUtilities.invokeLater(
      new Runnable(){
          public void run(){
          lienzo.pinta(img);
        }
      }
    );
  }

  public static void main( String args[] ){
    Cliente cl = new Cliente();
    cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cl.ejecutar();
  }

  class Lienzo extends JPanel{

    ImageIcon img = null ;

    public Lienzo(){

    }

    public void pinta(ImageIcon img){
      this.img = img;
      repaint();
    }

    public void paintComponent(Graphics g){

      super.paintComponent(g);

      if(img!=null)
        img.paintIcon(this,g,10,10);
    }
  }
}
like image 872
Luke Avatar asked Mar 04 '26 10:03

Luke


2 Answers

If you have control of both server and client then a very simple way would be to define classes for the different messages you want to send and then use object output/input streams to do the heavy lifting, aka marshaling.

Heres some wishy-washy pseudo-code:

class Message<T extends Serializable> implements Serializable {
    private T payload;

    public Message() {
            super();
    }

    public Message(T data) {
            super();
            setPayload(data);
    }

    public T getPayload() {
            return payload;
    }

    public void setPayload(T aPayload) {
            payload = aPayload;
    }
}

On server:

ObjectOutputStream oos = new ObjectOutputStream(...);
oos.writeObject(new Message<String>("Hello World"));

On client:

ObjectInputStream ois = new ObjectInputStream(...);
Message<?> msg = (Message<?>) ois.readObject();
if(msg.getPayload() instanceof String) // do one thing
else if(msg.getPayload() instanceof Image) // do some other thing
like image 177
Perception Avatar answered Mar 05 '26 23:03

Perception


You are talking about sending multiple types of data across a socket. At this point, you may have to redesign your application. Instead of sending ImageIcon object through the socket you may send a different object (you will have to create your own class like below )

public class ImageAndText implements Serializable{
        String text;
        ImageIcon image;
 // and so on...
}
  • This image and text object can have a String attribute that represents your text and ImageIcon attribute that represants your image. At the client end (in your procesar() method, read an ImageAndText type of object and get individual attributes and display them.
like image 44
ring bearer Avatar answered Mar 05 '26 23:03

ring bearer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!