Reputation: 1241
I write a program, that should create a server using a socket in a extra thread. This works well, until I #include <mutex>
to my application. But I need to include mutex for another function (not in the example here).
When I include mutex, I get this error when calling bind
(marked in code):
error C2440: '=': cannot convert 'std::_Bind' to 'long' in (PATH_TO_PROJECT_LINE_WHERE_BIND_IS_CALLED)
Here is my code:
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma comment(lib,"ws2_32.lib")
#include <cstdio>
#include <WinSock2.h>
#include <iostream>
#include <Windows.h>
#include <string>
#include <process.h>
#include <mutex> //if I do not include mutex, everything works
using namespace std;
unsigned int __stdcall threadCreateServer(void*data){
long res;
WSADATA wsaData;
res = WSAStartup(MAKEWORD(2, 0), &wsaData);
if (res == 0){
cout << "[CreateServer] " << "WSAStartup successful" << endl;
}
else {
cout << "[CreateServer] " << "Error WSAStartup" << endl;
return -201;
}
SOCKET slisten, client;
slisten = socket(AF_INET, SOCK_STREAM, 0);
if (slisten != INVALID_SOCKET){
cout << "[CreateServer] " << "Socket() successful" << endl;
}
else{
cout << "[CreateServer] " << "error Socket" << endl;
return -202;
}
sockaddr_in info;
info.sin_addr.s_addr = inet_addr("127.0.0.1");
info.sin_family = AF_INET;
info.sin_port = htons(54126);
res = bind(slisten, (struct sockaddr*)&info, sizeof(info)); //ERROR HERE
if (res != SOCKET_ERROR){
cout << "bind successful" << endl;
}
else {
cout << "ERROR bind" << endl;
return -203;
}
res = listen(slisten, 1);
if (res != SOCKET_ERROR){
cout << "[CreateServer] " << "Listen successful" << endl;
}
else {
cout << "[CreateServer] " << "Listen error" << endl;
return -204;
}
sockaddr_in clientinfo;
int clientinfolen = sizeof(clientinfo);
cout << endl << "~~~~~~~~~" << endl << "[CreateServer] " << "Please connect a client to " << inet_ntoa(info.sin_addr) << ":" << ntohs(info.sin_port) << endl << "~~~~~~~~~" << endl;
client = accept(slisten, (struct sockaddr*)&clientinfo, &clientinfolen);
if (client != SOCKET_ERROR){
cout << "[CreateServer] " << "Client accepted " << inet_ntoa(clientinfo.sin_addr) << ":" << ntohs(clientinfo.sin_port) << endl;
}
else{
cout << "[CreateServer] " << "ERROR client not accepted" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE handleThreadCreateServer=(HANDLE)_beginthreadex(0,0,&threadCreateServer,0,0,0);
WaitForSingleObject(handleThreadCreateServer, INFINITE);
CloseHandle(handleThreadCreateServer);
return 0;
}
Upvotes: 4
Views: 565
Reputation: 1241
As proposed in a comment, I cannot do using namespace std
, because there would be conflicting functions when including mutex
.
For everyone, who will have this problem, too:
//to not using namespace std, but:
using std::cout;
using std::endl;
using std::string;
For every other function that isn't used until now, a similar line has to be added. There is not possibllity of using the namespace std and "unusing" std::mutex.
Upvotes: 2