×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    22

    UDP Socket: Problem with SendTo

    huy

    udp sockets working fine for receiving messages but when i send a message using sendto, it first gives iStatus = KErrNone & then my program terminated abnormaly, following is my code,

    UDPEngine.h

    /* Copyright (c) 2001, Nokia Mobile Phones. All rights reserved */

    #ifndef __UDPENGINE_H__
    #define __UDPENGINE_H__

    #include <in_sock.h>
    #include "Sockets.hrh"

    class MUINotifier;

    class CUDPEngine : public CActive
    {
    public:

    static CUDPEngine* NewL(MUINotifier* aConsole);

    static CUDPEngine* NewLC(MUINotifier* aConsole);

    ~CUDPEngine();

    void BindL();

    void Disconnect();

    void WriteL(const TDesC8& aData);

    void Read();

    void SetPort(TInt aPort);

    void SetRemoteAddress(TUint32 aAddress,TInt aPort);

    TInt Port() const;

    void SetConsole(MUINotifier* aConsole);

    protected: // from CActive

    void DoCancel();

    void RunL();

    private: // New methods

    CUDPEngine(MUINotifier* aConsole);

    void ConstructL();

    private: // Member variables

    MUINotifier* iConsole;

    TBool iFlag;

    RSocket iSocket;

    RSocketServ iSocketServ;

    TInetAddr iAddress;

    TInetAddr iRemoteAddress;

    TInt iPort;

    TBuf8<100> iBuffer;
    };

    #endif // __UDPENGINE_H__



    UDPEngine.cpp


    /* Copyright (c) 2001, Nokia Mobile Phones. All rights reserved */

    #include <eikgted.h>
    #include "UDPEngine.h"
    #include "Sockets.pan"
    #include "UINotifier.h"
    #include "MyCodes.h"
    #include "GlobalData.h"

    CUDPEngine* CUDPEngine::NewL(MUINotifier* aConsole)
    {
    CUDPEngine* self = CUDPEngine::NewLC(aConsole);
    CleanupStack::Pop(self);
    return self;
    }

    CUDPEngine* CUDPEngine::NewLC(MUINotifier* aConsole)
    {
    CUDPEngine* self = new (ELeave) CUDPEngine(aConsole);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

    CUDPEngine::CUDPEngine(MUINotifier* aConsole): CActive(EPriorityStandard)
    {
    iConsole = aConsole;
    }

    CUDPEngine::~CUDPEngine()
    {
    Cancel();
    iSocketServ.Close();
    }


    void CUDPEngine::ConstructL()
    {
    CActiveScheduler::Add(this); // add active object
    iFlag = EFalse;
    User::LeaveIfError(iSocketServ.Connect()); // Open channel to Socket Server
    User::LeaveIfError( iSocket.Open( iSocketServ, KAfInet, KSockDatagram, KProtocolInetUdp));
    }

    void CUDPEngine::BindL()
    {
    iFlag = ETrue;
    iAddress.SetAddress(KInetAddrAny);
    iAddress.SetPort(iPort);
    User::LeaveIfError( iSocket.Bind(iAddress));
    Read();
    }

    void CUDPEngine:isconnect()
    {
    iSocket.CancelRecv();
    iSocket.Close();
    }

    // from CActive
    void CUDPEngine:oCancel()
    {
    iSocket.CancelRecv();
    iSocket.Close();
    }

    void CUDPEngine::WriteL(const TDesC8& aData)
    {
    iSocket.SendTo(aData,iRemoteAddress,0,iStatus);
    }

    void CUDPEngine::Read()
    {
    if(!IsActive())
    {
    iSocket.RecvFrom(iBuffer,iRemoteAddress,0,iStatus);
    SetActive();
    }
    }

    // from CActive
    void CUDPEngine::RunL()
    {
    if (iStatus == KErrNone)
    {
    if(iFlag)
    {
    iConsole->OnUDPDataReceived(iBuffer);
    Read(); //Start Next Read
    }
    }
    else
    {
    iSocket.Close();
    iConsole->OnErrorReceived(_L("Error Received."), iStatus.Int());
    }
    }

    void CUDPEngine::SetPort(TInt aPort)
    {
    iPort = aPort;
    }

    TInt CUDPEngine::Port() const
    {
    return iPort;
    }

    void CUDPEngine::SetConsole(MUINotifier* aConsole)
    {
    iConsole = aConsole;
    }

    void CUDPEngine::SetRemoteAddress(TUint32 aAddress,TInt aPort)
    {
    iRemoteAddress.SetPort(aPort);
    iRemoteAddress.SetAddress(aAddress);
    }

  2. #2
    Registered User
    Join Date
    Apr 2005
    Location
    Barcelona
    Posts
    1,678

    Re: UDP Socket: Problem with SendTo

    The WriteL() function is missing a SetActive()
    You should be aware that the active object can have a pending request at a time, so if you need to perform simultaneous read & write, you'll have to wrap it in two active objects. The sdk v2.x has a good socket example doing this (though it uses tcp)

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    22

    Re: UDP Socket: Problem with SendTo

    Huy

    thanks PushL. my problem has been solved with SetActive()...

    regards

Similar Threads

  1. Server Socket Creation Problem
    By emailatravi in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2006-12-21, 12:26
  2. Problem: running socket example with mRouter in emulator
    By deepbeat in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2006-04-26, 15:56
  3. problem in UDP connection on emulator
    By isymdev in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2004-12-15, 11:06
  4. Problem with socket connection
    By prakashsainani in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2003-09-10, 15:33
  5. TCP/IP socket connection problem
    By ArunP in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2002-09-19, 10:09

Posting Permissions

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