×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    6

    Infrared socket connection between symbian device and windows xp

    symbian Server program as follows:
    /////////////////////////////////////////////////////
    /////////header file///////////////////////////////
    #ifndef _IRDASERVER_H_
    #define _IRDASERVER_H_

    #include <e32base.h>
    #include <es_sock.h>

    class CIrDAServer : public CActive
    {
    public:
    CIrDAServer();
    void ConstructL();
    ~CIrDAServer();

    void StartServL();
    void AcceptConnectionL();
    void Send(const TDesC& aMessage);
    void SendL(const TDesC& aMessage);
    void RequestData();
    protected:
    void RunL();
    void DoCancel();

    private:

    RSocketServ iSocketServer;
    RSocket iListeningSocket;
    RSocket iAcceptedSocket;
    TSockXfrLength iLen;

    TBuf8<32> iMessage;
    HBufC* iSendMessage;

    enum TState
    {
    EDisconnected,
    EConnecting,
    EConnected,
    EWaitingForMessage,
    ESendData
    };
    TState iState;
    };

    #endif
    ///////////////////////////////////////////////////////////////
    //////////.cpp file////////////////////////////////////////////
    #include "IrDAServer.h"

    #include <ir_sock.h>
    #include <s32mem.h>
    #include <s32strm.h>

    #include <aknnotewrappers.h>

    _LIT(KServerTransportName, "RFCOMM");

    _LIT(KTxtIrDATinyTP,"IrTinyTP");

    CIrDAServer::CIrDAServer()
    : CActive(CActive::EPriorityStandard)
    , iLen(32)
    , iState(EDisconnected)
    {
    }

    void CIrDAServer::ConstructL()
    {
    // initialize iSendMessage
    iSendMessage = HBufC::NewL(32);
    TPtr message = iSendMessage->Des();
    message.Copy(KServerTransportName);

    CActiveScheduler::Add(this);
    }

    CIrDAServer::~CIrDAServer()
    {
    Cancel();
    delete iSendMessage;
    iSendMessage = NULL;
    }

    void CIrDAServer::StartServL()
    {
    if(iState != EDisconnected)
    {
    User::Leave(KErrInUse);
    }
    User::LeaveIfError(iSocketServer.Connect());

    TProtocolName protoName(KTxtIrDATinyTP);
    TProtocolDesc proto;
    TInt err = iSocketServer.FindProtocol(protoName, proto);
    if(err)
    {
    User::Leave(err);
    }

    User::LeaveIfError(iListeningSocket.Open(iSocketServer, proto.iAddrFamily, proto.iSockType, proto.iProtocol));

    TIrdaSockAddr listeningAddress;
    int port = 4;
    listeningAddress.SetPort(port);

    User::LeaveIfError(iListeningSocket.Bind(listeningAddress));

    RNetDatabase aNetdb;
    err = aNetdb.Open(iSocketServer, proto.iAddrFamily, proto.iProtocol);
    if(err)
    {
    User::Leave(err);
    }

    TIASDatabaseEntry entry;
    entry.SetClassName(_L8("MY SERVICE"));
    entry.SetAttributeName(_L8("IrDA:TinyTP:LsapSel"));
    entry.SetToInteger(port);
    aNetdb.Add(entry);
    aNetdb.Close();

    User::LeaveIfError(iListeningSocket.Listen(2));

    }

    void CIrDAServer::AcceptConnectionL()
    {
    iAcceptedSocket.Close();
    User::LeaveIfError(iAcceptedSocket.Open(iSocketServer));
    iState = EConnecting;
    iListeningSocket.Accept(iAcceptedSocket, iStatus);
    SetActive();
    }

    void CIrDAServer::Send(const TDesC &aMessage)
    {
    TRAPD(err, SendL(aMessage));
    }

    void CIrDAServer::SendL(const TDesC &aMessage)
    {
    iMessage.Zero();
    TDesBuf buffer;
    buffer.Set(iMessage);
    RWriteStream stream(&buffer);
    CleanupClosePushL(stream);
    stream<<aMessage;
    CleanupStack::PopAndDestroy();
    iState = ESendData;
    iAcceptedSocket.Send(iMessage, 0, iStatus);
    SetActive();
    }

    void CIrDAServer::RequestData()
    {
    iMessage.Zero();
    iState = EWaitingForMessage;
    iAcceptedSocket.RecvOneOrMore(iMessage, 0, iStatus, iLen);
    SetActive();
    }

    void CIrDAServer::RunL()
    {
    if(iStatus == KErrNone)
    {
    if(iState == EConnecting)
    {
    RequestData();
    }
    if(iState == ESendData)
    {
    // just for inform me
    CAknInformationNote* note = new (ELeave) CAknInformationNote;
    note->ExecuteLD(iSendMessage->Des());
    }
    if(iState == EWaitingForMessage)
    {
    // just for inform
    CAknInformationNote* note = new (ELeave) CAknInformationNote;
    iMessage.Copy(_L8("my service"));
    TPtr elem = iSendMessage->Des();
    elem.Copy(iMessage);
    note->ExecuteLD(iSendMessage->Des());
    }
    }
    }

    void CIrDAServer:oCancel()
    {
    switch(iState)
    {
    case EConnecting:
    {
    iListeningSocket.CancelAccept();
    break;
    }
    case EWaitingForMessage:
    {
    iAcceptedSocket.CancelRecv();
    break;
    }
    case ESendData:
    {
    iAcceptedSocket.CancelSend();
    break;
    }
    default:
    {
    break;
    }
    }
    }
    ////////////////////////////////////////////////////////////

    the windows application running on PC as follows;
    /////////////////////////////////////////////////////////
    // IrDaClient.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"

    #include <Winsock2.h>
    #include <Af_irda.h>

    #include <iostream>
    using namespace std;

    #define INVALID_DADDR ((DWORD)-1)
    #define MAX_DEVICES 20
    #define INFO_LEN 22
    #define DISCOVER_SLEEP 400
    #define DISCOVER_TIMEOUT 60000

    static DWORD irda_discover_device(SOCKET fd)
    {
    DEVICELIST* list;
    IRDA_DEVICE_INFO *dev;
    unsigned char *buf;
    int s;
    int len;
    int i;
    DWORD daddr = INVALID_DADDR;
    DWORD t1,t2;

    len = sizeof(*list) + sizeof(*dev) * 20;
    buf = (unsigned char*)malloc(len);
    list = (DEVICELIST*)buf;
    dev = list->Device;
    t1 = timeGetTime();

    cout<<"Expecting Nokia 6260...\n";
    do
    {
    s = len;
    memset(buf, 0, s);
    if (getsockopt(fd, SOL_IRLMP, IRLMP_ENUMDEVICES, (char*)buf, &s) != SOCKET_ERROR)
    {
    for (i=0; (i<list->numDevice)&&(daddr==INVALID_DADDR); i++)
    {
    if (strncmp(dev[i].irdaDeviceName, "Nokia 6260", INFO_LEN) == 0)
    {
    daddr = *(DWORD*)dev[i].irdaDeviceID;
    cout<<"Matching: "<<dev[i].irdaDeviceName<<" "<<(*(DWORD*)dev[i].irdaDeviceID)<<endl;
    }
    else
    {
    cout<<"Not matching: "<<dev[i].irdaDeviceName<<" "<<(*(DWORD*)dev[i].irdaDeviceID)<<endl;
    }
    }
    }
    if (daddr == INVALID_DADDR)
    {
    Sleep(DISCOVER_SLEEP);
    }
    t2 = timeGetTime();
    }while ((t2 - t1 < DISCOVER_TIMEOUT) && daddr == INVALID_DADDR);

    free(buf);
    return daddr;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    WSADATA wsaData;
    SOCKADDR_IRDA peer;
    SOCKET fd = INVALID_SOCKET;
    DWORD daddr = INVALID_DADDR;
    int x = 1;
    /* Initialize */
    if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
    cout<<"WSAStartup() failed.\n";
    cout<<"Failed to initialize socket subsystem: need WINSOCK2. Please upgrade.\n";
    return -1;
    }

    if((fd = socket(AF_IRDA, SOCK_STREAM, 0)) == INVALID_SOCKET)
    {
    cout<<"Failed to create irda socket!\n";
    }

    daddr = irda_discover_device(fd);
    if (daddr == INVALID_DADDR)
    {
    cout<<"Failed to discover any irda device!"<<endl;
    closesocket(fd);
    return -1;
    }

    peer.irdaAddressFamily = AF_IRDA;
    *(DWORD*)peer.irdaDeviceID = daddr;
    // strcpy(peer.irdaServiceName, "IrDA:IrCOMM");
    strcpy(peer.irdaServiceName, "MY SERVICE");
    if (setsockopt(fd, SOL_IRLMP, IRLMP_9WIRE_MODE, (char*)&x, sizeof(x)) == SOCKET_ERROR)
    {
    cout<<"setsockopt Failed to set irda socket options.\n";
    closesocket(fd);
    return -1;
    }

    if(connect(fd, (struct sockaddr*)&peer, sizeof(peer)))
    {
    cout<<"connect Failed to connect to irda socket.\n";
    closesocket(fd);
    return -1;
    }


    Sleep(2000);
    int ret = send(fd, "ddddd", 10, 0);
    cout<<ret<<endl;
    /*
    char buf[16];
    memset(buf, 0, 16);
    int rcvlen = recv(fd, buf, sizeof(buf), 0);
    if(recv < 0)
    {
    int err = GetLastError();
    cout<<"error: "<<err<<endl;
    }
    cout<<"rcvlen: "<<rcvlen<<endl;
    buf[rcvlen] = '\0';
    cout<<buf<<endl;*/
    closesocket(fd);
    return 0;
    }

    /////////////////////////////////////////////////////////////////
    the pc is a client connecting to symbian device, the problem is that when connect is complete, then transfer data, the pc send data to symbian device, the symbian device iAcceptedSocket.RecvOneOrMore(iMessage, 0, iStatus, iLen) return, then the RunL function execute, but the iMessage is null, it doesn't contain any data, expecting your answer, thank you in advance!

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    6

    Re: Infrared socket connection between symbian device and windows xp

    can anyone help me? I'm eager for it

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    2

    Re: Infrared socket connection between symbian device and windows xp

    Hi.

    In SendL, you might want to set aMessage to buffer instead of iMessage?

    void CIrDAServer::SendL(const TDesC &aMessage)
    {
    iMessage.Zero();
    TDesBuf buffer;
    buffer.Set(aMessage);
    RWriteStream stream(&buffer);
    CleanupClosePushL(stream);
    stream<<aMessage;
    CleanupStack::PopAndDestroy();
    iState = ESendData;
    iAcceptedSocket.Send(iMessage, 0, iStatus);
    SetActive();
    }

    (Or maybe I missed the point of stream<<aMessage.. what is this for? )
    Last edited by dabeat; 2007-08-27 at 20:34.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    6

    Re: Infrared socket connection between symbian device and windows xp

    thank you for your reply , I have found the reason, just because when the accept returns, the recvonemore become valid at once, at this time the client still don't send the data, just is this.

Similar Threads

  1. Problem MMC+USB Connection
    By Fede81 in forum Symbian C++
    Replies: 1
    Last Post: 2007-06-14, 06:13
  2. Problem MMC+USB Connection
    By Fede81 in forum General Development Questions
    Replies: 3
    Last Post: 2006-07-19, 15:21
  3. Infrared socket connection between Symbian device and Windows
    By vespe in forum Symbian Networking & Messaging (Closed)
    Replies: 15
    Last Post: 2003-10-10, 16:50
  4. TCP socket connection between Symbian device and Windows
    By raghucp in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2003-09-15, 10:15
  5. Replies: 5
    Last Post: 2003-02-07, 11:25

Posting Permissions

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