Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Server protocol for image

Posted on 2005-04-07
4
Medium Priority
?
207 Views
Last Modified: 2010-03-31
Hi All,

I have this server protocol  for transmitting text.

I need help to change this  to a protocol to send an image.

I thank you in advance.

Delphi3

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


public class localHostServerImage extends JFrame
{  
  private JTextField textConnection;
  private JLabel labelConnection;
  Container container = null;
  Vector vec1;
  Vector vec2;
  int b = 0;
 
 
  public localHostServerImage()    
  {    
    container = getContentPane() ;
    container.setLayout(new FlowLayout());
   
    labelConnection = new JLabel("Connection ") ;
    container.add(labelConnection );
    textConnection = new JTextField(15) ;
    container.add(textConnection);
    textConnection.setText("Disconnected");
   
    setSize ( 200,400) ;
    setVisible(true);
    try {
      FileInputStream fin = new FileInputStream("ColorPixlData.txt");
      ObjectInputStream inOStrm = new ObjectInputStream(fin);
      //image dimensions
      vec1 = (Vector) inOStrm.readObject();
      //image pixlColors
      vec2 = (Vector) inOStrm.readObject();
      inOStrm.close();
     
      //creating server socket binding at port # 3000
      ServerSocket server=new ServerSocket(3000);
      System.out.println("Server binded at

"+((server.getInetAddress()).getLocalHost()).getHostAddress()+":3000");
      System.out.println("Run the Client");

      //ready to accept client request

      while (true){
        b = 0;
        Socket socket=server.accept();
        textConnection.cut();
        textConnection.setText("Connected\n");
       
       
        //opening the input stream to read data from client connection

        BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(in.readLine());

        //using output stream responsing data

        ObjectOutputStream out=new  ObjectOutputStream(socket.getOutputStream());
       
        while ((true)&&(b<1))
        {
          out.writeObject(vec1);
          //System.out.println(vec1);

          out.writeObject(vec2);
          //System.out.println(vec2);
          out.flush();
          //out.close();
          ++ b;
        }
        //closing the in & out streams
        in.close();
        out.close();
        socket.close();
      }//end while true
    }
    catch(Exception err)
    {
      System.err.println("* err"+err);
      textConnection.cut();
      textConnection.setText("Disconnected\n");
    }
  }
 
  public static void main(String a[])
  {
    new localHostServerImage();
  }  
}
0
Comment
Question by:delphi3
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13729172
convert your Image to byte array and then back to Image.
You can use this example: http://www.javaalmanac.com/egs/javax.imageio/Graphic2File.html but instead of saving it to a file save it to ByteArrayOuptuStream and the call toByteArray().
You can use the objectoutputstream to write the byte array and then use the objectinputstream to read the byte array
then covert back the byte array by using Toolkit.getDefaultToolkit().createImage(<byte_array>) or new ImageIcon(<byte_array>)
0
 
LVL 4

Author Comment

by:delphi3
ID: 13778214
Hi aozarov,

I have puzzled  over this for days and have tried tens of different models.

I need more hints please.

Here is the server as modified to what extent I can understand.  While it works as a sever it fails to meet what you have outlined.

And I have yet to understand how to get the client in a reverse setup to read the image.

Delphi3

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;



// You can use this example:
// http://www.javaalmanac.com/egs/javax.imageio/Graphic2File.html 
// but instead of saving it to a file save it to
// ByteArrayOutputStream and the call toByteArray().

public class serverIOImage extends JFrame
{  
  private JTextField textConnection;
  private JLabel labelConnection;
  Container container = null;
  int b = 0;
  int width;
  int height;
 
  public static void main(String a[])
  {
    new serverIOImage();
  }
 
  public serverIOImage()    
  {    
    container = getContentPane() ;
    container.setLayout(new FlowLayout());
   
    labelConnection = new JLabel("Connection ") ;
    container.add(labelConnection );
    textConnection = new JTextField(15) ;
    container.add(textConnection);
    textConnection.setText("Disconnected");
   
    setSize ( 200,400) ;
    setVisible(true);
    Image image = null;
   
    try {
     
      InputStream is = new BufferedInputStream(
                                               new FileInputStream("NadiaFace.jpg"));    
      image = ImageIO.read(is);      
      BufferedImage bi = new BufferedImage(new ImageIcon(image).getIconWidth(), new ImageIcon(image).getIconHeight(), BufferedImage.TYPE_INT_RGB);          
      Graphics2D big = bi.createGraphics();
      big.drawImage(image,0,0,this);
      width = bi.getWidth();
      System.out.println(width);
      height = bi.getHeight();
      System.out.println(height );
      int data[][] = new int[width][height];
     
      for (int w=0; w < width; w++){
        for (int h=0; h < height; h++){
          data[w][h] = bi.getRGB(w,h);
          System.out.println(bi.getRGB(w,h));
        }
      }
     
      //creating server socket binding at port # 3000
      ServerSocket server=new ServerSocket(3000);
      System.out.println("Server binded at "+((server.getInetAddress()).getLocalHost()).getHostAddress()+":3000");
      System.out.println("Run the Client");
      //ready to accept client request
      while (true){//<<<<<<<<<<<
        b = 0; //>>>>>>>>> Delph3 added this
        Socket socket=server.accept();
        textConnection.cut();
        textConnection.setText("Connected\n");
       
        //opening the input stream to read data from client connection
        BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(in.readLine());
        //using output stream responsing data
        ObjectOutputStream out=new  ObjectOutputStream(socket.getOutputStream());
       
        while ((true)&&(b<1))
        {
          out.writeObject(Integer.toString (width));
          out.writeObject(Integer.toString (height));  
          for (int w=0; w<width; w++){
            for (int h=0; h<height; h++){
              //data[w][h] = bi.getRGB(w,h);
             
              out.writeObject(Integer.toString(data[w][h]));
            }}
          out.flush();
          //out.close();
          ++ b;
        }
        //closing the in & out streams
        in.close();//<<<<<<<<<<<
        out.close();//<<<<<<<<<<<
        socket.close();//<<<<<<<<<<
      }//end while true//<<<<<<<<<<<    
    }
    catch(Exception err)
    {
      System.err.println("* err"+err);
      textConnection.cut();
      textConnection.setText("Disconnected\n");
    }
  }
}


0
 
LVL 15

Accepted Solution

by:
aozarov earned 200 total points
ID: 13778565
Similar to the example in the provided link you can create a byte array from your BufferedImage by doing something like this:
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ImageIO.write(image, "jpg",  byteArray); // image is your BufferedImage
byte[] bytes = byteArray.toByteArray();

// transmit the bytes object
...

In the client side you use the ObjectInputStream to readObject and cast it to byte[]
From byte[] you can create image using the API I gave you above:
Toolkit.getDefaultToolkit().createImage(<byte_array>) or new ImageIcon(<byte_array>)
0
 
LVL 4

Author Comment

by:delphi3
ID: 13799856
Thanks for your efforts,
Can't solve it, too complicated, and not good models to look at, but you have made a good effort to respond.


D3
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question