Search code examples
c++tcp

TCP/IP server code stopped working after including additional library


I'm a beginner with computer-vision and networking. I have made a program to detect and localise small marbles and that gives me the X,Y,Z position of the marble. I have also made a small TCP/IP server application to send this X,Y,Z position to a robot.

I now want to combine these two different applications into one application. But when I try to combine them the tcp/ip server part stops working. After some testing with importing some different code lines I looks to go wrong whenever I try to include a different library, like OpenCV or Librealsense2. Would anyone know why this is happening and how I can solve this?

I've included the server code below:

#include <iostream>                 
#include <WS2tcpip.h>           

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

using namespace std;



int main()
{

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

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

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

    //bind the ip to a socket address and port
    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(1701);
    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);
    if (clientSocket == INVALID_SOCKET)
    {
        cerr << "Socket not valid! quitting" << endl;
        return 3;
    }

    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);
    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);

    char buf[4096];

    while (true) //this part of the code will be used to send and receive data
    {
        ZeroMemory(buf, 4096);
        float X = 12.4;
        float Y = 456;
        float Z = 900.87;

        sprintf_s(buf, "(%lf,%lf,%lf)",X,Y,Z);
        size_t buf_len = strlen(buf);

        send(clientSocket, buf, buf_len, 0);




        int bytesReceived = recv(clientSocket, buf, 4096, 0);
    }

    //close the socket(s)
    closesocket(clientSocket);

    //shutdown/cleanup winsock (sockets)
    WSACleanup();

}

When I add something like this, it stops working (returns error code 3):

#include <opencv2/opencv.hpp>       //OpenCV library onderdeel
#include <opencv2/core.hpp>         //OpenCV library onderdeel
#include <opencv2/highgui.hpp>      //OpenCV library onderdeel

Console screenshot: Console gives error 3


Solution

  • I think you can improve your code and solve the problem by doing these things:

    1. Since you are using C++, it is much better to construct the program into several class, which means several modules, and use them to realise complex jobs.

    2. It is not wise to use using namespace std, we will use std library only when need, such as std::cout because the std library is really huge and sometimes will be conflict with other third libiraries.

    3. You can try I/O multiplexing like epoll or IOCP, they can improve the performance of your code very much.