×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Feb 2010
    Posts
    78

    Post Sending image to a server

    hi,
    I need to send a "jpeg image" to a server using "socket communication".

    can anyone please help me

    Thanks in advance
    Deva

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: Sending image to a server

    Quote Originally Posted by devalin85 View Post
    hi,
    I need to send a "jpeg image" to a server using "socket communication".

    can anyone please help me

    Thanks in advance
    Deva
    Open a socket connection running on a specific port. Open the output stream and then write the image after converting in the row data.
    This is the server code..reading the data that is written to the server from the client.
    try
    {
    ServerSocketConnection ssc = (ServerSocketConnection)
    Connector.open("socket://:9002");
    StreamConnection sc = null;
    InputStream is = null;
    try{
    sc = ssc.acceptAndOpen();
    is = sc.openInputStream();
    int ch = 0;
    StringBuffer sb = new StringBuffer();
    while ((ch = is.read()) != -1){
    sb.append((char)ch);
    }
    System.out.println(sb.toString());
    } finally{
    ssc.close();
    sc.close();
    is.close();
    }
    } catch (IOException x) {
    x.printStackTrace();
    }
    and this is the client code..writing to the server from this client
    try{
    SocketConnection sc = (SocketConnection)
    Connector.open("socket://localhost:9002");
    OutputStream os = null;
    try{
    os = sc.openOutputStream();
    byte[] data = "Hello from a socket!".getBytes();
    os.write(data);
    } finally{
    sc.close();
    os.close();
    }
    } catch (IOException x){
    x.printStackTrace();
    }
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Regular Contributor
    Join Date
    Feb 2010
    Posts
    78

    Re: Sending image to a server

    Quote Originally Posted by raj_J2ME View Post
    Open a socket connection running on a specific port. Open the output stream and then write the image after converting in the row data.
    This is the server code..reading the data that is written to the server from the client.

    and this is the client code..writing to the server from this client
    Code:
       else if (command == start) {
        
          try{ 
    
               String path         =  System.getProperty("fileconn.dir.photos")+ "lat.jpeg";
               System.out.println("path is......."+path);
    /***************** it is in tis path v have the pic nd v give tis path in the file connection *******************/  
               FileConnection  fc   =  (FileConnection)Connector.open(path);
               System.out.println("fc.fileSize()= " + fc.fileSize());
               DataInputStream din  =  (DataInputStream)fc.openDataInputStream();
               bytedata             =   new byte[(int)fc.fileSize()];
               din.readFully(bytedata);
               System.out.println("length ::::::  "+bytedata.length);
               din.close();
               fc.close();
                  
                         /******** StreamConnection establish a socket connection with remote server *****************/
               StreamConnection connection    =  (StreamConnection)Connector.open("socket://192.168.1.13:65432");
               outputstream                   =   connection.openOutputStream();
               PrintStream output             =   new PrintStream(connection.openOutputStream());
               
               
               do {
                    System.out.println("write:" + index);
                    if(index + size > bytedata.length) {
                        System.out.println("insideeeeeeeee  dddd");
                        size = bytedata.length - index;
                    }
    /****************  send something to server **********************/                
                    outputstream.write(bytedata, index, size);
                    index+= size;
                } while(index < bytedata.length);
               
                         
    /******** "retrieve or read"  the "contents" or "response" of the requested page from Web server ***************/
               InputStream in = connection.openInputStream();
               int ch;
               while( ( ch = in.read() ) != -1 ){
                       System.out.print( (char) ch );
               }
               System.out.println("DONE");
               in.close();
               output.close();
               connection.close();
               outputstream.close();
          
          }catch( ConnectionNotFoundException error ){
                Alert alert = new Alert("Error","Cannot access socket.",null,null);
                alert.setTimeout(Alert.FOREVER);
                alert.setType(AlertType.ERROR);
                display.setCurrent(alert);      
          }
          catch( IOException error ){
                Alert alert = new Alert("Error", error.toString(), null, null);
                alert.setTimeout(Alert.FOREVER);
                alert.setType(AlertType.ERROR);
                display.setCurrent(alert);
          }
        }

    In the end am getting "java.io Exception " . My jpeg image is not being received in th server side.
    Can u please help me solving this issue.

    Thanks in Adance
    Deva

Similar Threads

  1. KERN-EXEC 3 or 0 with GPS sample code
    By neil.young in forum Symbian
    Replies: 12
    Last Post: 2009-10-14, 20:50
  2. sending from MIDlet, receiving in PHP on server
    By johnyjj2 in forum Mobile Java General
    Replies: 0
    Last Post: 2009-09-25, 18:32
  3. Server sending device information, system error
    By cursed in forum OMA DM/DS/CP
    Replies: 0
    Last Post: 2008-05-12, 11:28
  4. Problem with sending image
    By ckt in forum Symbian
    Replies: 5
    Last Post: 2007-01-03, 08:27
  5. Replies: 3
    Last Post: 2003-10-10, 09:41

Posting Permissions

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