OpenCV(3.4.1) with winsock2
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();
}
i don't think, we can help you much with this (off-topic), but at least try to explain:
btw, localhost ip is 127.0.0.1, and you're sending garbage to your client (line memory is never initialized)
if I didn't include opencv sConnect will be 192 if I did sConnect will be 18446744073709551615
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)
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.
i changed the broken:
to
and it just works.
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:
I recommend that you use non-blocking sockets, via calls to
ioctlsocket
.i recommend
select()
;)