Dave
Dave

Reputation: 2018

How to send properly data in fifo server

I am trying to create a simple fifo client/server. Whenever I compile the program and try to type some data in client, in server I am getting weird outputs such as: enter image description here How can I fix this?

server:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd;
    mkfifo("/tmp/my_fifo", 0666);
    for(;;){
        fd = open("/tmp/my_fifo", O_RDONLY);
        int d;
        char buf[64];
        read(fd, buf, sizeof(buf));
        sscanf(buf, "%d", &d);
        close(fd);
    }
    return 0;
}

client:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(){
    int fd;
    fd = open("/tmp/my_fifo", O_WRONLY);
    int d;
    scanf("%d", &d);
    char buf[32];
    sprintf(buf, "%d", d);
    write(fd, buf,strlen(buf));
    close(fd);
    return 0;
}

Upvotes: 0

Views: 105

Answers (0)

Related Questions