Newer
Older
#include "Clients.h"
#include "Util.h"
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
Alvaro Liber Vallve Maidana
committed
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
#include <opencv2/opencv.hpp>
using namespace cv;
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define MAXLEN 1048576 //1024*1024
const char delimiter[] = "Grupo25";
/**
* Funcion auxiliar que retorna dentro del array indicando la posicion del comienzo del delimitador
* array: arreglo a consultar
* len: largo de array
* delimiter: delimitador a buscar
* inicio: desde que posicion arrancar a buscar
*/
int findPosDelimiter(const char *array, int len, int inicio) {
int i = -1;
int pos = 0;
for (i = inicio; i<len; i++) {
if (sizeof(delimiter)-1 == pos) {
if (array[i] == delimiter[pos]) {
return i - sizeof(delimiter) + 2;
} else {
pos=0;
}
} else {
if (array[i] == delimiter[pos]) {
pos++;
} else {
pos=0;
}
}
}
return -1;
}
void TCP(const char* HOST){
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
//obtenemos la direccion con getaddrinfo
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
Alvaro Liber Vallve Maidana
committed
getaddrinfo(HOST, STR(TCP_PORT), &hints, &res);
//primitiva CONNECT
connect(client_socket, res->ai_addr, res->ai_addrlen);
Alvaro Liber Vallve Maidana
committed
printf("Intentando conexion con %s:%d\n",HOST,TCP_PORT);
//primitiva SEND
char *msg = "TCP";
int msg_size = strlen(msg);
int sent_msg_size = send(client_socket, msg, msg_size, 0);
Alvaro Liber Vallve Maidana
committed
if (sent_msg_size == -1) {
printf("ERROR: imposible establecer conexion con el servidor");
return;
}
printf("Conexion establecida con %s:%d\n",HOST,TCP_PORT);
char buf[MAXLEN]; // se almacenan los datos TCP recibidos en cada recv
char recibido[MAXLEN]; // mantiene el historico de datos recibidos, quitando los frames ya dibujados
int fin = 0; // Mantiene el indice del array recibido.
double fpms = 1000/30; //frames for ms
int received_data_size = 1;
while(received_data_size > 0)
{
//primitiva RECEIVE
int data_size = MAXLEN;
received_data_size = recv(client_socket, buf, data_size, 0);
printf("Datos recibidos: %d\n", received_data_size);
// en el arrray 'recibido' se mantiene los datos que ya exisitian mas los nuevos.
for(int j = 0; j < received_data_size; j++) {
recibido[fin+j] = buf[j];
}
fin = fin + received_data_size;
Alvaro Liber Vallve Maidana
committed
int inicio = 0;
int found = -1;
while ((found = findPosDelimiter(recibido, fin+1, inicio)) != -1) {
char jpg[found-inicio];
Alvaro Liber Vallve Maidana
committed
for (int h = 0; h < found-inicio; h++) {
jpg[h] = recibido[inicio+h];
}
Alvaro Liber Vallve Maidana
committed
printf("Nuevo jpeg size: %li\n", sizeof(jpg));
Alvaro Liber Vallve Maidana
committed
namedWindow("cliente", CV_WINDOW_AUTOSIZE);
Alvaro Liber Vallve Maidana
committed
Mat rawData = Mat(1, found, CV_8UC1, jpg);
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
Alvaro Liber Vallve Maidana
committed
imshow("cliente", frame);
waitKey(fpms);
inicio = found + sizeof(delimiter)-1;
}
if (inicio != 0) {
// mueve los datos recibidos al inicio, quitando el (o los) frame dibujado y el delimitador
for (int d = inicio; d < fin+1; d++) {
recibido[d-inicio] = recibido[d];
}
fin = fin-inicio;
}
}
close(client_socket);
freeaddrinfo(res);
Alvaro Liber Vallve Maidana
committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
}
void UDP(char* HOSTNAME){
unsigned int length;
struct from;
// CREATE SOCKET
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) error("error when creating socket");
char buffer[256];
bzero(buffer,256);
sprintf(buffer, "subscription");
// ENVIO PEDIDO DE SUSCRIPCION
printf("Sending Subscription\n");
if (sendDatagramUDPtoHostname(sock, HOSTNAME, buffer, UDP_PORT) < 0)
error("Error on send first datagram");
printf("SENT\n");
// ESPERO RESPUESTA DEL SERVIDOR
bzero(buffer,256);
struct sockaddr_in from;
if (recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length) < 0)
error("recvfrom");
printf("Subscription: %s\n", buffer);
while (true)
{
bzero(buffer,256);
if (recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length) < 0)
error("recv from UDP frames");
printf("%s\n", buffer);
}
close(sock);
}