Hello C++ experts,
I would like to ask someone who already used C++ for windows socket programming. I am getting following errors:
identifier "Winsock" is undefined and many redefinition errors
I searched through stackoverflow forums and for these errors people suggested:
"
You need to re-order the includes of the Windows headers like so:
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
In other words, the WinSock headers must be included before Windows.h.
"
I included WinSock header before Windows header and still can't get it working also tried other suggestions from this post:
c++ - socket errors can't get functions in WinSock2.h - Stack Overflow
Code:
#include "stdafx.h"
#include "Winsock.h"
#include <WinSock2.h>
#include <Windows.h>
#include <ws2bth.h>
#include <bthsdpdef.h>
#include <bluetoothapis.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "irprops.lib")
int main()
{
if (!Winsock::InitialzedForCurrentProcess())
{
return 1;
}
Winsock winsock = Winsock(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (!winsock.IsValid())
{
return 1;
}
winsock.Bind();
winsock.Listen();
winsock.RegisterServcie(_T("RFCOMM Server Demo Instance"), _T("Pushing data to PC"), OBEXObjectPushServiceClass_UUID);
winsock.OnConnected([](SOCKET s){
char buffer[1024] = { 0 };
memset(buffer, 0, sizeof(buffer));
int r = 0;
do
{
r = recv(s, (char*)buffer, sizeof(buffer), 0);
printf("result:%d, %s\n", r, buffer);
} while (r != 0);
closesocket(s);
});
winsock.Accept();
WSACleanup();
return 0;
}
code source: [Bluetooth] – How to Host a RFCOMM Service in C++ – Practice & Share
Best regards