Newer
Older
//============================================================================
// Name : Tarea2-RC-Grupo25-Cliente.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
Alvaro Liber Vallve Maidana
committed
#include <unistd.h>
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#include <opencv2/opencv.hpp>
using namespace cv;
#define BUFLEN 1024*1024 //Max length of buffer
#define PORT 8890 //The port on which to listen for incoming data
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Alvaro Liber Vallve Maidana
committed
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
void die(char *s)
{
perror(s);
exit(1);
}
/**
* Queda escuchando en el puerto UDP indicado.
*
*/
int pruebaRecibirUDP(){
struct sockaddr_in si_me, si_other;
int s, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];
//create a UDP socket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
//bind socket to port
if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
die("bind");
}
socklen_t server_addr_size = sizeof si_other;
//keep listening for data
while(1)
{
printf("Waiting for data...");
fflush(stdout);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &server_addr_size)) == -1)
{
die("recvfrom()");
}
//print details of the client/peer and the data received
printf("\nReceived packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Tamanio Data: %d\n", sizeof(buf));
namedWindow("cliente", CV_WINDOW_AUTOSIZE);
Mat rawData = Mat(1, sizeof(buf), CV_8UC1, buf);
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
imshow("cliente", frame);
waitKey(1000/30);
//now reply the client with the same data
/*
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1)
{
die("sendto()");
}
*/
}
close(s);
return 0;
}