/* tcp.slave.c SJ */ /* gcc -Wall -o tcp.slave tcp.slave.c -lnsl -lsocket -lresolv */ #include #include #include #include #include #include #include #include #include #include void sendall(int sock, char *buf); void receiveandprintall(int sock); int waitconnect(int port); int main(int argc, char **argv) { int j; int numbytes, count; char buf[1000]; char mesg2[100]; char hname[100]; struct sockaddr_in remote_addr, my_addr; int sock, newsock; int sin_size; char yes='1'; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (gethostname(hname, 99) == -1) { perror("gethostname"); strcpy(hname, "???"); } my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(atoi(argv[1])); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct /* bind to the port */ if (bind(sock, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } /* open port for connections */ if (listen(sock, 2) == -1) { perror("listen"); exit(1); } while(1) { sin_size = sizeof(struct sockaddr_in); /* accept new connection */ if ((newsock = accept(sock, (struct sockaddr *)&remote_addr, &sin_size)) == -1) { perror("accept"); continue; } printf("got connection from %s\n",inet_ntoa(remote_addr.sin_addr)); if ((numbytes=recv(newsock, buf, 999, 0)) == -1) { perror("recv"); close(newsock); continue; } buf[numbytes] = '\0'; /* terminate string */ count = atoi(buf); /* convert to int */ puts(buf); if (count < 1) { close(newsock); break; } sprintf(mesg2, "Hello from %s(pid=%d)\n", hname, getpid()); for (j = 0; j < count; j++) { sendall(newsock, mesg2); } close(newsock); } close(sock); exit(0); } void sendall(int sock, char *buf) { int len, bs; len = strlen(buf); while (len > 0) { bs = send(sock, buf, len, 0); if (bs == -1) { perror("send"); return; } else if (bs == 0) { printf("Connection closed prematurely\n"); return; } len -= bs; } } /* sendall() */ void receiveandprintall(int sock) { int numbytes; char buf[1000]; while ((numbytes = recv(sock, buf, 1000-1, 0))) { if (numbytes == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; /* terminate string */ fputs(buf, stdout); } close(sock); } /* returns socket */ int waitconnect(int port) { struct sockaddr_in remote_addr, my_addr; int sock, newsock; int sin_size; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(port); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct /* bind to the port */ if (bind(sock, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } /* open port for connections */ if (listen(sock, 2) == -1) { perror("listen"); exit(1); } sin_size = sizeof(struct sockaddr_in); /* accept new connection */ if ((newsock = accept(sock, (struct sockaddr *)&remote_addr, &sin_size)) == -1) { perror("accept"); } /* stop listening */ close(sock); return newsock; }