C++ socket, fork & pipes


 
Thread Tools Search this Thread
Top Forums Programming C++ socket, fork & pipes
# 1  
Old 02-17-2012
C++ socket, fork & pipes

Hello, I'm stuck and this is a matter which I need to resolve quite fast (but I couldn't post in the "Emergency" section); the problem is this :

I have created a chat program in which the client sends the sentence to the server and then the server should send it to all the clients connected, which is done with a "for" and a vector of all new_fd connections. So I suppose (correct me if I'm wrong) the easiest way is that in the server I fork, the child process recieves the sentence; then sends it through a pipe to the parent process which in turn sends it to all clients. But for some reason the pipe doesn't work! In the parent process it starts "read"ing even before the writing has begun; and it reads nothing, no matter what I do it can't transmet data. Here is the code :

Code:
pid_t pid = fork();
char buffer[100];
int fd[2];
pipe(fd);
int nbytes;
char ready[4];
    if(pid==0) {

while(true) {
        close(sockfd);
        close(fd[0]);

        "recieve sentence through char* reception)"
        write(fd[1], "yes", 4);
        write(fd[1], reception, taille);
    }

    }
        else if (pid>0) {
            close(new_fd);
            close(fd[1]);
       nbytes = read(fd[0], ready, 4);
      cout<<ready<<endl;
      //ready couts NOTHING

       nbytes = read(fd[0], buffer, sizeof(buffer));

if(ready=="yes") {
  "send data to all clients"
            }
...

# 2  
Old 02-17-2012
You create the pipe after you fork. The pipe the child gets, and the pipe the parent gets, are created independently hence not the same pipe. There will be no communication between parent and child.

Create the pipe before the fork and you'll have the same pipe in both, connecting the two.

Remember that the copies are completely independent. Be very sure to close all ends of the pipe you're not using -- close the read-end in the parent, and the write-end in the child! Otherwise you may end up hanging when the child is done reading but, with a read-end left open in the parent, write() still doesn't fail.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-17-2012
Oh yes, I didn't see that. The solution was extremely simple, thanks for your clear answer!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about global environment variables & fork() exec()

Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)
Discussion started by: bodisha
2 Replies

2. Shell Programming and Scripting

Troubles with pipes, fork, and dup2

I want to execute metasploit by two pipes to communicate with it, but I have troubles with that communication. When I run my program, I get this error: "stty: standard input: Inappropriate ioctl for device" and I don't receive the metasploit promt. just select an exploit. This is my code:... (2 Replies)
Discussion started by: dano88
2 Replies

3. Programming

C, unix, pipes, fork, recursion

Hi, I will try to keep my post as compressed as my title was. I am writing on pseudo code on a recursive function that I want to read from the one-above function-run and then give the result to the function-run down below until a stop is triggered. Example: $ ls -la | grep x | sort In my... (2 Replies)
Discussion started by: tarasque
2 Replies

4. Programming

C Socket Client/Server Fork Loop

Hello people. I'm trying to do something like a search engine. Server runs in the background by using ./server & which has data from a textfile stored in an array. Client then runs by using ./client It will then prompt "Search for:" For example, if I searched for Products called Instant... (0 Replies)
Discussion started by: andylbh
0 Replies

5. UNIX for Advanced & Expert Users

Unix: socket & Co

Hello, I need help to replace the ................. of client.c that request the server implemented by server.c ------------------ Listing 1 - server.c /* Inclusion des différentes librairies nécessaires */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> ... (0 Replies)
Discussion started by: bounkolh
0 Replies

6. Programming

C++ How to use pipe() & fork() with stdin and stdout to another program

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question:... (2 Replies)
Discussion started by: vvaidyan
2 Replies

7. Shell Programming and Scripting

awk pipes & print

Hi, I need to awk some data from some text files, basic stuff, eg: awk '/phrase/ {print $1,$2,$3}' file Which will print columns 1 to 3 of all lines containing "phrase" But what if I wanted to pipe just one of the columns to a command (in my case a date converter) as in: awk... (4 Replies)
Discussion started by: Jonny2Vests
4 Replies

8. Programming

regarding socket & mssage queue

hello , I have to write an application in which I had to implement both Socket Comminication and IPC- message queues. and that process should run in Infinite loop as well I had to continously check and send data through both type of communications... What should I use to implement it... I had... (34 Replies)
Discussion started by: arunchaudhary19
34 Replies

9. 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

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question