×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    7

    Send Snapshot to server with socket

    In my application I do a snapshot in this way:

    Code:
    private void doSnapshot() {
            new Thread() {
                public void run() {
                    try {
                        byte [] snap = vidCtrl.getSnapshot("encoding=jpeg");
                        if (snap != null) {
                            Image im = Image.createImage(snap, 0, snap.length);
                            ImageItem imi = new ImageItem("", im, Item.	PLAIN, "");
                            vidForm.append(imi);
                            inviaimg(snap);
                            System.out.println("La fotografia è stata inviata");
                            risposta_foto();
                           }
                    } catch (MediaException me) {
                        System.err.println(me);
                    }
                }
            }.start();
        }
    And server receives:

    Code:
                 
    byte[] image=new byte[900000];
    leggi(image,60,true);
    Function leggi:
    Code:
    public int leggi(byte[] buffer, int time, boolean pulisci){
            int num_bytes=0;
            try{
                // Aspetta la read per time secondi
                client.setSoTimeout(time*1000);
                // Legge il messaggio come flusso di byte
                if(pulisci) read_from_client=new BufferedInputStream(client.getInputStream());
                num_bytes=read_from_client.read(buffer,0,buffer.length);
                return num_bytes;
            }catch(SocketTimeoutException ex){
                System.err.println("Tempo scaduto per l'operazione di lettura");
                ex.printStackTrace();
                try{
                    // Chiude la connessione
                    System.out.println("CHIUSURA DEL THREAD DI SERVIZIO PER IL CLIENT "+client.getInetAddress()+":"+client.getPort());
                    read_from_client.close();
                    write_to_client.close();
                    client.close();
                }catch(IOException dex){
                    dex.printStackTrace();
                    System.exit(0);
                }
            }catch(Exception ex){
                ex.printStackTrace();
                try{
                    // Chiude la connessione
                    System.out.println("CHIUSURA DEL THREAD DI SERVIZIO PER IL CLIENT "+client.getInetAddress()+":"+client.getPort());
                    read_from_client.close();
                    write_to_client.close();
                    client.close();
                }catch(IOException dex){
                    dex.printStackTrace();
                    System.exit(0);
                }
            }
            return 0;
        }
    When I try on emulator (Sun or Nokia) it run ok, but when I try on Nokia 5800 XE the server takes this error:
    Code:
    sun.awt.image.ImageFormatException: Invalid JPEG file structure: missing SOS marker
            at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
            at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:119)
            at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
            at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
            at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
    Can you help me?

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Send Snapshot to server with socket

    Quote Originally Posted by federico.cappabianca View Post
    Code:
    if(pulisci) read_from_client=new BufferedInputStream(client.getInputStream());
    Sorry to seem fussy, but can you stop putting an entire block on one line? It makes the code much harder to read. You might want to read the Sun Code Conventions for Java.

    Quote Originally Posted by federico.cappabianca View Post
    Code:
    num_bytes=read_from_client.read(buffer,0,buffer.length);
    How many bytes are actually being read? You're assuming this will read the entire image data, but it might not.

    I'd suggest you use on the phone:

    Code:
    DataOutputStream dout = connection.openDataOutputStream();
    dout.writeInt(snap.length);
    dout.write(snap);
    Then, on the server:

    Code:
    private static byte[] readImage() throws IOException {
        DataInputStream din = new DataInputStream(client.getInputStream());
        byte[] data;
        try {
            // how much data is there to read?
            int length = din.readInt();
            // create a buffer of the correct size
            data = new byte[length];
            // read blocks until we have all the data
            int readSoFar = 0;
            while (readSoFar < length) {
                readSoFar += din.read(data, readSoFar, length - readSoFar);
            }
        } finally {
            din.close();
        }
        return data;
    }
    Graham.

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Send Snapshot to server with socket

    Sorry, that could have been:

    Code:
    private static byte[] readImage() throws IOException {
        DataInputStream din = new DataInputStream(client.getInputStream());
        byte[] data;
        try {
            int length = din.readInt();
            data = new byte[length];
            din.readFully(data);
        } finally {
            din.close();
        }
        return data;
    }
    D'uh.

Similar Threads

  1. Retrieve a sound file from server side using Socket
    By 182guide in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2009-12-25, 05:36
  2. XML onLoad with background http server problem
    By simonjuric in forum [Archived] Flash Lite on Nokia Devices
    Replies: 5
    Last Post: 2009-06-08, 16:47
  3. Replies: 0
    Last Post: 2009-05-31, 13:08
  4. Socket Server Problem
    By mircomusolesi in forum Symbian
    Replies: 6
    Last Post: 2009-01-23, 15:55
  5. How to send mobile file data to Server on Nokia 6630?
    By SanjayKhuntia in forum Symbian
    Replies: 1
    Last Post: 2008-08-25, 11:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×