Skip to content Skip to sidebar Skip to footer

Java TCP Server Accepting Images As Well As Strings From Android Client

i want to send string messages as well as images.i am designing android chatting application which user is allowed to share images and chat(string) as well Java Server is at the b

Solution 1:

Keep Seperate Sockets that makes it simpler,or you need syncronisation

1)use objectoutputstream for image thats fastest method of image transfer

2)use dataoutputstream for string

Image Sending code

      File myFile = new File ("d:\\ab.jpg");
      byte [] mybytearray  = new byte [(int)myFile.length()];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);
      OutputStream os = sock.getOutputStream();
      System.out.println("Sending...");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
      os.close();

Image receiving code

    int filesize=6022386; // filesize temporary hardcoded
    long start = System.currentTimeMillis();
    int bytesRead;
    int current = 0;

    File f=new File("d:\\ab.jpg");
    f.createNewFile();
    // receive file
    byte [] mybytearray  = new byte [filesize];
    InputStream is = socket.getInputStream();
    FileOutputStream fos = new FileOutputStream(f);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;
   do {
       bytesRead =
       is.read(mybytearray, current, (mybytearray.length-current));
       if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);
    count=current;
    Copy=mybytearray.clone();
    bos.write(mybytearray, 0 , current);
    bos.flush();
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    bos.close();
    fos.close();
    Status=true;

Solution 2:

You have discovered the entire reason for an application layer protocol. Sockets themselves just provide ways to send bytes they don't say anything about what the bytes mean. This is where an application layer protocol comes into play.

You could design a protocol that first sends a type byte (0 or 1) for image or data. Then you might also want a integer (4 bytes) for length (length of data or image). This would allow the receiving side to read those first and know how many bytes to receive and whether those bytes represented data or an image.


Solution 3:

Check out the Java Serializable interface: http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html

In summary, a class that implements Serializable can be serialized with no additional work if all of its fields are also Serializable. So:

public class Message<Img_Type> implements Serializable {
    protected String mText = null;
    protected <Img_Type> mImage = null;    // Img_Type must implement Serializable

    // ... constructor(s), get/set methods        
}

You can then write and read these objects using Sockets, ObjectInputStream, and ObjectOutputStream. To read (one) Message:

    Socket socket;    // initialize your socket
    Message msg;
    try {
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        Message msg = (Message) ois.readObject();
        ois.close();
    } catch (ClassNotFoundException e) {
        // handle exception
    }
    // handle message

Writing a Message can be done in a similar manner using ObjectOutputStream. In this example, you can check the Message fields against null to see what it contains.

One thing to be conscious of is that the constructor for ObjectInputStream will not return until it reads the header of an Object from the input stream, e.g. a Message is ready to be received. Additionally, closing the object input stream on the server will throw an exception on the client if they have a corresponding output stream open, and vice versa.


Post a Comment for "Java TCP Server Accepting Images As Well As Strings From Android Client"