/*------------------------------------------------------------------ * itzserv.c : itz server for COEN146 lab 6 * * January 2002, Your Name * * *------------------------------------------------------------------ * $Log$ *------------------------------------------------------------------ * $Endlog$ */ #include #include #include #include #include #include #include #include #include #define MY_PORT 8016 #define BACK_LOG 10 #define MAX_DATA_SIZE 100 #define MAX_BUFFER_SIZE 500 #define TRUE 1 /* Prototypes are declared here */ void fill_array(); int reverse(char *); char *int_to_string(int); char *city[100]; /* an array of character pointers */ int itz[100]; /* an array of integers */ int i, v, t; /* i, v, or t holds size of city array */ char aster[1]; char tempcity[20]; char buf[MAX_DATA_SIZE]; int main() { int sd; /* socket sd to listen on */ int new_sd; /* socket to communicate with */ int con; /* continue the while loop */ int x, numbytes; /* Add more declaration here */ struct sockaddr_in my_addr; /* my address information */ struct sockaddr_in their_addr; /* connector's address info */ int sin_size; fill_array(); /* now the city and itz arrays have values */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit (1); } /* New we have a socket connection for listening */ my_addr.sin_family = AF_INET; /* host byte order */ my_addr.sin_port = htons(MY_PORT); /* short, network byte order */ my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ for (x = 0; x < 7; x++) my_addr.sin_zero[x] = '0'; if (bind(sd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit (1); } if (listen(sd, BACK_LOG) == -1) { perror("Listen"); exit(1); } /* And our socket is now successfully listening */ con=1; while (con==1) { sin_size = sizeof(struct sockaddr_in); printf("listening\n"); if ((new_sd = accept(sd, (struct sockaddr*)&their_addr, &sin_size)) == -1) { perror("accept"); continue; } printf("accepted\n"); /* at this point we have received and accepted a connection request */ printf("Server: got connection from %s\n", inet_ntoa(their_addr.sin_addr)); /* * At this point, if we were expecting more connections, * we would fork a child process to handle the interaction, * but we're not going to be that complicated! */ /* if (send(new_sd, "Hello, Internet world!\n", 23, 0) == -1) { perror("send"); } */ /* We're sending a message back to the connecting process */ /* * Get the city name from client use recv() */ if ((numbytes = recv(new_sd, buf, MAX_DATA_SIZE, 0)) == -1) { perror("recv"); exit (1); } /* * Check if cityname is "*" * Yes, close the connection and stop the process. */ buf[numbytes] = '\0'; aster[0]='*'; if (buf[0]==aster[0]) { con=0; send(new_sd, "Connection closed.\n", 19, 0); } /* * look for table see if it's in the list * */ for(v=0; v 0) { city[i] = (char *)malloc(strlen(cityin) + 1); strcpy(city[i], cityin); itz[i] = itzin; i++; } /* * You can pull this for loop out if you like. All * it does is print out the contents of the city and * its arrays. But you might want it for debugging * purposes. */ for (j = 0; j < i; j++) printf(" %s %d\n", city[j], itz[j]); } /* * reverse () * * Description: * This routine is used to reverse a string. * * Parameters: * string - Pointer to the string * * Returns: * int 0 */ int reverse (char *string) { char *lp = string; /* left pointer */ char *rp = &string[strlen(string)-1]; /* right pointer */ char tmp; while(lp < rp) { tmp = *lp; *lp = *rp; *rp = tmp; lp++; rp--; } return (0); } /* * int_to_string () * * Description: * This routine is used to convert an integer to a string. * * Parameters: * a - int integer * * Returns: * s - Pointer to a string */ char *int_to_string (int a) { char *s; int i = 0; while( a > 0 ) { s[i] = (a % 10) + '0'; a /= 10; i++; } s[i] = '\0'; reverse(s); return (s); }