Multiple read / write processes of child processes on the same channel

I am currently studying socket programming using C in a Linux environment. As a project, I am trying to write a basic chat server and client.

It is assumed that the server will blame the process for each connected client.

The problem I encountered is reading the data in one child element and writing it to all connected clients.

I tried to accomplish this by going through a call to select in a child that expects the data to arrive at the socket or at the end of the read. If it arrives at the socket, the idea is that it writes the end of the record to the handset, which causes a choice to return the read end of the channel as ready for reading.

Since this channel is shared by all children, each child must then read the data on the pipe. This does not work, since the data feed, apparently, cannot be read by each child at the same time, and children who “skip” the data block in the read request.

Below is the code of the child process that does this:

for( ; ; )
{
  rset = mset;
  if(select(maxfd+1, &rset, NULL, NULL, NULL) > 0)
  {
    if(FD_ISSET(clientfd, &rset))
    {
      read(clientfd, buf, sizeof(buf));
      write(pipes[1], buf, strlen(buf));
    }
    if(FD_ISSET(pipes[0], &rset))
    {
      read(pipes[0], buf, sizeof(buf));
      write(clientfd, buf, sizeof(buf));
    }
  }
}

I assume that the method I'm using right now just won't work. Is it possible for messages received from the client to be recorded to all other connected clients via IPC?

thank

+3
source share
2 answers

, (, , "", ), , , POSIX , . , , , , , , - /, , , "" , - , , , , .

, . , , , , , , , . "" , . /, .

, , POSIX ( mqueue.h). , , (.. struct, ID), ... , , , , , FIFO.

+2

, , . , .

, , . , "" -, "" . , -, , .

+2

Source: https://habr.com/ru/post/1656144/


All Articles