-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.cpp
116 lines (92 loc) · 2.79 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "opencv2/opencv.hpp"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
//--------------------------------------------------------
// networking stuff: socket , connect
//--------------------------------------------------------
int sokt;
char *serverIP;
int serverPort;
if (argc < 3)
{
serverIP = "10.0.0.210";
serverPort = 1235;
}
else
{
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(1024, 1280, CV_8UC1);
int imgSize = img.total() * img.elemSize();
uchar *iptr = img.data;
int bytes = 0;
int key;
int thickness = 2;
int posX = 5;
int posY = 50;
stringstream ss;
ss << "Connection time out , please wait ...";
Point textOrg(posX, posY);
int fontFace = FONT_HERSHEY_SIMPLEX;
double fontScale = 0.7;
// make img continuos
if (!img.isContinuous())
{
img = img.clone();
}
std::cout << "Image Size:" << imgSize << std::endl;
namedWindow("TSL FireView Spaceborne", 1);
while (key != 'q')
{
if ((bytes = recv(sokt, iptr, imgSize, MSG_WAITALL)) < 1)
{
std::cerr << "recv failed, received bytes = " << bytes << std::endl;
close(sokt);
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;
}
putText(img, ss.str(), textOrg, fontFace, fontScale, CV_RGB(250, 0, 0), thickness);
cv::imshow("TSL FireView Spaceborne", img);
}
else
{
cv::imshow("TSL FireView Spaceborne", img);
}
if (key = cv::waitKey(10) >= 0)
break;
}
close(sokt);
return 0;
}