Click here to Skip to main content
15,886,823 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to get all Acitve Windows Com Port Pin
Code-o-mat30-Aug-11 1:12
Code-o-mat30-Aug-11 1:12 
QuestionWaitForMultipleObjects Pin
john563229-Aug-11 23:47
john563229-Aug-11 23:47 
AnswerRe: WaitForMultipleObjects Pin
ThatsAlok29-Aug-11 23:54
ThatsAlok29-Aug-11 23:54 
AnswerRe: WaitForMultipleObjects Pin
Malli_S30-Aug-11 0:11
Malli_S30-Aug-11 0:11 
AnswerRe: WaitForMultipleObjects Pin
«_Superman_»30-Aug-11 4:38
professional«_Superman_»30-Aug-11 4:38 
QuestionBluetooth _ What is the GUID for " Text transfer" Pin
Vijay Rajanna29-Aug-11 10:11
Vijay Rajanna29-Aug-11 10:11 
AnswerRe: Bluetooth _ What is the GUID for " Text transfer" Pin
«_Superman_»30-Aug-11 4:43
professional«_Superman_»30-Aug-11 4:43 
QuestionBluetooth socket not receiving data sent from client Pin
Vijay Rajanna29-Aug-11 7:51
Vijay Rajanna29-Aug-11 7:51 
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.

C++
#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.
AnswerRe: Bluetooth socket not receiving data sent from client Pin
«_Superman_»30-Aug-11 4:44
professional«_Superman_»30-Aug-11 4:44 
QuestionCreateFile, ReadFile and Writhe file thread Pin
Bogdan Markovic29-Aug-11 3:28
Bogdan Markovic29-Aug-11 3:28 
AnswerRe: CreateFile, ReadFile and Writhe file thread Pin
Malli_S29-Aug-11 4:10
Malli_S29-Aug-11 4:10 
QuestionPorting MFC Application [modified] Pin
pix_programmer28-Aug-11 23:06
pix_programmer28-Aug-11 23:06 
AnswerRe: Porting MFC Application Pin
Sarath C28-Aug-11 23:51
Sarath C28-Aug-11 23:51 
QuestionRe: Porting MFC Application Pin
pix_programmer29-Aug-11 0:20
pix_programmer29-Aug-11 0:20 
AnswerRe: Porting MFC Application Pin
Albert Holguin29-Aug-11 5:02
professionalAlbert Holguin29-Aug-11 5:02 
AnswerRe: Porting MFC Application Pin
Rolf Kristensen29-Aug-11 8:53
Rolf Kristensen29-Aug-11 8:53 
QuestionRe: Porting MFC Application [modified] Pin
pix_programmer29-Aug-11 18:25
pix_programmer29-Aug-11 18:25 
AnswerRe: Porting MFC Application Pin
Rolf Kristensen29-Aug-11 20:20
Rolf Kristensen29-Aug-11 20:20 
QuestionWriting multiple files simultaneously inside the loop Pin
VCProgrammer28-Aug-11 19:38
VCProgrammer28-Aug-11 19:38 
AnswerRe: Writing multiple files simultaneously inside the loop Pin
Sarath C28-Aug-11 23:53
Sarath C28-Aug-11 23:53 
AnswerRe: Writing multiple files simultaneously inside the loop Pin
Code-o-mat28-Aug-11 23:58
Code-o-mat28-Aug-11 23:58 
AnswerRe: Writing multiple files simultaneously inside the loop Pin
Rolf Kristensen29-Aug-11 8:52
Rolf Kristensen29-Aug-11 8:52 
QuestionCode changes req. to improve 64 bit application performance Pin
am 200928-Aug-11 18:22
am 200928-Aug-11 18:22 
AnswerRe: Code changes req. to improve 64 bit application performance Pin
Rolf Kristensen28-Aug-11 20:30
Rolf Kristensen28-Aug-11 20:30 
AnswerRe: Code changes req. to improve 64 bit application performance Pin
MicroVirus29-Aug-11 1:33
MicroVirus29-Aug-11 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.