×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Question Problem in socket communication between Symbian c++ and MIDLET

    hi, I have written a MIDLET as a socket client, and a Symbian C++ application as a socket listen server.
    The problem is: when MIDLET sent message to server, the server can read the data successfully; but the server can not write back the data to client.
    Here is the code:

    J2ME code:
    Code:
               conn = (StreamConnection)Connector.open(url);
                        
                out = conn.openOutputStream();
                
                byte[] buf = request.getBytes();
                
                out.write(buf, 0, buf.length);
                
                out.flush();
                out.close();            
                
                byte[] data = new byte[256];           
                in = conn.openInputStream();
                int actualLength = 0;
                int mark = 0;
                
                while(in.available() > 0 && (actualLength=in.read(data))!=(-1))
                {
                    String tmp = new String(data);
                    tmp1 = textbox.getString();
                    textbox.setString(tmp1 + tmp);
                   
                    System.arraycopy(data, 0, res, mark, actualLength);
                    mark += actualLength;
                }
                
                String tmp2 = textbox.getString();
                textbox.setString(tmp2 + "\n" + "get data finish \n");
                in.close();
    Symbian C++ server code:
    main code:
    Code:
        CSocketListenEngine* engine = CSocketListenEngine::NewL();
        engine->StartEngineL();
        CActiveScheduler::Start();
    CSocketListenEngine code:
    Code:
    void CSocketListenEngine::ConnectL()
        {
        TInt err;
        err=iListen.Open(iSession, KAfInet, KSockStream, KProtocolInetTcp);
        User::LeaveIfError(err);
        User::LeaveIfError(iSocket.Open(iSession));
        TInetAddr ServerAddrOnPort(ServerAddr, ServerPort);
        iListen.Bind(ServerAddrOnPort);
        }
    
    void CSocketListenEngine::StartEngineL()
        {
        iListen.Listen(1);
        iListen.Accept(iSocket, iStatus);
        SetActive();
        }
    
    void CSocketListenEngine::RunL()
        {
        if(iStatus==KErrNone)
            {
            TRequestStatus status;
            TSockXfrLength dummyLength;
            TBool running = ETrue;
            TBuf8<256> buffer;
                
            iSocket.RecvOneOrMore(buffer, 0, status, dummyLength);  // here it can get data successfully
            User::WaitForRequest(status);   
            if(status != KErrNone) User::Leave(KErrGeneral);
                   
            buffer.Copy(_L("123"));
            // respappid = 1234 pStatusCode = 0;
            status = KRequestPending;
            iSocket.Write(buffer, status); // problem: midlet can not get the data, but the status is none error.
            User::WaitForRequest(status);
            if(status != KErrNone) User::Leave(KErrGeneral);
            }
        }
    PS: CSocketListenEngine is derived from CActive

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,751

    Re: Problem in socket communication between Symbian c++ and MIDLET

    Java
    Note that your code simply ends if there is no data in the input stream. You could enforce to read at least one byte (similarly to RecvOneOrMore on the C++ side). If you are debugging the Symbian code, this effect surely appears.

    Symbian C++:
    You should never set a TRequestStatus to anything, certain methods check if it is KRequestPending already, and behave differently than expected.
    RSocket::Write may require LocalServices capability even in the case of a local socket, though I am not sure about that.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Re: Problem in socket communication between Symbian c++ and MIDLET

    wizard_hu_, thanks a lot for your comments!
    I have modified the midlet and navtive application in your suggestion, but midlet was in the wait loop and did not get anything.

    Sorry, I have not pointed the phenomenon at the top: if i put the code of read and write socket to the E32Main code without start CActiveScheduler, the code can work well.
    the E32Main code likes this:

    TInt E32Main()
    {
    ......

    TRequestStatus status;
    TSockXfrLength dummyLength;
    TBool running = ETrue;
    TBuf8<256> buffer;

    iSocket.RecvOneOrMore(buffer, 0, status, dummyLength); // here it can get data successfully
    User::WaitForRequest(status);
    if(status != KErrNone) User::Leave(KErrGeneral);

    buffer.Copy(_L("123"));
    // respappid = 1234 pStatusCode = 0;
    iSocket.Write(buffer, status); // work well
    User::WaitForRequest(status);
    if(status != KErrNone) User::Leave(KErrGeneral);

    ......
    }

    but if I put the code in a AO's runl function, the problem occurred.
    Last edited by hareluya_no_1; 2011-03-28 at 09:21. Reason: modify

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,751

    Re: Problem in socket communication between Symbian c++ and MIDLET

    The scheduler can not matter, since in both code variants you are using a local TRequestStatus, and the code runs in the same, synchronous way practically (User::WaitForRequest). Are you absolutely sure about correct and complete execution? Note that the error messages are not displayed by default, they have to be enabled on the device (and partially on the emulator too): http://wiki.forum.nokia.com/index.ph...ded_panic_code

    A bit of magical thinking: what happens if you move out.close() to the end of execution on the Java side? (I have not done Java ME networking in the last 6 years, so I do not remember if closing the stream appears as a network event or not - however if it gets propagated somehow, that may interfere with receiving).


    A Symbian C++ thing: consider using User::LeaveIfError, internally it is something like
    Code:
    TInt User::LeaveIfError(TInt aError)
    {
        if(aError<0)User::Leave(aError);
        return aError;
    }
    however it will not affect the actual issue

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Re: Problem in socket communication between Symbian c++ and MIDLET

    Thanks.

    I have solved it, I forgot to close the socket, the send data may be in the buffer.

Similar Threads

  1. silent communication between native S60 app and java midlet
    By dirk.koehler in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2010-01-25, 19:36
  2. problem in serial communication between pc and mobile
    By prakashvs in forum Bluetooth Technology
    Replies: 4
    Last Post: 2008-09-15, 08:10
  3. Communication between c++ and midlet
    By alexpr in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2007-07-06, 14:51
  4. problem with connecting socket between phone and midlet running on a PC
    By weiyou in forum Symbian Networking & Messaging (Closed)
    Replies: 19
    Last Post: 2007-02-14, 06:09
  5. Communication between MIDLet and C++
    By rahull in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2003-06-11, 02:48

Posting Permissions

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