×

Discussion Board

Results 1 to 2 of 2

Hybrid View

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    136

    Not able to receive message using socket

    Hi All,

    I have created a two separate active objects to read and write from same socket. One AO reads from socket and another writes to socket.

    I can successfully connect to server and send data to server but I fail to read data from server. Even server is sending, I am not receiving any data it seems it is just listening to stream and there is no data.... why??

    my code is below

    Reciever

    /*
    * ============================================================================
    * Name : CTcpCocketReceiver from TcpCocketReceiver.cpp
    * Part of : TcpCocket
    * Created : 06.03.2006 by ToBeReplacedByAuthor
    * Implementation notes:
    *
    * Version :
    * Copyright: ToBeReplacedByCopyright
    * ============================================================================
    */

    // INCLUDE FILES
    #include "TcpCocketReceiver.h"

    // ================= MEMBER FUNCTIONS =======================

    // constructor
    CTcpCocketReceiver::CTcpCocketReceiver(MObserver& aObserver, RSocket& aSocket)
    : CActive(CActive::EPriorityStandard), iObserver(aObserver), iSocket(aSocket),
    iBuf(const_cast<TUint8*>(iBuffer.Ptr()), 4)
    {
    CActiveScheduler::Add(this);
    }

    // destructor
    CTcpCocketReceiver::~CTcpCocketReceiver()
    {
    Cancel();
    }

    void CTcpCocketReceiver::Receive()
    {
    Cancel();
    // Receive the length of the message
    iBuf.Set(const_cast<TUint8*>(iBuffer.Ptr()), 0, 4);
    iSocket.Read(iBuf, iStatus);
    SetActive();
    }

    void CTcpCocketReceiver:oCancel()
    {
    iSocket.CancelRead();
    }

    void CTcpCocketReceiver::RunL()
    {
    TInt err = iStatus.Int();
    if(err==KErrNone)
    {
    if(iBuf.Size()==4)
    {
    TInt size = 0;
    TPckg<TInt> pkg(size);
    pkg.Copy(iBuf);
    // Receive the message
    iBuf.Set(const_cast<TUint8*>(iBuffer.Ptr()), 0, size);
    iSocket.Read(iBuf, iStatus);
    SetActive();
    }
    else
    {
    // Notify observer
    const TUint16* ptr = reinterpret_cast<const TUint16*>(iBuf.Ptr());
    TPtrC msg(ptr, iBuf.Length()/2);
    iObserver.OnReceiveL(msg, err);
    }
    }
    else
    {
    iObserver.OnReceiveL(KNullDesC(), err);
    }
    }

    // End of File



    Socket Engine

    /*
    * ============================================================================
    * Name : CTcpCocketEngine from TcpCocketEngine.cpp
    * Part of : TcpCocket
    * Created : 06.03.2006 by ToBeReplacedByAuthor
    * Implementation notes:
    *
    * Version :
    * Copyright: ToBeReplacedByCopyright
    * ============================================================================
    */

    // INCLUDE FILES
    #include <TcpSocketPC.pan> // for Panic();

    #include "TcpCocketEngine.h"

    const TInt KTimeout = 30; // 30 seconds time-out

    // ================= MEMBER FUNCTIONS =======================

    // Two-phased constructor.
    CTcpCocketEngine* CTcpCocketEngine::NewL(MTcpObserver& aObserver)
    {
    CTcpCocketEngine* self = new (ELeave) CTcpCocketEngine( aObserver );
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop();
    return self;
    }

    // destructor
    CTcpCocketEngine::~CTcpCocketEngine()
    {
    delete iTimer;
    delete iSender;
    delete iReceiver;
    Cancel();
    iSocket.Close();
    iListenSocket.Close();
    iSocketServ.Close();
    }

    // New functions
    CTcpCocketEngine::TState CTcpCocketEngine::State()
    {
    return iState;
    }

    void CTcpCocketEngine::ConnectL(const TDesC& aAddr, TUint aPort)
    {
    __ASSERT_DEBUG(iState==EStateIdle, Panic(ETcpCocketPanicFailToConnect));

    User::LeaveIfError(iAddr.Input(aAddr));
    iAddr.SetPort(aPort);

    // Open a TCP socket
    User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));

    // Initiate socket connection
    iSocket.Connect(iAddr, iStatus);
    SetActive();

    // Start a timeout timer
    iTimer->Start(KTimeout);

    iState = EStateConnecting;
    }

    void CTcpCocketEngine:isconnect()
    {
    __ASSERT_DEBUG(iState==EStateConnected, Panic(ETcpCocketPanicFailToDisconnect));

    // cancel all outstanding operations
    // since we are connected, the only possibilities are read and write
    iReceiver->Cancel();
    iSender->Cancel();
    iSocket.Close();

    iState = EStateIdle;
    }

    void CTcpCocketEngine::ListenL()
    {
    __ASSERT_DEBUG(iState==EStateIdle, Panic(ETcpCocketPanicFailToListen));

    TInetAddr addr(INET_ADDR(127,0,0,1), 8002);
    User::LeaveIfError(iListenSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
    User::LeaveIfError(iSocket.Open( iSocketServ ) );

    // Bind the socket to the required port.
    iListenSocket.Bind(addr);

    // Listen for incoming connections
    iListenSocket.Listen(1);

    // Accept incoming connections
    iListenSocket.Accept(iSocket, iStatus);
    SetActive();

    iState = EStateListening;
    }

    void CTcpCocketEngine::SendL(const TDesC& aMsg)
    {
    __ASSERT_DEBUG(iState==EStateConnected, Panic(ETcpCocketPanicFailToSend));

    iSender->Send(aMsg);
    }

    void CTcpCocketEngine::ReceiveL()
    {
    __ASSERT_DEBUG(iState==EStateConnected, Panic(ETcpCocketPanicFailToReceive));

    iReceiver->Receive();
    }

    // Functions from CActive
    void CTcpCocketEngine:oCancel()
    {
    switch(iState)
    {
    case EStateConnecting:
    {
    iTimer->Cancel();
    iSocket.CancelConnect();
    break;
    }
    case EStateListening:
    {
    iListenSocket.CancelAccept();
    break;
    }
    default:
    {
    // unreachable
    break;
    }
    }
    iState = EStateIdle;
    }

    void CTcpCocketEngine::RunL()
    {
    TInt err = iStatus.Int();
    switch(iState)
    {
    case EStateConnecting:
    {
    iTimer->Cancel();
    if (err == KErrNone)
    {
    // Connection completed successfully
    iState = EStateConnected;
    ReceiveL();
    }
    else
    {
    iSocket.Close();
    iState = EStateIdle;
    }
    iObserver.OnConnectL(err);
    break;
    }
    case EStateListening:
    {
    if(err == KErrNone)
    {
    iState = EStateConnected;
    ReceiveL();
    }
    iListenSocket.Close();
    iObserver.OnConnectL(err);
    break;
    }
    default:
    {
    // unreachable
    break;
    }
    }
    }

    // Functions from CTcpCocketTimer::MTimerObserver
    void CTcpCocketEngine::OnTimerL(TInt aError)
    {
    Cancel();
    TInt err = (aError==KErrNone)?KErrTimedOut:aError;
    iObserver.OnConnectL(err);
    }

    // Functions from CTcpCocketSender::MObserver
    void CTcpCocketEngine::OnSendL(const TDesC& aMsg, TInt aError)
    {
    iObserver.OnSendL(aMsg, aError);
    }

    // Functions from CTcpCocketReceiver::MObserver
    void CTcpCocketEngine::OnReceiveL(const TDesC& aData, TInt aError)
    {
    iObserver.OnReceiveL(aData, aError);
    if(aError==KErrNone)
    {
    ReceiveL();
    }
    else
    {
    Disconnect();
    }
    }

    // constructor
    CTcpCocketEngine::CTcpCocketEngine(MTcpObserver& aObserver)
    : CActive(CActive::EPriorityStandard), iObserver(aObserver)
    {
    CActiveScheduler::Add(this);
    }

    // EPOC default constructor can leave.
    void CTcpCocketEngine::ConstructL()
    {
    // Open a session to Socket Server
    User::LeaveIfError(iSocketServ.Connect());

    iTimer = CTcpCocketTimer::NewL(*this);

    iSender = CTcpCocketSender::NewL(*this, iSocket);
    iReceiver = new(ELeave) CTcpCocketReceiver(*this, iSocket);
    }




    not getting what is error please some one tell how to resolve it
    Regards,
    Cholker

  2. #2
    Registered User
    Join Date
    Jan 2005
    Location
    Italy
    Posts
    639

    Re: Not able to receive message using socket

    Hello,
    i admit i didn't read your code in detail, but i remember another thread discussing the same issue, here it is, if you haven't found it yet:
    http://www.developer.nokia.com/Commu...ng-from-a-port

    hope it helps somehow,
    regards
    pg

Similar Threads

  1. how to receive a big data by socket?
    By lmyuanhang in forum Symbian
    Replies: 6
    Last Post: 2010-05-27, 10:35
  2. sending message from j2se socket to j2me socket
    By gizyy in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2010-05-10, 07:14
  3. How to know Socket has read the full message form a server in SDK socket example?
    By Mohan Kumar in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2007-06-22, 16:26
  4. My UDP socket can't receive any data
    By djypanda in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2004-12-10, 09:01
  5. RE:about the socket example (cannot receive)
    By yanxiang in forum Symbian
    Replies: 0
    Last Post: 2003-07-09, 12:35

Posting Permissions

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