#include #include #include #include #include #include #include #include // receives results from the workers and displays them on the console // usage: // a.out portno // number of the port for the result socket #define MAXHOSTNAME 80 int main(int argc, char * argv[]) { int s,si, retcode, i; unsigned int salen; struct sockaddr_in sa,sai; char hostname[MAXHOSTNAME]; si = socket(AF_INET,SOCK_STREAM,0); // socket to receive the results if(si == -1) { perror("while opening socket"); return -1;} sai.sin_family = AF_INET; sai.sin_port = htons(atoi(argv[1])); gethostname(hostname,MAXHOSTNAME); memcpy(&sai.sin_addr, (gethostbyname(hostname)->h_addr), sizeof(sa.sin_addr)); retcode = bind(si,(struct sockaddr *) & sai, sizeof(sai)); if(retcode == -1) { perror("while binding socket to addr"); return -1; } retcode = listen(si,1); if(retcode == -1) { perror("while calling listen"); return -1; } while(1==1) { salen = sizeof(sa); printf("Accepting connections\n"); s = accept(si,(struct sockaddr *)&sa,&salen); // accept a connection if(s == 1) { perror("while accepting a connection"); return -1; } retcode = read(s,&i,sizeof(int));// read a res from one of the Worker if(retcode == -1) { perror("while reading a result"); return -1; } if(i==(-1)) { printf("EOS -> terminating\n"); break; } printf("Read result: %d ",i); fflush(stdout); // and print it on console close(s); } close(si); return 0; }