添加opencv库时套接字不工作

atmip9wb  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(99)

大家好!我正在尝试开发 AR 应用程序,使用C++服务器和Unity作为客户端。我已经使用WinSock在C++服务器和Unity之间建立了简单的连接,并发送了简单的字符串。现在我尝试将opencv添加到服务器(想法是:它检测到标记并发送坐标到Unity).然而,当我添加opencv库时,服务器停止正常工作(当我启动服务器时,它立即说客户端已连接,但客户端没有启动).所以我不知道该怎么做.下面是一个代码:

#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;
}

请注意,代码运行良好(当没有添加opencv库时)。

j13ufse2

j13ufse21#

删除using namespace std;using namespace cv;,并添加std::cv::。问题将得到解决。

相关问题