Skip to content

Commit

Permalink
SIGPIPE gives -1. UPD no losses
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-Maria-Janeiro committed Apr 14, 2020
1 parent 20ea1e9 commit 938ce7f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Binary file modified main
Binary file not shown.
15 changes: 10 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ int main(int argc, char *argv[]) {
}
udp_main = connectToUdpServer(connectIp, connectPort);

sprintf(buff, "EFND %d\n", myServer->myKey);
n=sendto(udp_main->fd,buff,strlen(buff),0, udp_main->res->ai_addr, udp_main->res->ai_addrlen);
if(n==-1) /*error*/ exit(1);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000; // 100 ms timeout
setsockopt(udp_main->fd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv));


n=recvfrom(udp_main->fd,buff,128,0, (struct sockaddr*)&(udp_main->res->ai_addr),&(udp_main->res->ai_addrlen));
if(n==-1) /*error*/ exit(1);
sprintf(buff, "EFND %d\n", myServer->myKey);
do { // If the answer from the server isn't received, send the request again
n=sendto(udp_main->fd,buff,strlen(buff),0, udp_main->res->ai_addr, udp_main->res->ai_addrlen);
if(n==-1) /*error*/ exit(1);
} while ((n=recvfrom(udp_main->fd,buff,128,0, (struct sockaddr*)&(udp_main->res->ai_addr),&(udp_main->res->ai_addrlen))) < 0);


sscanf(buff, "%s %d %d %s %s", aux, &(myServer->myKey), &(myServer->nextKey), myServer->nextIp, myServer->nextPort); // Get the successor details
Expand Down
23 changes: 19 additions & 4 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <math.h>
#include <sys/select.h>
#include <errno.h>
#include <signal.h>
#include "server.h"

#define N 16
Expand Down Expand Up @@ -72,6 +73,12 @@ void createServer(Server* server, int _onGoingOperation) {
int ongoingOperation = _onGoingOperation;
int serverInRing = 1;

struct sigaction act;

memset(&act,0,sizeof act);
act.sa_handler=SIG_IGN;
if(sigaction(SIGPIPE,&act,NULL)==-1)/*error*/exit(1);

while(1) {
// Set all sockets to 0
FD_ZERO(&rfds);
Expand Down Expand Up @@ -191,11 +198,18 @@ void createServer(Server* server, int _onGoingOperation) {
udp = connectToUdpServer(connectIp, connectPort);

sprintf(buffer, "EFND %d\n", server->myKey);
n=sendto(udp->fd,buffer,strlen(buffer),0, udp->res->ai_addr, udp->res->ai_addrlen);
if(n==-1) /*error*/ exit(1);

n=recvfrom(udp->fd,buffer,128,0, (struct sockaddr*)&(udp->res->ai_addr),&(udp->res->ai_addrlen));
if(n==-1) /*error*/ exit(1);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000; // 100 ms timeout
setsockopt(udp->fd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv));


sprintf(buffer, "EFND %d\n", server->myKey);
do { // If the answer from the server isn't received, send the request again
n=sendto(udp->fd,buffer,strlen(buffer),0, udp->res->ai_addr, udp->res->ai_addrlen);
if(n==-1) /*error*/ exit(1);
} while ((n = recvfrom(udp->fd,buffer,128,0, (struct sockaddr*)&(udp->res->ai_addr),&(udp->res->ai_addrlen))) < 0);


sscanf(buffer, "%s %d %d %s %s", str, &(server->myKey), &(server->nextKey), server->nextIp, server->nextPort); // Get the successor details
Expand Down Expand Up @@ -497,6 +511,7 @@ void tcpWrite(int toWriteFd, char * strToWrite) {
nwritten = write(toWriteFd, strToWrite, nleft);
if(nwritten <= 0) {
printf("\n\n MASSIVE ERROR \n\n");
break;
}
nleft-=nwritten;
strToWrite += nwritten;
Expand Down

0 comments on commit 938ce7f

Please sign in to comment.