Skip to content
Snippets Groups Projects
Commit 3d49363a authored by Alvaro Liber Vallve Maidana's avatar Alvaro Liber Vallve Maidana
Browse files

Se incluye funcion de ejemplo que escucha cierto puerto TCP.

Luego de recibir el flujo de bytes, busca frames separado por delimitador y dibuja en pantalla.
parent 69db1ec9
No related branches found
No related tags found
No related merge requests found
......@@ -19,11 +19,18 @@ using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#define BUFLEN 1024*1024 //Max length of buffer
#define BUFLEN 1048576 //1024*1024 //Max length of buffer
#define MAX_QUEUE 10
#define PORT 8890 //The port on which to listen for incoming data
int pruebaRecibirUDP();
int pruebaRecibirTCP();
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
pruebaRecibirTCP();
return 0;
}
......@@ -100,3 +107,124 @@ int pruebaRecibirUDP(){
close(s);
return 0;
}
int findPosDelimiter(const char *array, int len, char * delimiter, 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;
}
int pruebaRecibirTCP() {
char buf[BUFLEN];
//primitiva SOCKET
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
//primitiva BIND
struct sockaddr_in server_addr;
socklen_t server_addr_size = sizeof server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(
server_socket,
(struct sockaddr*)&server_addr, server_addr_size
);
cout << "Waiting for connection..." << endl;
//primitiva LISTEN
listen(server_socket, MAX_QUEUE);
//primitiva ACCEPT
struct sockaddr_in client_addr;
socklen_t client_addr_size = sizeof client_addr;
int socket_to_client = accept(
server_socket,
(struct sockaddr *)&client_addr, &client_addr_size
);
char delimiter[] = "Grupo25";
char recibido[BUFLEN];
int fin = 0; // Mantiene el indice de los bytes recibidos.
int received_data_size = 1;
while(received_data_size > 0)
{
//primitiva RECEIVE
int data_size = BUFLEN;
received_data_size = recv(socket_to_client, buf, data_size, 0);
//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));
cout << "Datos recibidos: " << received_data_size << endl;
// 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;
//cout << "fin: " << fin << endl;
int inicio = 0;
int found = -1;
while ((found = findPosDelimiter(recibido, fin+1, delimiter, inicio)) != -1) {
char jpg[found-inicio];
//cout << "encontre delimitador en pos: " << found << endl;
for (int h = 0; h < found-inicio; h++) {
jpg[h] = recibido[inicio+h];
}
cout << "nuevo jpg size: " << sizeof(jpg) << endl;
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(1000/30);
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;
//cout << "nuevo fin: " << fin << endl;
}
}
//primitiva CLOSE
//close(socket_to_client);
close(server_socket);
return 0;
}
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