Skip to content
Snippets Groups Projects
Commit 1ce27bd7 authored by Ramiro Facundo Lorenzo Rodriguez Inthamoussu's avatar Ramiro Facundo Lorenzo Rodriguez Inthamoussu
Browse files

SERVICIO UDP TERMINADO

parent 6a7573d2
No related branches found
No related tags found
No related merge requests found
...@@ -47,20 +47,17 @@ void* keepAliveMethod(void* ptr); ...@@ -47,20 +47,17 @@ void* keepAliveMethod(void* ptr);
int findPosDelimiter(const char *array, int len, int inicio) { int findPosDelimiter(const char *array, int len, int inicio) {
int i = -1; int i = -1;
int pos = 0; int pos = 0;
for (i = inicio; i<len; i++) { for (i = inicio; i<len; i++){
if (sizeof(delimiter)-1 == pos) { if (sizeof(delimiter)-1 == pos)
if (array[i] == delimiter[pos]) { if (array[i] == delimiter[pos])
return i - sizeof(delimiter) + 2; return i - sizeof(delimiter) + 2;
} else { else
pos=0; pos=0;
} else
} else { if (array[i] == delimiter[pos])
if (array[i] == delimiter[pos]) {
pos++; pos++;
} else { else
pos=0; pos=0;
}
}
} }
return -1; return -1;
} }
...@@ -94,66 +91,56 @@ void TCP(const char* HOST){ ...@@ -94,66 +91,56 @@ void TCP(const char* HOST){
char buf[MAXLEN]; // se almacenan los datos TCP recibidos en cada recv 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 char recibido[MAXLEN]; // mantiene el historico de datos recibidos, quitando los frames ya dibujados
int fin = 0; // Mantiene el indice del array recibido. int fin = 0; // Mantiene el indice del array recibido.
double fpms = 1000/30; //frames for ms
int received_data_size = 1; int received_data_size = 1;
while(received_data_size > 0) while(received_data_size > 0)
{ {
//primitiva RECEIVE
int data_size = MAXLEN; int data_size = MAXLEN;
received_data_size = recv(client_socket, buf, data_size, 0); received_data_size = recv(client_socket, buf, data_size, 0);
fin = printFrame( received_data_size, recibido, fin, buf);
}
printf("Datos recibidos: %d\n", received_data_size); close(client_socket);
freeaddrinfo(res);
// 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;
int inicio = 0;
int found = -1;
while ((found = findPosDelimiter(recibido, fin+1, inicio)) != -1) {
char jpg[found-inicio];
for (int h = 0; h < found-inicio; h++) {
jpg[h] = recibido[inicio+h];
}
printf("Nuevo jpeg size: %li\n", sizeof(jpg));
namedWindow("cliente", CV_WINDOW_AUTOSIZE);
Mat rawData = Mat(1, found, CV_8UC1, jpg);
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
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;
}
int printFrame(int received_data_size, char* recibido, int fin, char* buf){
double fpms = 1000/30; //frames for ms
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 += received_data_size;
int inicio = 0;
int found = -1;
while ((found = findPosDelimiter(recibido, fin+1, inicio)) != -1) {
char jpg[found-inicio];
for (int h = 0; h < found-inicio; h++)
jpg[h] = recibido[inicio+h];
printf("Nuevo jpeg size: %li\n", sizeof(jpg));
namedWindow("cliente", CV_WINDOW_AUTOSIZE);
Mat rawData = Mat(1, found, CV_8UC1, jpg);
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
imshow("cliente", frame);
waitKey(fpms);
inicio = found + sizeof(delimiter)-1;
} }
close(client_socket); if (inicio != 0) {
freeaddrinfo(res); // 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 -= inicio;
}
return fin;
} }
void UDP(char* HOSTNAME){ void UDP(char* HOSTNAME){
unsigned int length; unsigned int length;
struct from; struct from;
...@@ -196,15 +183,29 @@ void UDP(char* HOSTNAME){ ...@@ -196,15 +183,29 @@ void UDP(char* HOSTNAME){
char* frameBuffer = new char[100]; char* frameBuffer = new char[100];
while (true)
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.
int received_data_size = 1;
while(received_data_size > 0)
{ {
memset ( frameBuffer , '\0' , 100 ); received_data_size = recvfrom( sock , buf , MAXLEN , 0 , (struct sockaddr *)&from , &length );
if ( recvfrom( sock , frameBuffer , 100 , 0 , (struct sockaddr *)&from , &length ) < 0) fin = printFrame( received_data_size, recibido, fin, buf);
error("recv from UDP frames");
printf("%s\n", frameBuffer);
} }
//while (true)
//{
// memset ( frameBuffer , '\0' , 100 );
// if ( recvfrom( sock , frameBuffer , 100 , 0 , (struct sockaddr *)&from , &length ) < 0)
// error("recv from UDP frames");
// printf("%s\n", frameBuffer);
//}
close(sock); close(sock);
} }
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#define UDP_PORT 9998 #define UDP_PORT 9998
#define TCP_PORT 9999 #define TCP_PORT 9999
int printFrame(int received_data_size, char* recibido, int fin, char* buf);
void TCP(const char* HOST); void TCP(const char* HOST);
void UDP(char* HOSTNAME); void UDP(char* HOSTNAME);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment