-1

Hello everyone! I'm trying to develop AR app, using C++ server and Unity as a client. I've done simple connection using WinSock between C++ server and Unity, and it send simple string. Now I tried to add opencv to the server (The idea is: it detects the marker and sends cordinates to Unity). However, when I add opencv libriaries, the server stops to work properly (When I start the server, it immediately says that the client is connected, but the client is not started). So i have no idea, what to do. Here is a code:

#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma comment(lib,"ws2_32.lib")
#include <WinSock2.h>
#include <iostream>
#include <string>
#include "opencv2\core.hpp"
#include "opencv2\imgcodecs.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\highgui.hpp"
#include "opencv2\aruco.hpp"
#include "opencv2\calib3d.hpp"

#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
int main() {

    WSAData wsaData;
    WORD DllVersion = MAKEWORD(2, 1);
    if (WSAStartup(DllVersion, &wsaData) != 0) {
        MessageBoxA(NULL, "WinSock sturtup failed", "Error", MB_OK | MB_ICONERROR);
        exit(1);
    }

    SOCKADDR_IN addr; // socket adresy
    int addrlen = sizeof(addr);
    addr.sin_addr.s_addr = inet_addr("100.100.0.1"); //
    addr.sin_port = htons(10101);
    addr.sin_family = AF_INET; //ipv4

    SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL);
    bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
    listen(sListen, SOMAXCONN);

    SOCKET newConnection;
    newConnection = accept(sListen, (SOCKADDR*)&addr, &addrlen);
    if (newConnection == 0) {
        cout << "Failed to accept the client's connection" << endl;
    }
    else {
        cout << "Client connected!" << endl;
        char MOTD[256];
        string sendData;
        cin >> sendData;
        int i;
        for (i = 0; i < sendData.length(); i++)
        {
            MOTD[i] = sendData.at(i);
        }
        send(newConnection, MOTD, i, NULL);
    }

    system("pause");

    return 0;
}

Please Notice that code is working well (when there no opencv libs added).

Данияр
  • 91
  • 1
  • 10
  • @MickyD You're not wrong, but I think OP meant C# for the Unity side code not shown – Passer By Sep 16 '17 at 07:42
  • @PasserBy there is no problem with c# side. I'm sure. Because server and client works. But when I add opencv to the server, the server shows wrong. Or are you wanted to say that I shouldn't tag c# – Данияр Sep 16 '17 at 08:27
  • I thing testing for zero is wrong in the IF statement. See : https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526(v=vs.85).aspx – jdweng Sep 16 '17 at 09:59
  • @PasserBy my bad. Thanks –  Sep 16 '17 at 10:18
  • @jdweng It should be INVALID_SOCKET but that has value of zero so it won't change behaviour – David Heffernan Sep 16 '17 at 13:25
  • Are you sure that INVALID_SOCKET = 0? Shouldn't it be : if(newConnection != 0). It is -1 see https://msdn.microsoft.com/en-us/library/windows/desktop/ms740516(v=vs.85).aspx – jdweng Sep 16 '17 at 15:19
  • @Данияр I recommend pasting a [mcve], since that would really cut down any doubt where the problem is. However, if you choose not to, remove the C# tag. – Passer By Sep 16 '17 at 15:42

1 Answers1

1

Delete using namespace std; and using namespace cv; and add std:: and cv:: . The problem will be solved.

hossein
  • 11
  • 1