OpenCV(3.4.1) with winsock2

asked 2018-05-31 01:32:14 -0500

Silli Tim gravatar image

updated 2018-05-31 01:58:28 -0500

Recently, I am coding a c++ image transmit using OpenCV(3.4.1) &winsock2.

while using the function accept(), it will get a overflowing value.

If i didn't include opencv2/opencv.hpp , it will be fine.

someone plz help me!!

pragma comment(lib, "Ws2_32.lib")

#include <WinSock2.h>
#include <iostream>
#include <Ws2tcpip.h>
#include <opencv2/opencv.hpp>


using namespace std;


int main()
{
    int r;
    WSAData wsaData;
    WORD DLLVSERION;
    DLLVSERION = MAKEWORD(2, 1);//Winsocket-DLL 版本

                                //用 WSAStartup 開始 Winsocket-DLL
    r = WSAStartup(DLLVSERION, &wsaData);

    //宣告 socket 位址資訊(不同的通訊,有不同的位址資訊,所以會有不同的資料結構存放這些位址資訊)
    SOCKADDR_IN addr;
    int addrlen = sizeof(addr);

    //建立 socket
    SOCKET sListen; //listening for an incoming connection
    SOCKET sConnect; //operating if a connection was found

    sConnect = socket(AF_INET, SOCK_STREAM, NULL);

    cout << "sConnect is " << sConnect << endl;

    //設定位址資訊的資料
    inet_pton(AF_INET, "172.0.0.1", &addr.sin_addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(1234);

    //設定 Listen
    sListen = socket(AF_INET, SOCK_STREAM, NULL);
    bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
    listen(sListen, SOMAXCONN);//SOMAXCONN: listening without any limit

                               //等待連線
    SOCKADDR_IN clinetAddr;



    cout << "waiting.............." << endl;
    sConnect = accept(sListen, (SOCKADDR*)&clinetAddr, &addrlen);
    //sTemp = sConnect;     //saving the descripter of client's socket descripter

    //char *sendbuf;
    char line[100]= {0};
    send(sConnect, line, (int)strlen(line), 0);
    cout << "sListen is " << sListen << endl;
    cout << "sConnect is " << sConnect << endl;
    cout << "failed" << endl;
    getchar();
}
edit retag flag offensive close merge delete

Comments

i don't think, we can help you much with this (off-topic), but at least try to explain:

it will get a overflowing value.

berak gravatar imageberak ( 2018-05-31 01:35:09 -0500 )edit

btw, localhost ip is 127.0.0.1, and you're sending garbage to your client (line memory is never initialized)

berak gravatar imageberak ( 2018-05-31 01:50:25 -0500 )edit

if I didn't include opencv sConnect will be 192 if I did sConnect will be 18446744073709551615

Silli Tim gravatar imageSilli Tim ( 2018-05-31 01:55:50 -0500 )edit

18446744073709551615 == -1 == INVALID_SOCKET.

again, change the ip to 127.0.0.1 (or 0.0.0.0, if you want a "real" network device)

berak gravatar imageberak ( 2018-05-31 03:10:32 -0500 )edit

I worked with WinSock1 under MFC. Would be interesting to do the same with WinSock2. If you are ready to wait for a while, I can reproduce your program on my machine and look what happens.

ya_ocv_user gravatar imageya_ocv_user ( 2018-05-31 03:17:06 -0500 )edit

i changed the broken:

inet_pton(AF_INET, "172.0.0.1", &addr.sin_addr);

to

addr.sin_addr.s_addr = INADDR_ANY;

and it just works.

berak gravatar imageberak ( 2018-05-31 03:40:56 -0500 )edit

If you want a working, finished reference to go by, here is the code for my multithreaded TCP server:

https://github.com/sjhalayka/tcpspeed...

One thing I do is check for errors:

bool TCP_server::check_for_pending_connection(void)
{
    // stuff goes here

    if (INVALID_SOCKET == (accept_socket = accept(tcp_socket, (struct sockaddr *) &my_addr, &sock_addr_len)))
    {
        if (WSAEWOULDBLOCK != WSAGetLastError())
            cout << "  Accept error " << WSAGetLastError() << endl;

        return false;
    }

    return true;
}
sjhalayka gravatar imagesjhalayka ( 2018-05-31 10:13:16 -0500 )edit

I recommend that you use non-blocking sockets, via calls to ioctlsocket.

sjhalayka gravatar imagesjhalayka ( 2018-05-31 10:14:47 -0500 )edit
2

i recommend select() ;)

berak gravatar imageberak ( 2018-05-31 10:42:37 -0500 )edit