please help a problem in client-server ipc message 2 pipes communication simple example


 
Thread Tools Search this Thread
Top Forums Programming please help a problem in client-server ipc message 2 pipes communication simple example
# 1  
Old 06-09-2011
please help a problem in client-server ipc message 2 pipes communication simple example

I want to have a message send & receive through 2 half-duplex pipes

Flow of data

top half pipe
Code:
stdin--->parent(client) fd1[1]--->pipe1-->child(server) fd1[0]

bottom half pipe
Code:
child(server) fd2[1]---->pipe2--->parent(client) fd2[0]--->stdout

I need to have boundary structed message mesg_len+mesg_type+mesg_data



Code:
#include <stdio.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <fcntl.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/wait.h> 
 
#define MAX_BUF 100 
#define MAXMESGDATA (MAX_BUF - 2* sizeof(long)) 
#define MESGHDRSIZE (sizeof(struct mymesg)-MAXMESGDATA) 
 
struct mymesg{ 
    long mesg_len; //byte in mesg_data 
    long mesg_type; //message type 
    char mesg_data[MAXMESGDATA]; 
}; 
 
ssize_t mesg_send(int,struct mymesg *); 
ssize_t mesg_recv(int,struct mymesg *); 
void client (int,int),server(int,int); 
 
int main(int argc, char ** argv){ 
    //MAXMESGDATA== 92 bytes 
    //sizeof(struct mymesg)== 100 bytes 
    //2* sizeof(long)== 8 bytes 
    //MESGHDRSIZE ==8 bytes 
 
    int pipe1[2],pipe2[2]; 
    pid_t childpid; 
    pipe(pipe1); //create 2 pipes 
    pipe(pipe2); 
 
    if ((childpid=fork())==0) { //child 
        close(pipe1[1]); 
        close(pipe2[0]); 
        server(pipe1[0],pipe2[1]); 
        exit(0); 
    } 
    //parent 
    close(pipe1[0]); 
    close(pipe2[1]); 
    client(pipe1[1],pipe2[0]); 
    waitpid (childpid,NULL,0); 
    return EXIT_SUCCESS; 
}


Last edited by ouou; 06-09-2011 at 06:25 PM.. Reason: please use code tags for code, thanks
# 2  
Old 06-09-2011
You've got the parameters for client() wrong. You're giving it a readfd which is the write-end of the wrong pipe, and a writefd which is the read-end of the other wrong pipe. Try client(pipe2[0], pipe1[1]);

Also keep in mind that the message from the child only gets flushed because it quits. Pipes have a long buffer. If you try to wait for a message from the client when it hasn't quit, you could just block forever. (Leaving in the newlines might help.)
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

kill() function problem in client-server ipc message using 2 FIFOs

I want to have a message send & receive through 2 uni-direction FIFO Flow of data FIFO1 stdin--->parent(client) writefd--->FIFO1-->child(server) readfd FIFO2 child(server) writefd2---->FIFO2--->parent(client) readfd2--->stdout I need to have boundary structed message... (3 Replies)
Discussion started by: ouou
3 Replies

2. Programming

IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process... (5 Replies)
Discussion started by: pharaoh
5 Replies

3. Programming

IPC between processes, pipes, etc

I need help with understanding this in C-programming style(the systemcalls only): Three processes communicates via two pipes. (when the processes creates all stdin is the keyboard and all stdout is the screen) This is how the communication goes: Process 2 stdin (keyboard) and stdout goes via... (3 Replies)
Discussion started by: oskis
3 Replies

4. Programming

Client server communication using FIFO.

Hiii..... I need a client server communication using a FIFO. Sever is contacted by multiple clients.Each client writes its request to a FIFO.The server replies back to the client through a client specific FIFO. give any link to sample FIFO programs.......... Thanking you KRISH:cool: (1 Reply)
Discussion started by: krishnampkkm
1 Replies

5. Programming

C program using IPC (inter process communication)

i want to write a C chat program that communicates over IPC(inter process communication), that could be run using 2 seperate terminal windows within the same computer. so that wat u type in one terminal window , should appear on the other and vice versa... could some one please help me with the... (2 Replies)
Discussion started by: localp
2 Replies

6. Programming

client /server pipes

here is the concept: the client reads a pathname from the standard input and writes it to pipe1.The server reads this pathname from the pipe1 and tries to open the file for reading.If the server can open the file ,the server responds by reading the file and writting it to pipe2;otherwise the... (2 Replies)
Discussion started by: tolkki
2 Replies

7. UNIX for Advanced & Expert Users

Inter-process communication:pipes,doors,etc.

Hi, I am thinking about writing a log daemon for a multi-processed ksh application (yes - I know that high-level language would be a better option). My question is as follows: If many processes (many scripts) will try writing to a single log file: print "message" > common.log Will it work or... (2 Replies)
Discussion started by: adderek
2 Replies

8. UNIX for Dummies Questions & Answers

socket programming : client server IPC

I'm new to socket programming. Have a basic doubt. I have a structure(global) at the server side. I have two different client connecting to the server. Will the changes made by one client on the structure be visible to the other client when it accesses the same client? I'm creating a STREAM... (3 Replies)
Discussion started by: abc.working
3 Replies

9. Cybersecurity

client-server message transfer

using fork().how do v send and receive messages in child and parent process. (2 Replies)
Discussion started by: krishnavel
2 Replies

10. UNIX for Advanced & Expert Users

Interprocess communication using pipes and fork

I'm very worried. I have an assignment that is due in 3 weeks, and also tute exercises which I can't seem to understand and work out. Okay, the question: The parent process will convert the command arguments into integer values using atoi() and store them into an integer array which you will... (2 Replies)
Discussion started by: scmay
2 Replies
Login or Register to Ask a Question