
tcpcli.c
Go to the documentation of this file.
00001 /* tcpcli.c */ 00002 /* linked with wrapsock */ 00003 /* Server module */ 00004 /* Author: Michèle Garoche */ 00005 /* Date: February 26th 2003 00006 /* Feel free to do what you want with it */ 00007 00029 /* Includes */ 00030 #include "wrapsock.h" 00031 00038 int main(int argc, char *argv[]) 00039 { 00040 /* Variables */ 00044 int sockfd; /* socket's file descriptors */ 00048 int chopt; /* dummy variable for options test */ 00052 int port_served = 0; /* port to connect */ 00056 int bflag = 0; /* dummy variable for number of addresses supplied */ 00060 char progname[MAXLINE]; /* program's name */ 00064 char addropt[MAXLINE]; /* bound address */ 00068 struct sockaddr_in servaddr; /* structure of socket address */ 00069 00070 /* Retrieve the program's name */ 00072 strcpy(progname, argv[0]); 00073 00074 /* The user should supplied an option for port and an address. 00075 As the first argument is the program name, we should have 00076 3 arguments */ 00078 if (argc != 3) 00079 { 00080 /* Display the help and quit */ 00081 cliusage(progname); 00082 } 00083 00084 /* Check for a valid option */ 00085 while ((chopt = getopt(argc, argv, "abch")) != -1) 00087 { 00088 switch (chopt) 00089 { 00090 case 'a': 00091 { 00092 port_served = 1; 00093 break; 00094 } 00095 case 'b': 00096 { 00097 port_served = 2; 00098 break; 00099 } 00100 case 'c': 00101 { 00102 port_served = 3; 00103 break; 00104 } 00105 case 'h': 00106 default: 00107 { 00108 /* Display help and quit */ 00109 cliusage(progname); 00110 } 00111 } 00112 } 00113 00114 /* Check the existence of the address 00115 optind is incremented each time an option is checked, 00116 hence if optind is equal to argc at the end of the 00117 previous function, it means the user has either not supplied an address 00118 or not supplied a port as a valid option */ 00120 if (optind == argc) 00121 { 00122 /* Display help and quit */ 00123 cliusage(progname); 00124 } 00125 else 00126 { 00127 /* Loop through the not already traversed arguments */ 00128 for (; optind < argc; optind ++) 00129 { 00130 /* Increments the dummy flag and store the address */ 00131 bflag++; 00132 strcpy(addropt, argv[optind]); 00133 } 00134 /* If there are more than one address, display help and quit */ 00135 if (bflag > 1) 00136 { 00137 cliusage(progname); 00138 } 00139 } 00140 00141 /* Compute the real port number */ 00142 port_served = port_served + SERV_PORT1 - 1; 00143 00144 /* Display help and quit if the argument for port is supplied without a dash before it */ 00146 if (port_served < SERV_PORT1 || port_served > SERV_PORT3) 00147 { 00148 cliusage(progname); 00149 } 00150 00151 /* Help message for the user */ 00153 fprintf(stdout, "\nThe client %s will be connected on address %s " 00154 "to port %d.\nIts pid is %d.\n\nDepending of the port you chose" 00155 ", you will be able to send requests\nfor computing numbers " 00156 "(b), converting words to lowercase (c), or both types\nof " 00157 "requests (a).\n\nTo Terminate the client, use ctrl-D.\n\nTo " 00158 "erase an entry before sending it, use ctrl-U\n(useful if the " 00159 "buffer is full).\n\nYou can connect as many clients as your " 00160 "system supports to the server.\n\nYou can also connect from " 00161 "a remote host (via ssh for example)." 00162 "\n\n", progname, addropt, port_served, (int)getpid()); 00163 00164 /* Create the socket */ 00166 sockfd = Socket(AF_INET, SOCK_STREAM, 0); 00167 00168 /* Fill in the address structure */ 00170 bzero(&servaddr, sizeof(servaddr)); 00171 servaddr.sin_family = AF_INET; 00172 servaddr.sin_port = htons(port_served); 00173 /* Converts the address supplied by the user into network byte order address 00174 if the address is incorrect, quit */ 00176 Inet_pton(AF_INET, addropt, &servaddr.sin_addr); 00177 00178 /* Connect the socket to address and port supplied */ 00180 Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); 00181 00182 /* Send the request to the socket */ 00184 str_cli(stdin, sockfd); 00185 00187 exit(EXIT_SUCCESS); 00188 }