I'm making a client-server model in C using FIFOs and even if I see that all the code is right I don't get the expected result. The model works like so: the client writes a file name, which is written in a FIFO for the server to read (the expected answer from the server is the first line of the file, if the file doesn't exist an empty string is sent back to the client). Along this, the client sends a name for an specific FIFO, in this second FIFO is where the server is going to write the line. One unique FIFO is used to send file names from various clients to the server but every client has it's own FIFO to recieve the data.
Here is the code for the client:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define BUFSIZE 512
struct sent {
char *name;
char *fifo;
};
int main()
{
char name[BUFSIZE];
char recieved[BUFSIZE];
int client_server_fifo;
char *cs_fifo = "cs_fifo";
int server_client_fifo;
char *sc_fifo;
sprintf(sc_fifo, "sc_fifo_%d", getpid());
struct sent *sent = malloc(sizeof(struct sent));
mkfifo(sc_fifo, 0777);
while(1) {
printf("Write the name of the file: ");
scanf("%s", name);
printf("1111\n");
client_server_fifo = open(cs_fifo, O_WRONLY);
printf("2222\n");
printf("%s", name);
printf("%s", cs_fifo);
sent->name = name;
sent->fifo = cs_fifo;
printf("%s", name);
printf("%s", cs_fifo);
write(client_server_fifo, sent, sizeof(*sent));
server_client_fifo = open(sc_fifo, O_RDONLY);
if (read(server_client_fifo, recieved, sizeof(recieved)) == -1) {
printf("An error ocurred.\n");
} else {
printf("First line of the file: \n%s\n", recieved);
close(client_server_fifo);
close(server_client_fifo);
}
memset(recieved, 0, sizeof(recieved));
}
return 0;
}
And here's the server:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define BUFSIZE 512
struct sent {
char *name;
char *fifo;
};
int main()
{
int client_server_fifo;
char *cs_fifo = "cs_fifo";
int server_client_fifo;
char *sc_fifo;
struct sent *sent = malloc(sizeof(struct sent));
char *name;
char *line;
FILE *file;
printf("Server running...\n");
mkfifo(cs_fifo, 0777);
while (1)
{
client_server_fifo = open(cs_fifo, O_RDONLY);
printf("1111\n");
read(client_server_fifo, sent, sizeof(*sent));
printf("2222\n");
name = sent->name;
printf("%s\n", name);
sc_fifo = sent->fifo;
printf("%s\n", sc_fifo);
printf("%s", name);
printf("%s", sc_fifo);
server_client_fifo = open(sc_fifo, O_WRONLY);
if (strcmp(izena, "exit") == 0) {
printf("Closing session...");
break;
} else {
if((file = fopen(name, "r")) != NULL) {
fgets(line, BUFSIZE, file);
}
}
printf("Reached this point\n");
write(server_client_fifo, line, strlen(line));
memset(name, 0, sizeof(name));
memset(line, 0, sizeof(line));
}
close(client_server_fifo);
close(server_client_fifo);
unlink(cs_fifo);
unlink(sc_fifo);
return 0;
}
I've translated the code from Basque to English so there may be some mistakes.
Also, as you can see I put some printfs to see where my code reaches but in the case of the client it prints the 1111 one, seems like opens the fifo and does the 2222 print and here it hangs. In the server the same happens, prints the 1111 and the 2222 but nothing more, and then segmentation fault.
Any idea on why this happens?
And do I need to fork() in the server? Or doing everything in the infinite loop works aswell?
Thanks.