Bluetooth socket not receiving data sent from client.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Bluetooth socket not receiving data sent from client.

  1. #1
    Join Date
    Feb 2009
    Posts
    24

    Bluetooth socket not receiving data sent from client.

    HI,

    I'm using windows 7 , 64 bit os, and had written bluetooth server program running on this pc.

    This server will accept connections from clients , and display the text sent by clients.

    My issue is , Bluetooth server running on PC, is not receiving data sent from clients, but connection establishment is successful.

    What I mean is, after executing "recv(s2,(char*)buffer, sizeof(buffer), 0);" function call, the server is blocked indefinitely.

    Please let me know how this issue could be resolved.

    #include "stdafx.h"
    #include <WinSock2.h>
    #include <ws2bth.h>
    #include <bthsdpdef.h>
    #include <BluetoothAPIs.h>

    #pragma comment(lib, "Ws2_32.lib")
    #pragma comment(lib, "irprops.lib")

    TCHAR *GetLastErrorMessage(DWORD last_error)
    {
    static TCHAR errmsg[512];

    if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
    0,
    last_error,
    0,
    errmsg,
    511,
    NULL))
    {
    /* if we fail, call ourself to find out why and return that error */
    return (GetLastErrorMessage(GetLastError()));
    }
    return errmsg;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    WORD wVersionRequested = 0x202;
    WSADATA m_data;

    if (0 == WSAStartup(wVersionRequested, &m_data))
    {
    SOCKET s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);

    const DWORD lastError = ::GetLastError();

    if (s == INVALID_SOCKET)
    {
    printf("Failed to get bluetooth socket! %s\n", GetLastErrorMessage(lastError));
    exit(1);
    }

    WSAPROTOCOL_INFO protocolInfo;

    int protocolInfoSize = sizeof(protocolInfo);

    if (0 != getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFO, (char*)&protocolInfo, &protocolInfoSize))
    {
    exit(1);
    }

    SOCKADDR_BTH address;

    address.addressFamily = AF_BTH;
    address.btAddr = 0;
    address.serviceClassId = GUID_NULL;
    address.port = BT_PORT_ANY;

    sockaddr *pAddr = (sockaddr*)&address;

    if (0 != bind(s, pAddr, sizeof(SOCKADDR_BTH)))
    {
    printf("%s\n", GetLastErrorMessage(GetLastError()));
    }
    else
    {
    printf("\nBinding Successful....\n");
    int length = sizeof(SOCKADDR_BTH) ;
    getsockname(s,(sockaddr*)&address,&length);
    wprintf (L"Local Bluetooth device is %04x%08x \nServer channel = %d\n", GET_NAP(address.btAddr), GET_SAP(address.btAddr), address.port);
    }

    int size = sizeof(SOCKADDR_BTH);

    if (0 != getsockname(s, pAddr, &size))
    {
    printf("%s\n", GetLastErrorMessage(GetLastError()));
    }

    if (0 != listen(s, 10))
    {
    printf("%s\n", GetLastErrorMessage(GetLastError()));
    }

    WSAQUERYSET service;
    memset(&service, 0, sizeof(service));
    service.dwSize = sizeof(service);
    service.lpszServiceInstanceName = _T("Accelerometer Data...");
    service.lpszComment = _T("Pushing data to PC");

    GUID serviceID = OBEXFileTransferServiceClass_UUID;

    service.lpServiceClassId = &serviceID;
    service.dwNumberOfCsAddrs = 1;
    service.dwNameSpace = NS_BTH;

    CSADDR_INFO csAddr;
    memset(&csAddr, 0, sizeof(csAddr));
    csAddr.LocalAddr.iSockaddrLength = sizeof(SOCKADDR_BTH);
    csAddr.LocalAddr.lpSockaddr = pAddr;
    csAddr.iSocketType = SOCK_STREAM;
    csAddr.iProtocol = BTHPROTO_RFCOMM;

    service.lpcsaBuffer = &csAddr;

    if (0 != WSASetService(&service, RNRSERVICE_REGISTER, 0))
    {
    printf("Service registration failed....");
    printf("%d\n", GetLastErrorMessage(GetLastError()));
    }
    else
    {

    printf("\nService registration Successful....\n");

    }

    printf("\nBefore accept.........");

    SOCKADDR_BTH sab2;
    int ilen = sizeof(sab2);
    SOCKET s2 = accept (s,(sockaddr*)&sab2, &ilen);
    if (s2 == INVALID_SOCKET)
    {
    wprintf (L"Socket bind, error %d\n", WSAGetLastError ());
    }

    wprintf (L"\nConnection came from %04x%08x to channel %d\n",
    GET_NAP(sab2.btAddr), GET_SAP(sab2.btAddr), sab2.port);
    wprintf (L"\nAfter Accept\n");

    unsigned char buffer[2000];

    while(1)
    {

    memset(buffer, 0, sizeof(buffer));
    int r = recv(s2,(char*)buffer, sizeof(buffer), 0);
    printf("Error = %d\n",WSAGetLastError() /*GetLastErrorMessage(GetLastError())*/);
    wprintf(L"Received : %s\n",buffer);
    Sleep(500);
    }
    //SpinConnectionThreadsOnSocket (s2);

    closesocket(s2);
    if (0 != WSASetService(&service, RNRSERVICE_DELETE, 0))
    {
    printf("%s\n", GetLastErrorMessage(GetLastError()));
    }
    closesocket(s);
    WSACleanup();
    }

    return 0 ;
    }




    Here is the snapshot, where you can see server is waiting, (executing recv())

    http://dl.dropbox.com/u/37131873/Pics/6675.jpg

    Thanks in advance.

    Regards,
    Vijay.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,290

    Re: Bluetooth socket not receiving data sent from client.

    Dear Vijay,
    please edit your post adding Code tags around code snippet(s) and attach "the snapshot" to the post.
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: Bluetooth socket not receiving data sent from client.

    Agreement with VictorN about the need to edit your post.

    In addition:
    Quote Originally Posted by VijayDandur View Post
    My issue is , Bluetooth server running on PC, is not receiving data sent from clients, but connection establishment is successful.

    What I mean is, after executing "recv(s2,(char*)buffer, sizeof(buffer), 0);" function call, the server is blocked indefinitely.
    How have you confirmed that the client is indeed sending data? The behavior you are seeing on the server side is completely consistent with a non-communicative client who, after establishing a connection, has not bothered to send any data.

    So, please explain how you have confirmed that the client is sending something.

    Perhaps post the client code, but I have a suspicion that (as in your other post) you have not written any client code but are instead relying on some "built-in" behavior of the client (a phone, I think).

    Mike

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center