Windows Sockets to share data between projects (IPC) [SOLVED]

Can’t find the culprit, please help.

In a client-server Visual Studio 2017 (Windows 10) solution, the server socket returns “204.204.204.204 connected on port 52428” when I #include almost any OF098 header file, i.e., the socket stays in 0 and does not wait to listen to client (no connection with client). When I comment OF code as shown below, the connection between Project A and Project B works fine. I first started looking into OF addons/headers that might conflict, e.g., ofKinect, ofThread, but ended finding that #include “ofConstants.h” causes the behavior explained above. Further digging led me to comment out #include functional (inside ofConstants.h), but then #include <boost/filesystem.hpp> inside ofFileUtils.h causes the same problem. Do you have any tips?

SOLUTION: https://stackoverflow.com/questions/20314343/c-including-thread-redefines-winsock-function-bind

Project A implements winsock client. It sends data:

main.cpp

#include
#include <opencv2/opencv.hpp>
#include “Opencv2Opengl.h”
#include “glew.h”
#include “freeglut.h”

#include “vpg.h”
#include “Spout.h”

#include <WS2tcpip.h>
#include

#pragma comment (lib, “ws2_32.lib”)

using namespace std;

//#define DATA_WINDOW
template
std::string num2str(T value, unsigned char precision=1);

void drawDataWindow(const cv::String &_title, const cv::Size _windowsize, const float *_data, const int _datalength, double _ymax, double _ymin, cv::Scalar _color);

int main(int argc, char *argv[])
{
this_thread::sleep_for(5s);
string ipAddress = “127.0.0.1”; // IP Address of the server
int port = 54000; // Listening port # on the server

// Initialize WinSock
WSAData data;
WORD ver = MAKEWORD(2, 2);
int wsResult = WSAStartup(ver, &data);

//Winsock: send and receive data 
const SIZE_T buf_size = 4096;
char buf[buf_size];
string userInput;	

if (wsResult != 0)
{
	cerr << "Can't start Winsock, Err #" << wsResult << endl;
	system("pause");
	return 0;
}

// Create socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
	cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
	WSACleanup();
	system("pause");
	return 0;
}

// Fill in a hint structure
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(port);
inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);

// Connect to server
int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
if (connResult == SOCKET_ERROR)
{
	cerr << "Can't connect to server, Err #" << WSAGetLastError() << endl;
	closesocket(sock);
	WSACleanup();
	system("pause");
	return 0;
}

…data generaton code

//get data and output to OF098
const float *hrvData = hrvproc.getHRVSignal();
userInput = to_string(*hrvData);
//cout << "hrv: " << *hrvData << endl;

	if (userInput.size() > 0) // Make sure the user has typed in something
	{
							
		// Send the text
		int sendResult = send(sock, userInput.c_str(), userInput.size(), 0);
		if (sendResult != SOCKET_ERROR)
		{
			// Wait for response
			ZeroMemory(buf, buf_size);
			int bytesReceived = recv(sock, buf, buf_size, 0);
			if (bytesReceived > 0)
			{
				// Echo response to console
				cout << "SERVER> " << string(buf, 0, bytesReceived) << endl;
			}
		}
	}		

Project B (OpenFrameworks) implements winsock server. It receives the data from Project A:

main.cpp

#include
#include <WS2tcpip.h>
#include

#pragma comment (lib, “ws2_32.lib”)

using namespace std;

//#include “ofMain.h”
//#include “ofApp.h”

//========================================================================
void main( ){

//Initialze winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);

int wsOk = WSAStartup(ver, &wsData);
if (wsOk != 0)
{
	cerr << "Can't Initialize winsock! Quitting" << endl;
	return;
}

// Create a socket
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
	cerr << "Can't create a socket! Quitting" << endl;
	return;
}

// Bind the ip address and port to a socket
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(54000);
hint.sin_addr.S_un.S_addr = INADDR_ANY; // Could also use inet_pton .... 

bind(listening, (sockaddr*)&hint, sizeof(hint));

// Tell Winsock the socket is for listening 
listen(listening, SOMAXCONN);

// Wait for a connection
sockaddr_in client;
int clientSize = sizeof(client);

SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);

char host[NI_MAXHOST];		// Client's remote name
char service[NI_MAXSERV];	// Service (i.e. port) the client is connect on

ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);
ZeroMemory(service, NI_MAXSERV);

if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
	cout << host << " connected on port " << service << endl;
}
else
{
	inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
	cout << host << " connected on port " <<
		ntohs(client.sin_port) << endl;
}

// Close listening socket
closesocket(listening);

// While loop: accept and echo message back to client
const SIZE_T buf_size = 4096;
char buf[buf_size];
while (true)
{
	
	ZeroMemory(buf, buf_size);

	// Wait for client to send data
	int bytesReceived = recv(clientSocket, buf, buf_size, 0);
	if (bytesReceived == SOCKET_ERROR)
	{
		cerr << "Error in recv(). Quitting" << endl;
		break;
	}

	if (bytesReceived == 0)
	{
		cout << "Client disconnected " << endl;
		break;
	}

	cout << string(buf, 0, bytesReceived) << endl;

	// Echo message back to client
	send(clientSocket, buf, bytesReceived + 1, 0);

}
   
//ofSetupOpenGL(1024, 768, OF_WINDOW);			// <-------- setup the GL context

// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
//ofRunApp(new ofApp());

// Close the socket
closesocket(clientSocket);

// Cleanup winsock
WSACleanup();

system("pause");	   

}