Skip to content
Snippets Groups Projects
Commit 6fc503cf authored by Leonardo's avatar Leonardo
Browse files

Mergie a mano lo de todo en single file y lo de manejo errores

parent c8071e84
Branches soingle-file-client
No related tags found
1 merge request!4Single file y manejo errores
......@@ -78,10 +78,13 @@ void TCP(const char* HOST){
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(HOST, STR(TCP_PORT), &hints, &res);
//primitiva CONNECT
connect(client_socket, res->ai_addr, res->ai_addrlen);
printf("Intentando conexion con %s:%d ...\n",HOST,TCP_PORT);
printf("Intentando conexion con %s:%d\n",HOST,TCP_PORT);
//primitiva CONNECT
if (connect(client_socket, res->ai_addr, res->ai_addrlen)<0){
printf("Error, imposible establecer conexion con el servidor.\n");
return;
}
//primitiva SEND
char msg[4];
......@@ -91,11 +94,10 @@ void TCP(const char* HOST){
int msg_size = strlen(msg);
int sent_msg_size = send(client_socket, msg, msg_size, 0);
if (sent_msg_size == -1) {
printf("ERROR: imposible establecer conexion con el servidor");
printf("Error, imposible establecer conexion con el servidor.\n");
return;
}
printf("Conexion establecida con %s:%d\n",HOST,TCP_PORT);
printf("Conexion establecida.\n");
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
......@@ -161,15 +163,28 @@ void UDP(char* HOSTNAME){
sprintf(buffer, "subscription");
// ENVIO PEDIDO DE SUSCRIPCION
printf("Sending Subscription\n");
printf("Enviando Suscripcion a %s:%d ...\n", HOSTNAME, UDP_PORT); ::fflush(stdout);
if (sendDatagramUDPtoHostname(sock, HOSTNAME, buffer, UDP_PORT) < 0)
error("Error on send first datagram");
// ESPERO RESPUESTA DEL SERVIDOR
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
bzero(buffer,256);
struct sockaddr_in from;
if (recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length) < 0)
error("recvfrom");
if (recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length) < 0){
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)){
printf("Error, no hay respuesta del servidor.\n");
return;
}else{
printf("Error al recibir datos.\n");
return;
}
}else
printf("Listo");
printf("Subscription: %s\n", buffer);
char* keepAliveId = new char[3];
......@@ -194,10 +209,20 @@ void UDP(char* HOSTNAME){
int fin = 0; // Mantiene el indice del array recibido.
int received_data_size = 1;
while(received_data_size > 0)
while(true)
{
received_data_size = recvfrom( sock , buf , MAXLEN , 0 , (struct sockaddr *)&from , &length );
if (received_data_size < 1){
if ((received_data_size < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))){
printf("Error, no hay respuesta del servidor.\n");
return;
}else{
printf("Error al recibir datos.\n");
return;
}
}else
fin = printFrame( received_data_size, recibido, fin, buf);
}
//while (true)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment