Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Bluetooth socket not receiving data sent from client

Bluetooth socket not receiving data sent from client

Scheduled Pinned Locked Moved C / C++ / MFC
helpsysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vijay Rajanna
    wrote on last edited by
    #1

    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 #include #include #include #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()));
    
    _ 1 Reply Last reply
    0
    • V Vijay Rajanna

      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 #include #include #include #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()));
      
      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Use SerialPortServiceClass_UUID for the serviceClassId parameter.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups