-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathServer.cpp
94 lines (78 loc) · 3.57 KB
/
Server.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
/*
* C++ UDP socket server for live image upstreaming
* Modified from http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/UDPEchoServer.cpp
* Copyright (C) 2015
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "PracticalSocket.h" // For UDPSocket and SocketException
#include <iostream> // For cout and cerr
#include <cstdlib> // For atoi()
#define BUF_LEN 65540 // Larger than maximum UDP packet size
#include "opencv2/opencv.hpp"
using namespace cv;
#include "config.h"
int main(int argc, char * argv[]) {
if (argc != 2) { // Test for correct number of parameters
cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
exit(1);
}
unsigned short servPort = atoi(argv[1]); // First arg: local port
namedWindow("recv", WINDOW_AUTOSIZE);
try {
UDPSocket sock(servPort);
char buffer[BUF_LEN]; // Buffer for echo string
int recvMsgSize; // Size of received message
string sourceAddress; // Address of datagram source
unsigned short sourcePort; // Port of datagram source
clock_t last_cycle = clock();
while (1) {
// Block until receive message from a client
do {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
} while (recvMsgSize > sizeof(int));
int total_pack = ((int * ) buffer)[0];
cout << "expecting length of packs:" << total_pack << endl;
char * longbuf = new char[PACK_SIZE * total_pack];
for (int i = 0; i < total_pack; i++) {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
if (recvMsgSize != PACK_SIZE) {
cerr << "Received unexpected size pack:" << recvMsgSize << endl;
continue;
}
memcpy( & longbuf[i * PACK_SIZE], buffer, PACK_SIZE);
}
cout << "Received packet from " << sourceAddress << ":" << sourcePort << endl;
Mat rawData = Mat(1, PACK_SIZE * total_pack, CV_8UC1, longbuf);
Mat frame = imdecode(rawData, IMREAD_COLOR);
if (frame.size().width == 0) {
cerr << "decode failure!" << endl;
continue;
}
imshow("recv", frame);
free(longbuf);
waitKey(1);
clock_t next_cycle = clock();
double duration = (next_cycle - last_cycle) / (double) CLOCKS_PER_SEC;
cout << "\teffective FPS:" << (1 / duration) << " \tkbps:" << (PACK_SIZE * total_pack / duration / 1024 * 8) << endl;
cout << next_cycle - last_cycle;
last_cycle = next_cycle;
}
} catch (SocketException & e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}