By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,371 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,371 IT Pros & Developers. It's quick & easy.

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

P: 1
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
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
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.