OK, so here's the deal.
I'm trying to create 2 programs, a server and a client, which communicates between them. I'm working on Ubuntu, throughout FIFO.
So, 1st of all, I create 2 FIFO's:
$ mkfifo pipe
$ mkfifo pipo
Ok, and after that I create the client and the server programs.
My objective is that, when running both programs (in two separate terminals) when I enter an input from the client's terminal, the server would take that input and use it to get some informations. Now my client sends a directory name, and the server will perform a ls operation and than returns to the client a list with the result of the ls command, or an error message otherwise.
But my problem arise when, having the information, as a line, to store it into a list. Basically, I need to store a line which I get out of the fgets() funtion to an array of strings/chars, but due to the fact that I mostly code in C++, getting back in C is really hard for me (and no, I can't do it in C++).
Ok, enough with the chichat, here's what I got till now.
The Client.c program:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define _GNU_SOURCE
int main(){
FILE* fd;
char* line=NULL;
size_t len=0;
ssize_t read1;
int pipe, pipo;
char s[256]="";
char d[10240]="";
printf("Folder name: ");
scanf("%255s", s);
pipe=open("pipe", O_WRONLY);
pipo=open("pipo", O_RDONLY);
write(pipe, &s, sizeof(s));
printf("Client write done.\n");
read(pipo, &d, sizeof(d));
printf("Client read done.\n");
close(pipe);
close(pipo);
// fd =fopen("/home/b.txt", "r");
// while((read1 = getline(&line, &len, fd)) != -1){
// printf("%s", line);}
// free(line);
return (0);
}
And the Server.c program:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(){
int pipe, pipo;
int k;
char *p;
pipe=open("pipe", O_RDONLY);
pipo=open("pipo", O_WRONLY);
char *lss[128];
for(k=0;k<128;k++){
lss[k]=malloc(sizeof(char)*20);
}
p=&lss[0];
char s[256];
char out[10240];
char buf[256];
char* ls="ls /";
int i=0;
read(pipe, &s, sizeof(s));
snprintf(buf, sizeof (buf), "%s%s", ls, s);
FILE* f;
f=popen(buf, "r");
if (f!=NULL){
while(fgets(out, sizeof(out), f) != NULL){
// fputs(out, lss[i]);
// i++;
}
}
else{
printf("F is NULL.\n");}
pclose(f);
write(pipo, &out, sizeof(out));
for (;i==0;i--){
printf("%s\n", lss[i]);
}
close(pipe);
close(pipo);
for (k=0;k<128;k++){
free(lss[k]);
}
return (0);
}
Ok, but my problem is here
if (f!=NULL){
while(fgets(out, sizeof(out), f) != NULL){
// fputs(out, lss[i]);
// i++;
}
I can't figure it out how to get the string from the out variable into the lss array, because out holds a string, or sorta.
Can any1 help me?