Skip to content
Snippets Groups Projects
Commit 8a1d6cc7 authored by Leonardo's avatar Leonardo
Browse files

Manejo de error al conectar

parent 1ce27bd7
No related branches found
No related tags found
1 merge request!2Manejo de error al conectar
......@@ -72,21 +72,25 @@ void TCP(const char* HOST){
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(HOST, STR(TCP_PORT), &hints, &res);
printf("Intentando conexion con %s:%d ...\n",HOST,TCP_PORT);
//primitiva CONNECT
connect(client_socket, res->ai_addr, res->ai_addrlen);
if (connect(client_socket, res->ai_addr, res->ai_addrlen)<0){
printf("Error, imposible establecer conexion con el servidor.\n");
return;
}
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);
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
......@@ -153,15 +157,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];
......@@ -189,10 +206,19 @@ 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 );
fin = printFrame( received_data_size, recibido, fin, buf);
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);
}
......
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