/*** * * Connect to machine "scg.math" at port 8080, send one string at a time * to the server, terminate when "quit" is entered. * **/ #include #include #include #include #include #include #include #include #include #define PORTNUM 8080 #define BACKLOG 5 #define BUFLEN 80 #define FNFSTR "404 Error File Not Found " #define BRSTR "Bad Request " int main(int argc, char *argv[]) { struct sockaddr_in own_addr, party_addr; struct hostent *hp; int sockfd, newsockfd, filefd; int party_len; char buf[BUFLEN]; int len; int i; /* create socket */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("can't create socket\n"); return 0; } /* bind socket */ memset(&party_addr, 0, sizeof(party_addr)); hp = gethostbyname("scg.math.uwaterloo.ca"); if (!hp) { printf("Unknown host\n"); return 0; } party_addr.sin_family = AF_INET; memcpy(&party_addr.sin_addr.s_addr, hp->h_addr, hp->h_length); party_addr.sin_port = htons(PORTNUM); /* start requesting for connection */ if (connect(sockfd, (struct sockaddr *)&party_addr, sizeof(party_addr)) < 0) { printf("can't connect socket!"); return 0; } printf("Enter line:\n"); while (fgets(buf, BUFLEN, stdin)) { if (buf[strlen(buf)-1] == '\n') { buf[strlen(buf)-1] = 0; } len = strlen(buf); if (send(sockfd, buf, len, 0) < 0) { printf("error writing socket!"); return 0; } if (!strcmp(buf,"quit")) { printf("Done!\n"); return 0; } } }