forked from prouast/heartbeat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cpp
84 lines (59 loc) · 1.98 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* OpenCV video streaming over TCP/IP
* Client: Receives video from server and display it
* by Steve Tuenkam
*/
#include "opencv2/opencv.hpp"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace cv;
int main(int argc, char** argv)
{
//--------------------------------------------------------
//networking stuff: socket , connect
//--------------------------------------------------------
int sokt;
char* serverIP;
int serverPort;
if (argc < 3) {
std::cerr << "Usage: cv_video_cli <serverIP> <serverPort> " << std::endl;
}
serverIP = argv[1];
serverPort = atoi(argv[2]);
struct sockaddr_in serverAddr;
socklen_t addrLen = sizeof(struct sockaddr_in);
if ((sokt = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
std::cerr << "socket() failed" << std::endl;
}
serverAddr.sin_family = PF_INET;
serverAddr.sin_addr.s_addr = inet_addr(serverIP);
serverAddr.sin_port = htons(serverPort);
if (connect(sokt, (sockaddr*)&serverAddr, addrLen) < 0) {
std::cerr << "connect() failed!" << std::endl;
}
//----------------------------------------------------------
//OpenCV Code
//----------------------------------------------------------
Mat img;
img = Mat::zeros(480 , 640, CV_8UC1);
int imgSize = img.total() * img.elemSize();
uchar *iptr = img.data;
int bytes = 0;
int key;
//make img continuos
if ( ! img.isContinuous() ) {
img = img.clone();
}
std::cout << "Image Size:" << imgSize << std::endl;
namedWindow("CV Video Client",1);
while (key != 'q') {
if ((bytes = recv(sokt, iptr, imgSize , MSG_WAITALL)) == -1) {
std::cerr << "recv failed, received bytes = " << bytes << std::endl;
}
cv::imshow("CV Video Client", img);
if (key = cv::waitKey(10) >= 0) break;
}
close(sokt);
return 0;
}