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 #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()));
-
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()));
Use
SerialPortServiceClass_UUID
for theserviceClassId
parameter.«_Superman_» _I love work. It gives me something to do between weekends.