/*------------------------------------------------------------------ * inetclnt.c : inet client for COEN146 lab 6 * * January 2002, Your Name * * *------------------------------------------------------------------ * $Log$ *------------------------------------------------------------------ * $Endlog$ */ #include #include #include #include #include #include #include #include #define PORT 8016 #define MAX_DATA_SIZE 100 #define CITY_NAME_SIZE 20 #define TRUE 1 int main(int argc, char *argv[]) { int csd; /* socket descriptor for the client */ int con; /* continue the while loop */ /* Add cityname declaration here char array with size CITY_NAME_SIZE */ char cityname[CITY_NAME_SIZE]; char aster[CITY_NAME_SIZE]; int numbytes, x; char buf[MAX_DATA_SIZE]; struct hostent *he; struct sockaddr_in their_addr; /* connector's (client's) info */ if (argc != 2) { printf("usage: client hostname\n"); exit (1); } if ((he = gethostbyname(argv[1])) == NULL) { perror("gethostbyname"); exit (1); } /* * Add while loop here to allow user enter city name forever * until "*" typed to terminate */ con=1; while(con==1) { /* now the user has invoked te program correctly */ if ((csd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit (1); } /* we successfully got a socket descriptor */ their_addr.sin_family = AF_INET; their_addr.sin_port = htons(PORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); /* bzero(&their_addr.sin_zero), 8); */ for (x = 0; x < 7; x++) { their_addr.sin_zero[x] = '0'; } if (connect(csd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1) { perror("connect"); exit (1); } /* Add prompt here */ printf("Enter the name of the city: "); /* Read cityname here */ scanf("%s", cityname); /* if user entered "*", send "*" to server, * then close connection. */ aster[0]='*'; if (cityname[0]==aster[0]) con=0; /* otherise, send the cityname to server * and let server handle the cityname * * Receive the corresponding itz * print the cityname and itz here * */ if (send(csd, cityname, strlen(cityname), 0) == -1) { perror("send"); } if ((numbytes = recv(csd, buf, MAX_DATA_SIZE, 0)) == -1) { perror("recv"); exit (1); } buf[numbytes] = '\0'; printf("Received: %s \n", buf); } close(csd); return 0; }