Discussion Board

Results 1 to 14 of 14
  1. #1
    Registered User lilyaaa's Avatar
    Join Date
    Sep 2009
    Posts
    36
    I am new to Symbian C++ and I try to write a tcpip client to send data.
    I also use java to write the server and run on the same computer.

    I find some code on the web write it inside one file and like that but the emulator close after runing a while,
    it seems fail in lSocket.Connect(lServSockAddr,status) and my Java Server doesn't get any client.
    Does anyone can tell me what's wrong? Thank you very much!

    This is all my code:

    #include <e32cons.h>
    #include <es_sock.h>
    #include <in_sock.h>

    // Function prototype
    LOCAL_C void UseBasicTypesL();

    //////////////////////////////////////////////////////////////////////////////
    //
    // Main function called by E32
    //
    //////////////////////////////////////////////////////////////////////////////
    GLDEF_C TInt E32Main()
    {

    // Catch any Leaves thrown
    TRAPD(error, UseBasicTypesL());

    _LIT(KMsgPanic,"Error in Symbian OS Basics Lab: ");
    __ASSERT_ALWAYS(!error, User::Panic(KMsgPanic, error));

    return 0;
    }


    //////////////////////////////////////////////////////////////////////////////

    LOCAL_C void UseBasicTypesL()
    {
    // Constant text used for the console app title
    _LIT(KLabTitle,"Testing");

    CConsoleBase* console = Console::NewL(KLabTitle, TSize(KConsFullScreen, KConsFullScreen));

    TRequestStatus status;
    RSocketServ lSession;
    RSocket lListenSocket, lSocket;

    TInt lErr;

    // Connect to the socket server
    lErr=lSession.Connect();
    User::LeaveIfError(lErr);

    lErr = lListenSocket.Open(lSession,KAfInet, KSockStream,KProtocolInetTcp);
    User::LeaveIfError(lErr);

    TInetAddr lServSockAddr;

    const TUint32 KInetAddr = INET_ADDR(127,0,0,1);

    lServSockAddr.SetAddress(KInetAddr);

    lServSockAddr.SetPort(5888);

    lSocket.Connect(lServSockAddr,status); // it seems fail at here when I use debug mode

    const TText8* txt = (TText8*)_S("My Tcpip client test");
    TBuf8<100> sbuf(txt);
    lSocket.Send(sbuf, 0, status));

    TBuf8<100> rbuf;
    TSockXfrLength aLen;
    lSocket.RecvOneOrMore(rbuf,0,status, aLen);
    console->Printf(_L("Received Data is:: "),rbuf);


    // Continue
    _LIT(KMsgPressAnyKey,"Press any key to end");
    console->Printf(KMsgPressAnyKey);
    console->Getch();

    delete console;
    }

  2. #2
    Nokia Developer Moderator wizard_hu_'s Avatar
    Join Date
    Feb 2006
    Location
    Mallorca, Holiday
    Posts
    27,683
    Communication over sockets is an asynchronous activity. It means that Connect, Send, RecvOneOrMore calls return immediately, and they will notify you about the result in the TRequestStatus variable, later.
    In such test code, you can put "User::WaitForRequest(status);" after each of these calls, then it will work.
    Printing the received data will not succeed, because it is in a 8-bit descriptor, while Printf works with Unicode data.
    Code:
    TBuf<100> rbuf16;
    rbuf16.Copy(rbuf);
    console->Printf(_L("Received Data is:: %S"),&rbuf16);
    does the conversion, and also print something.

  3. #3
    Registered User lilyaaa's Avatar
    Join Date
    Sep 2009
    Posts
    36
    Thanks for your reply , I try the way you say to modify the code, but the problem still the same: it close immediately when it
    goes to the connect part. I have read many documents and it seems it is a very simple code, I don't know why it has problem.
    My java server program is listen to port 1234 and it can accept other java tcp client in my computer but except this.
    Please Help Me.

    CConsoleBase* console = Console::NewL(KLabTitle, TSize(KConsFullScreen, KConsFullScreen));
    TRequestStatus status;
    RSocketServ lSession;
    RSocket lListenSocket, lSocket;

    TInt lErr;

    // Connect to the socket server
    lErr=lSession.Connect();
    User::LeaveIfError(lErr);

    lErr = lListenSocket.Open(lSession,KAfInet, KSockStream,KProtocolInetTcp);
    User::LeaveIfError(lErr);

    TInetAddr lServSockAddr;

    const TUint32 KInetAddr = INET_ADDR(192,168,10,211);
    lServSockAddr.SetAddress(KInetAddr);
    lServSockAddr.SetPort(1234);

    lSocket.Connect(lServSockAddr,status); // it close suddenly here
    User::WaitForRequest(status);

    const TText8* txt = (TText8*)_S("TCP Client Testing!");
    TBuf8<100> sbuf(txt);
    lSocket.Send(sbuf, 0, status);
    User::WaitForRequest(status);

    TBuf8<100> rbuf;
    TSockXfrLength aLen;
    lSocket.RecvOneOrMore(rbuf,0,status, aLen);
    User::WaitForRequest(status);

    TBuf<100> rbuf16;
    rbuf16.Copy(rbuf);
    console->Printf(_L("Received Data is:: %S"),&rbuf16);
    ..........

  4. #4
    Nokia Developer Expert symbianyucca's Avatar
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    28,667
    Check the panic code: http://wiki.forum.nokia.com/index.ph...ded_panic_code

    Also it would be a lot better to actually use active objects and not WaitForRequest.

  5. #5
    Nokia Developer Moderator wizard_hu_'s Avatar
    Join Date
    Feb 2006
    Location
    Mallorca, Holiday
    Posts
    27,683
    TRequestStatus stores result code of asynchronous activities.
    Whenever you wait for a given request, you could print its result after it has completed:
    Code:
    User::WaitForRequest(status);
    console->Printf(_L("stat: %d"),status.Int());
    console->Getch();
    0 is KErrNone - everything is fine
    -x is some error: http://wiki.forum.nokia.com/index.php/Error_codes
    The Getch is there for enabling you to read the code prior to the crash :-)

    Jukka: I think it is acceptable if beginners experiment with console-based "linear" code - I also do that rather often

  6. #6
    Nokia Developer Expert symbianyucca's Avatar
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    28,667
    Quote Originally Posted by wizard_hu_ View Post

    Jukka: I think it is acceptable if beginners experiment with console-based "linear" code - I also do that rather often
    Certainly it is ok for experiment, anyway, some people think that they can actually do that in release code and still have application that behaves and responds well, so just trying to get people to use something that actually work in the long run..

  7. #7
    Registered User lilyaaa's Avatar
    Join Date
    Sep 2009
    Posts
    36
    THank you for you reply. But the problem is still there. and I find out that my code hangs with KERN-EXEC 0 :trying to use a not created object in the line

    lSocket.Connect(lServSockAddr,status);

    Even I want to print out the status.

    lSocket.Connect(lServSockAddr,status);
    User::WaitForRequest(status);
    console->Printf(_L("stat: %d"),status.Int());
    console->Getch();

    Since it quit immediately, I cannot see any status and I think it doesnot execute User::WaitForRequest(status) and after codes.
    I found a post http://discussion.forum.nokia.com/fo...d.php?t=171997 and add CAPABILITY localservices but no use.

    and the lErr are 0 in:

    lErr=lSession.Connect();
    User::LeaveIfError(lErr);

    lErr = lListenSocket.Open(lSession,KAfInet, KSockStream,KProtocolInetTcp);
    User::LeaveIfError(lErr);

    SO I really don't know the problem is.

  8. #8
    Nokia Developer Moderator wizard_hu_'s Avatar
    Join Date
    Feb 2006
    Location
    Mallorca, Holiday
    Posts
    27,683
    Quote Originally Posted by lilyaaa View Post
    lSocket.Connect(lServSockAddr,status);

    ...

    lErr = lListenSocket.Open(lSession,KAfInet, KSockStream,KProtocolInetTcp);
    User::LeaveIfError(lErr);

    SO I really don't know the problem is.
    Well, I do: you have two sockets for some reason, initialize one of them, then attempting to use the other, that is why.
    The client needs one socket only, thus you could avoid such issues via simply commenting the declaration of lListenSocket for now.

  9. #9
    Registered User lilyaaa's Avatar
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by wizard_hu_ View Post
    Well, I do: you have two sockets for some reason, initialize one of them, then attempting to use the other, that is why.
    The client needs one socket only, thus you could avoid such issues via simply commenting the declaration of lListenSocket for now.
    Yes! You are right!!!!!! My server can recieve the connection now!!!!! Thank you very much!!!!!!!!

  10. #10
    Registered User lilyaaa's Avatar
    Join Date
    Sep 2009
    Posts
    36
    Now my server can get the connection and my client can receive the data from server , but cannot reverse.

    my Send is like this and the status is 0 and it seems send successfully:

    const TText8* txt = (TText8*)_S("TCP Client Testing!");
    TBuf8<100> sbuf(txt);
    lSocket.Send(sbuf, 0, status);

    and in my java code, I use

    BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    String message = br.readLine(); // waits for input now
    System.out.println("The message is " + message);

    but its seem no message can be read and wait at the line with readline.
    Does anybody know how to read the data from symbian c++ in java?

  11. #11
    Nokia Developer Moderator wizard_hu_'s Avatar
    Join Date
    Feb 2006
    Location
    Mallorca, Holiday
    Posts
    27,683
    Note that _S creates a 16-bit (Unicode) string of your text, which means that its second byte of your message is going to be 0. It may or may not affect BufferedReader - but as far as I remember it expects normal ASCII stream by default.
    What happens if you send _LIT8(KMessage,"TCP Client Testing!") or even an in-place _L8("TCP Client Testing!")?

  12. #12
    Registered User lilyaaa's Avatar
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by wizard_hu_ View Post
    Note that _S creates a 16-bit (Unicode) string of your text, which means that its second byte of your message is going to be 0. It may or may not affect BufferedReader - but as far as I remember it expects normal ASCII stream by default.
    What happens if you send _LIT8(KMessage,"TCP Client Testing!") or even an in-place _L8("TCP Client Testing!")?
    THanks for you reply, I try

    _LIT8(KMessage,"TCP Client Testing!");
    lSocket.Send(KMessage, 0, status);
    and

    lSocket.Send(_L8("TCP Client Testing!"), 0, status);
    and

    lSocket.Write(_L8("I am Client "),status);
    and
    _LIT8(KMessage,"TCP Client Testing!");
    lSocket.Write(KMessage,status);

    status return is 0 but still cannot be received in java Server. Server keep waiting for reading.

  13. #13
    Nokia Developer Moderator wizard_hu_'s Avatar
    Join Date
    Feb 2006
    Location
    Mallorca, Holiday
    Posts
    27,683
    Note that you do not send a line break, which probably cause readLine to wait until the socket gets closed.
    You can also try if direct usage of InputStream makes a difference, in this case assumptions of Reader/BufferedReader would not matter since you get the raw bytes.

  14. #14
    Registered User lilyaaa's Avatar
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by wizard_hu_ View Post
    Note that you do not send a line break, which probably cause readLine to wait until the socket gets closed.
    You can also try if direct usage of InputStream makes a difference, in this case assumptions of Reader/BufferedReader would not matter since you get the raw bytes.
    Yes, you are right! I add \n at the end of message and it's work.
    Thank you very much!

Similar Threads

  1. Fail to run Midlet: Connect to Agent - Connection refused
    By d-safety in forum Mobile Java Tools & SDKs
    Replies: 7
    Last Post: 2010-04-12, 17:23
  2. Replies: 7
    Last Post: 2009-03-21, 18:54
  3. Irobex connect fail
    By vchanonly in forum Symbian C++
    Replies: 11
    Last Post: 2005-03-23, 13:44
  4. fail to connect to database
    By ahmednagaty in forum Mobile Web Site Development
    Replies: 0
    Last Post: 2003-05-25, 19:29
  5. toolit and 6310i fail to connect securely to WTLS gateway.
    By mikt in forum Mobile Web Site Development
    Replies: 1
    Last Post: 2002-09-23, 10:38

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved