471,049 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

c: Parse input command for main(int argc, char *argv[])

Hello,

I am new to c and need help with a current program I am working on.

Currently, the program (code below) input is as follows:

program apple.test.com 51112

What I need is (essentially three arguments with a ':' space between the hostname and port number, and a third argument):

program apple.test.com:51112 usr/bin/test

where:

apple.test.com is argv[1]
51112 is argv[2]
usr/bing/test is argv[3]

Thanks guys.

Here is a bit of the current client file which needs to be modified.
________________________________________

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];


// get proper number of input arguments before proceeding
if (argc != 3) {
fprintf(stderr,"usage %s <hostname> <port>\n", argv[0]);
exit(0);
}

portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
error("ERROR opening socket");


server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
Nov 12 '06 #1
1 10741
horace1
1,510 Expert 1GB
run time parameters to main() are seperated by spaces, i.e. running the program
program apple.test.com:51112 usr/bin/test

gives

apple.test.com:51112 is argv[1]
usr/bin/test is argv[2]

you can then use function strstr() in <string.h> to extract apple.test.com and 51112 from argv[1], see
http://www.cplusplus.com/ref/cstring/strstr.html
Nov 13 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

19 posts views Thread by Steven T. Hatton | last post: by
13 posts views Thread by Sokar | last post: by
3 posts views Thread by Bill Cunningham | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.