×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    128

    How to know that a read operation had finished reading an integer?

    Hi,

    I am using a buffer to read in an integer from a server. I am using the CSocketRead class from the socket example. Is it recieving a new integer everytime it calls the RecvOneOrMore function? or it requires many operation to recieve 1 integer? Below is a segment of my code in my RunL function. I want it to read in a new integer each time and store it into my array which will be incremented each time it read in a new integer. However, it is not reading the integer the way I expected. It seems to be storing the same number in the array each time the RunL function runs.

    void CSocketsRead::RunL()
    {
    // Active object request complete handler
    if (iStatus == KErrNone)
    {
    // Character has been read from socket
    //iConsole.PrintNotify(iBuffer); // display data on console

    TInt num;
    num=-1;
    TLex8 lexRead(iBuffer);

    lexRead.Val(num);//convert to integer

    if (num==8)
    {
    iConsole.SetStatus(_L("Connected"),1);
    }
    else
    {
    if((num==0)||(num==1)||(num==2)||(num==3)||(num==4))
    {
    if (x==4)//increment of array
    {
    x=0;
    }
    if (y==8)
    {
    x=x+1;
    y=1;
    }
    else
    {
    y=y+1;
    }
    oppGrid[x][y]=num;//store integer into array

    }
    }

    IssueRead(); // Immediately start another read
    }
    else
    {
    // Error: pass it up to user interface
    iConsole.ErrorNotify(_L("\nCSocketsRead error"), iStatus.Int());
    }
    }


    void CSocketsRead::IssueRead()
    {
    // Initiate a new read from socket into iBuffer
    ASSERT(!IsActive());
    iSocket.RecvOneOrMore(iBuffer, 0, iStatus, iDummyLength);
    SetActive();
    }

    Is there any problem in my code?

    Thanks a million,
    zhen
    Last edited by zhenLeow; 2003-09-22 at 03:01.

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    14
    It might receive more than 1 integer with RecvOneOrMore. Do you know what exactly the server does?

    Also, are you sure the integer is being send as 'text'?

    I wonder what the number is that is being stored in the array. You should check the return value for lexRead.Val ( num );.

    Also, the integer might be sent binary which would be great, because than you should be able to read one (1) integer at a time with:

    TBuf8 < sizeof ( TInt ) > buffer;

    iSocket.Recv ( buffer, iStatus );

    and than a binary conversion in RunL...

    Something like:

    TInt32 DesToI ( const TDesC8 & des )
    {
    return ( ( static_cast < TInt32 > ( des [ 0 ] ) ) |
    ( static_cast < TInt32 > ( des [ 1 ] ) << 8 ) |
    ( static_cast < TInt32 > ( des [ 2 ] ) << 16 ) |
    ( static_cast < TInt32 > ( des [ 3 ] ) << 24 ) );
    }


    Hope this helps.
    ManiaC++
    Jan Knepper

  3. #3
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    128
    Hi,

    Thanks for ur reply. I found out that my integer is sent as integer not text. Does it affect my read operation? The number that can be store in my array is 0,1,2,3. I dun quite understand how ur binay conversation code works. Can explain?

    Thanks for ur help,

    zhen
    Last edited by zhenLeow; 2003-09-22 at 05:20.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    14
    Well, if the integer is being sent as an TInt you sound use the second part of first reply. You solution is going to be simple.

    class CSocketsRead ...
    {
    // ...
    private :
    TBuf8 < sizeof ( TInt ) > buffer;
    // ...
    };


    void CSocketsRead :: IssueRead ()
    {
    // Initiate a new read from socket into iBuffer
    ASSERT ( !IsActive () );

    iSocket.Recv ( buffer, iStatus, );

    SetActive ();
    }



    void CSocketsREad :: RunL ()
    {
    // ...

    // Construct the 'TInt' from 'buffer'. 4 separate bytes become a 32 bits integer. The indexes 0, 1, 2, 3 might have to be reversed depending on how the integer was stored.

    TInt num =
    ( static_cast < TInt > ( buffer [ 0 ] ) ) |
    ( static_cast < TInt > ( buffer [ 1 ] ) << 8 ) |
    ( static_cast < TInt > ( buffer [ 2 ] ) << 16 ) |
    ( static_cast < TInt > ( buffer [ 3 ] ) << 24 ) |

    // ...
    }

    Good luck!
    ManiaC++
    Jan Knepper

  5. #5
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    128
    Hi,

    Thanks, but i have some qns for the binary conversion. I still dun quite understand the codes. Why do we need to do the static_cast 4 times? and what is the 8,16,24 for? and how do you know u need to take from buffer of indexes 0,1,2,3?

    Sorry for asking so much... but hope u can explain...

    Thanks a million..

    zhen

  6. #6
    Registered User
    Join Date
    Jul 2005
    Location
    Bengaluru, India
    Posts
    747

    Re: How to know that a read operation had finished reading an integer?

    Can any body help me to convert an 8 bit descriptor to binary format? I want to convert the 8 bit descriptor to binary format and send through socket. Hope for an early reply.

    Thanks in advance.

Posting Permissions

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