-1

I need help with a program I made.

It is a simple client server connection program that works perfectly fine, but as soon as I add #include anything openCV related, the connection consistently fails instantly, as if it didn't even try.

The only related things I found online were this post: Socket doesn't work, when opencv libriary is added

Here is the code, as long as the opencv #include are commented out it works fine, but if they are added at all, it fails to connect always.

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include <iostream>
#include <winsock2.h>
#include <string>

//#include <opencv2/opencv.hpp>
//#include <opencv2/objdetect.hpp>
//#include <opencv2/imgcodecs.hpp>
//#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
//using namespace cv;

#pragma comment(lib, "Ws2_32.lib")

int main()
{
    WSADATA WSAData;
    SOCKET server, client;
    SOCKADDR_IN serverAddr, clientAddr;

    WSAStartup(MAKEWORD(2, 0), &WSAData);
    server = socket(AF_INET, SOCK_STREAM, 0);

    serverAddr.sin_addr.s_addr = inet_addr("addresshere");
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(porthere);

    bind(server, (SOCKADDR*)&serverAddr, sizeof(serverAddr));
    listen(server, 0);

    cout << "Listening for incoming connections..." << endl;

    int clientAddrSize = sizeof(clientAddr);
    client = accept(server, (SOCKADDR*)&clientAddr, &clientAddrSize);

    cout << client << "\n";
}
  • 2
    Get rid of `using namespace std`. Chances are it's resulting in an incorrect symbol being used (my guess would be `bind`). – G.M. Dec 23 '19 at 12:27
  • Might be something to do with MACROS. Defined macros might interfere with each other. Whenever I include `windows.h` I gotta `#undef max` or program stops compiling. You could try isolating the headers to different .cpp so they don't interfere with each other. – ALX23z Dec 23 '19 at 12:27
  • Thank you so much! I removed using namespace std and it worked now! thank you so much. – Gromek999 Dec 23 '19 at 12:33
  • And how does it fail? – user253751 Dec 23 '19 at 12:42

1 Answers1

0

Answer from the comments: remove using namespace std, and just added std:: on the cout lines instead and now it works.

Seemingly it caused some issue with opencv, but when it is gone it works.

  • https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice | I'd say not just bad practice, it's one step shy of actually pointing a gun at your foot a pulling the trigger. C++ has namespaces for a good reason. – Dan Mašek Dec 23 '19 at 14:01