×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Aug 2006
    Location
    Cairo, Egypt
    Posts
    67

    FileInputStream problem

    Hi,
    im a newbie in the J2ME programming world.
    im developing a client/server application for file transfer using obex between mobile and PC.

    the thing is im trying to use in the J2ME application the fileinputstream()
    which should be in the calss java.io but i cant find it.

    is there a way i can use it?? like some sort of library or something?
    its really important as its my graduation project and im in shortage in time.
    Thanks a lot.

  2. #2
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: FileInputStream problem

    Hi,
    There is no such class as you mentioned.plz tell what ur requirement is.

    thanks
    Soku

  3. #3
    Regular Contributor
    Join Date
    Aug 2006
    Location
    Rawalpindi, Pakistan
    Posts
    486

    Re: FileInputStream problem

    there is input stream and byte array input stream classes available and similarly there corresponding output streams. u check both of them out. IF ur trying to send a file using Obex transfer via bluetooth then these streams will be help u out.
    Regards
    Nouman

  4. #4
    Regular Contributor
    Join Date
    Aug 2006
    Location
    Cairo, Egypt
    Posts
    67

    Re: FileInputStream problem

    Hi,thanks a lot for ur replys
    i already tried another way by the inputstream() and outputstream().
    im supposed to be recieving the data on the PC as a byte[]
    but im trying to use the "btgoep" url connection on the PC(using j2se) which doesnt support OBEX file exchange and so i used the AVETANA library but it doesnt seem to be working,,,it gives me an the following exception:

    javax.microedition.io.ConnectionNotFoundException
    at javax.microedition.io.Connector.open(Unknown Source)
    at server2.Main.connecting(Main.java:135)
    at server2.Main.run(Main.java:127)
    at java.lang.Thread.run(Thread.java:619)
    Exception in thread "Thread-2" java.lang.NullPointerException
    at server2.Main.connecting(Main.java:148)
    at server2.Main.run(Main.java:127)
    at java.lang.Thread.run(Thread.java:619)

    does anybody know how can i fix it?

    this is the server code on the PC

    public void run(){
    try {
    UUID uuid = new UUID(0x1105);
    device = LocalDevice.getLocalDevice(); device.setDiscoverable(DiscoveryAgent.GIAC);
    }catch (Exception e)
    { System.err.println("Cant init set discvover");
    e.printStackTrace();
    }

    String url= "btgoep://localhost:"+uuid+
    ";name=BTTP;authenticate=false;master=false;encrypt=false";
    connecting(url);

    }

    public void connecting(String url){
    try{

    // obtain connection and stream to this service
    btServerNotifier = (StreamConnectionNotifier) Connector.open(url);
    }
    catch ( Exception e) {
    e.printStackTrace();
    }


    while (mServerState )
    {
    StreamConnection btConn=null;
    try {
    updateStatus("[server:] Now waiting for a client to connect");

    btConn = btServerNotifier.acceptAndOpen();
    updateStatus("[server:] accept and open");
    } catch (IOException ioe) {
    updateStatus( ioe.getMessage());
    }
    //if (btConn != null)
    updateStatus("[server:] processing connection");
    processConnection(btConn);
    }

    }


    void processConnection(StreamConnection conn) {
    updateStatus("[server:] A client is now connected");
    byte[] file = new byte[10000];
    try {
    InputStream in = conn.openInputStream();
    in.read(file);
    in.close();


    Thread.sleep(1000);

    } catch (Exception e)
    {
    e.printStackTrace();

    }
    try {
    conn.close();
    updateStatus("[server:] Finished connection");

    }catch (Exception e ){ }


    }
    thanks again for all your help.

  5. #5
    Regular Contributor
    Join Date
    Aug 2006
    Location
    Rawalpindi, Pakistan
    Posts
    486

    Re: FileInputStream problem

    i am not clear. r u gettings this error on PC side or on mobile side?
    Regards
    Nouman

  6. #6
    Regular Contributor
    Join Date
    Aug 2006
    Location
    Cairo, Egypt
    Posts
    67

    Re: FileInputStream problem

    Im getting it on the server side, i have a problem with the OBEX connector.
    i updated my code so now it succeeds in opening the connection but it tells on the client side that it failed to send the file.

    the client side(on the mobile) sends file to other mobile devices successfully so thats why i know the problem is on the server side(on the PC).

    i've tried everything i can and im getting really frustrated...

    this is my server code
    PHP Code:
    /*
     * Main.java
     *
     * Created on June 10, 2007, 2:06 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */

    package server3;

    import de.avetana.bluetooth.obex.OBEXConnection;
    import de.avetana.javax.obex.ClientSession;
    import de.avetana.javax.obex.HeaderSet;
    import de.avetana.javax.obex.Operation;
    import de.avetana.javax.obex.SessionNotifier;
    import de.avetana.javax.obex.ServerRequestHandler;
    import de.avetana.obexsolo.OBEXConnector;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.JLabel;
    import javax.microedition.io.*;
    import java.io.*;
    import javax.bluetooth.*;

    /**
     *
     * @author BoBo
     */
    public class Main  implements ActionListener Runnable {
            
    JLabel spacerlabel = new JLabel(" ");
        
    JButton startButton = new JButton("Start Server");
        
    JTextArea textarea = new JTextArea("",2040);
           
    JButton endButton = new JButton("End Server");
    Connection connection null;
    SessionNotifier conn1;
           
        private 
    LocalDevice device;
      
        
    ClientSession cs;
    //    private SessionNotifier connection;
       
    private ServerRequestHandler serverRequest;
       private 
    boolean listening true;
       private 
    boolean finished true;
    String serverURL;
    SessionNotifier conn null;
    HeaderSet hs ;
    Thread serv null;
    File file;
    server3.OBEXConnector obex = new server3.OBEXConnector();

        
              
        
    /** Creates a new instance of Main */
        
    public Main() {
            
    JFrame.setDefaultLookAndFeelDecorated(true);    

            
    JFrame frame = new JFrame("FileServer ");
            
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            
    JScrollPane scrollPane = new JScrollPane(textarea);            
            
    textarea.setEditable(false);
        
            
    Container cp frame.getContentPane();
            
    cp.setLayout(new BoxLayout(cpBoxLayout.Y_AXIS));

            
    startButton.setAlignmentX(Component.CENTER_ALIGNMENT);
            
    startButton.addActionListener(this);
                   
    cp.add(startButton);        

            
    endButton.setAlignmentX(Component.CENTER_ALIGNMENT);
            
    endButton.addActionListener(this);
                   
    cp.add(endButton);        
                   
                   
            
    spacerlabel.setAlignmentX(Component.CENTER_ALIGNMENT);
            
    cp.add(spacerlabel);
            
            
    scrollPane.setAlignmentX(Component.CENTER_ALIGNMENT);
            
    cp.add(scrollPane);

            
    frame.pack();
            
    frame.setVisible(true);
    //                serverRequest = new ServerRequestHandler() ;

            
    updateStatus("[server:] FileServer Application started");
            
    updateStatus("[server:] Press the \"Start Server\" button to await for client devices");

     
        }
            public 
    void updateStatus(String message){
            
    textarea.append("\n" message);

        }

        
        
    /**
         * @param args the command line arguments
         */
        
    public static void main(String[] args) {
            
    // TODO code application logic here
            
    new Main();
        }

        public 
    void actionPerformed(ActionEvent e) {
             if ((
    e.getActionCommand()).equals("Start Server")    ) {
                       
    startButton.setEnabled(false);
                                        
    startServer();
                   }
        
                   if ((
    e.getActionCommand()).equals("End Server")    ) {
                       
    endButton.setEnabled(false);
                       
    startButton.setEnabled(true);
                       
    endServer();
               }
        }

    public 
    void connect()
    {
         
          

    }
        public 
    void run() {
             try{
             
          
    LocalDevice localDevice LocalDevice.getLocalDevice();
          
    localDevice.setDiscoverableDiscoveryAgent.GIAC ); 
          }
               catch(
    Exception ep){
             
    ep.printStackTrace();
          }   
          
                 
    UUID uuid = new UUID(0x1105);
      
    serverURL "btgoep://localhost:"+uuid
          
    ";name=HTBP;authenticate=false;master=false;encrypt=false";
     
          try {
              
          
    conn = (SessionNotifierobex.open(serverURL);
          
          
    updateStatus("Now waiting for a client to connect");
                   
          } catch (
    Exception e) {
             
    updateStatus("Could not listen for url");
             
    //System.exit(1);
          
    }
         try{
               
    Thread.sleep(1000);
              
    conn.acceptAndOpen(serverRequest);
                   
    updateStatus("[Sever]: Client Connected");
          }catch(
    Exception e){
             
    e.printStackTrace();
          }   
             
    ProcessConection();          
          

    }
          private 
    void startServer() {
               if (
    serv !=null)
                   return;
               
    //start the server and receiver
               
    serv = new Thread(this);
              
    // connect();
               
    serv.start();            
           }
           
           private 
    void endServer() {
               if (
    serv== null)
                   return;
               try {
                   
    serv.join();
               } catch (
    Exception ex) {};
               
    serv null;
                                  
           }
           public 
    void ProcessConection()
           {
                    try 
             { 
                
    Operation op null;
                
    //serverRequest.onGet(op);
             
    java.io.InputStream is op.openInputStream(); 
              
             
    String name op.getReceivedHeaders().getHeader(HeaderSet.NAME).toString(); 
              
             
    updateStatus("Bytes: " 
                                
    is.available() + 
                                
    ", name: " 
                                
    name 
                                
    ", type: " 
                                
    op.getType()); 
              
                   
    File f = new File(name); 
              
             
    FileOutputStream fos = new FileOutputStream(f); 
              
             
    byte b[] = new byte[is.available()]; 
              
             
    int len
              
             while(
    is.available() > && (len is.read(b)) > 0
             { 
                
    fos.write(b0len); 
             } 
              
             
    fos.close(); 
             
    updateStatus("Local file: " f.getAbsolutePath()); 
             
    op.sendHeaders(null); 
          } 
          catch(
    Exception e
          { 
             
    updateStatus(e.getMessage());
          } 
           


                    
           }
         


    Below is the part where my client code pushes the file
    PHP Code:
     class FilePusher implements CancellableTask{
            
            
    // this is used for the purposes of the Cancellable task
            
    boolean isOperationFailed false;
            
            
    // this is the failure message used by the Cancellable task
            
    String failure_message null;
            
            
    // this is the connection object to be used for
            //  bluetooth i/o
            
    Connection connection null;
            
            public 
    FilePusher(){
                
            }
            
            
    // this is method 1 of 4 methods needed to be implemented by
            // the CancellableTask interface
            
    public boolean cancel(){
                
    // sorry this can't be cancelled
                
    return false;
            }
            
            
    // this is method 2 of 4 needed to be implemented by
            // the CancellableTask interface
            
    public String getFailureMessage(){
                
    // of course, if there's no problem
                // then this method should return null as specified by the javadoc
                
    return failure_message;
            }
            
            
    // this is method 3 of 4 needed to be implemented by
            // the CancellableTask interface
            
    public boolean hasFailed(){
                return 
    isOperationFailed;
            }
     
            
    // this is method 4 of 4 needed to be implemented by
            // the CancellableTask interface       
            
    public void run(){
                
                try{
                    
    connection Connector.open(btConnectionURL);
                    
    // connection obtained
                    
                    // now, let's create a session and a headerset objects
                    
    ClientSession cs = (ClientSession)connection;
                    
    HeaderSet hs cs.createHeaderSet();
                    
                    
    // now let's send the connect operation
                    
    cs.connect(hs);
                    
                    
    hs.setHeader(HeaderSet.NAMEfilename);
                    
    hs.setHeader(HeaderSet.TYPE"image/jpeg"); 
                    
    hs.setHeader(HeaderSet.LENGTH, new Long(file.length));
                    
                    
    Operation putOperation cs.put(hs);
                    
                    
    OutputStream outputStream putOperation.openOutputStream();
                    
    outputStream.write(file);
                    
    // file push complete
                    
    Thread.sleep(1000);
                    
    outputStream.close();
                    
    putOperation.close();
                    
                    
    cs.disconnect(null);
                    
                    
    connection.close();
                } catch (
    Exception e){
                    
    isOperationFailed true;
                    
    failure_message e.toString();
                }
                
                
            } 

    Thats all...plz if anybody can help me i will be really grateful and thanks a lot for ur concern

Similar Threads

  1. Problem with eglSwapBuffers and heap corruption
    By greatape in forum Symbian Media (Closed)
    Replies: 2
    Last Post: 2007-05-24, 03:35
  2. netcards - Problem with opening adapter
    By kernj in forum Symbian Tools & SDKs
    Replies: 5
    Last Post: 2007-01-10, 08:56
  3. WAP service problem on Apache
    By bigg_o in forum Browsing and Mark-ups
    Replies: 3
    Last Post: 2005-04-17, 16:05
  4. Problem: S60 SDK for CW in PC with .NET
    By anttij in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2005-02-28, 11:36

Posting Permissions

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