I'm having a little problem with my programs and I don't know how to solve it. My programs work fine as long as I don't include getopt
(without arguments).
I wrote 2 simple programs Server&Client
The Server writes numbers in stdout
and the Client should read them with stdin
.
Furthermore the programs should be able to get called with arguments(parameters).
For example a call of the program looks like that:
Terminal: ./Server -f72 | ./Client -r22
If I run the program then it just prints the "Her is r" with the argument "22" and "Waiting for Server", but it should print the numbers from the server
I am not able to find the mistake. I would really appreciate your help. Thanks in advance to all who read that.
Server:
int main(int argc , char *argv[])
{
int i;
FILE *ziel;
ziel=stdout;
int option;
char buf[50];
while ((option = getopt(argc, argv,"f:tr:d:j")) != -1)
{
switch (option)
{
case 'f' :
strcpy(buf,optarg);
break;
case 't' :
break;
case 'r' :
strcpy(buf,optarg);
break;
default:
//printf("1ERROR");
exit(EXIT_FAILURE);
}
}
for(i=1;i>0;i++)
{
fprintf(ziel,"%d\n",i);
sleep(1);
}
return 0;
}
Client:
int main(int argc , char *argv[])
{
FILE* cl;
cl=stdin;
int i;
int zahl[10];
int option;
char buf[50];
while ((option = getopt(argc, argv,"f:tr:")) != -1)
{
switch (option)
{
case 'f' :
puts("Here is f");
strcpy(buf,optarg);
puts(buf);
break;
case 't' :
puts("Here is t");
break;
case 'r' :
puts("Here is r");
strcpy(buf,optarg);
puts(buf);
break;
default:
printf("ERROR");
exit(EXIT_FAILURE);
}
}
puts("Waiting for Server");
for(i=0;i<10;i++)
{
fscanf(cl,"%d",&zahl[i]);
printf("Received %d\n ",zahl[i]);
}
return 0;
}