Sponsored Content
Top Forums Programming Trouble with pipes in chat client on linux Post 302718281 by vindy on Friday 19th of October 2012 11:34:33 AM
Old 10-19-2012
Trouble with pipes in chat client on linux

I'm writing a simple chat client in C++ on linux to connect to a win32 chat server on my computer also written in C++. I'm confident that the server works but the chat client is giving me some trouble. I'm forking the chat client and have one process dealing with incoming messages and another dealing with sending messages. I'm using two pipes in the chat client so each process can tell the other when it has terminated so the other process can go ahead and terminate. The pipes don't seem to be working though and I'm not sure where I'm going wrong. If you guys could take a look at my code I'd appreciate it. Maybe one of you will notice something I'm not doing right. Here's my code:

Code:
#define ChildRead pipefd1[0]
#define ChildWrite pipefd2[1]
#define ParentRead pipefd2[0]
#define ParentWrite pipefd1[1]

int main()
{
  int nret, theSocket, theMode = 1, pipefd1[2], pipefd2[2];
  struct sockaddr_in server;
  char buf[2] = {0}, message[999] = {0}, ipAddy[20]= {0}, scrName[20] = {0};
  pid_t childpid;

  printf("Enter the IP address to connect to.\n\n");
  scanf("%s", ipAddy);

  printf("\nEnter your screen name.\n\n");
  scanf("%s", scrName);

  theSocket = socket(AF_INET, SOCK_STREAM, 0);

  if(theSocket < 0)
   printf("\nCouldn't create socket\n");

  memset(&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr(ipAddy);
  server.sin_port = htons(8888);

  printf("\nAttempting to connect to %s\n", ipAddy);

  nret = connect(theSocket, (struct sockaddr *)&server, sizeof(server));

  if(nret < 0)
   printf("\nCouldn't connect\n");

  ioctl(theSocket, FIONBIO, &theMode);

  printf("\nConnected to server!\n\n");

  memset(message, 0, strlen(message));

  pipe(pipefd1);
  pipe(pipefd2);

  fcntl(ParentRead, F_SETFL, O_NONBLOCK);
  fcntl(ParentWrite, F_SETFL, O_NONBLOCK);
  fcntl(ChildRead, F_SETFL, O_NONBLOCK);
  fcntl(ChildWrite, F_SETFL, O_NONBLOCK);

  childpid = fork();

  if(childpid == 0)
  {
    close(ParentRead);
    close(ParentWrite);

    while(1)
    {
      read(ChildRead, &buf, 1);

      if(buf[0] == 'q')
      {
       close(ChildRead);
       close(ChildWrite);
       close(theSocket);
       exit(0);
      }

      if((nret = recv(theSocket, message, 999, 0)) < 1)
       continue; 

      if(message[7] == '~' && message[8] == 'q')
      {
        printf("\nChat server disconnected..\n");  
        write(ChildWrite, "q\0", 1);
        close(ChildRead);
        close(ChildWrite); 
        close(theSocket); 
        exit(0);
      }

      printf("%s\n", message);

      memset(message, 0, strlen(message));
    }
  }
  else
  {  
     strcpy(message, scrName);
     strcpy(&message[strlen(message)], ": ");

     close(ChildWrite);
     close(ChildRead);

     while(1)
     {
       read(ParentRead, &buf, 1);

       if(buf[0] == 'q')
       {
         close(ParentRead);
         close(ParentWrite);   
         close(theSocket);
         exit(0); 
       }

       scanf("%s", &message[strlen(message)]);

       if(strcmp(&message[strlen(scrName) + 2], "~q\0") == 0)
       {
         write(ParentWrite, "q\0", 1);
         close(ParentRead);
         close(ParentWrite);
         close(theSocket);
         exit(0);          
       }

       send(theSocket, message, strlen(message), 0);

       memset(&message[strlen(scrName) + 2], 0, strlen(&message[strlen(scrName) + 2]));     
     }  
  } 

  return 0;
}

 

10 More Discussions You Might Find Interesting

1. Programming

multiuser chat server closes when one client closes. code included

I have been trying to write a very basic chat program but at the moment I am having problems getting it to be multiuser as it closes all connections when one client shutsdown. I have also been having problems trying to get the program to display a list of usernames to the clients. I have tried... (0 Replies)
Discussion started by: dooker
0 Replies

2. Programming

Chat client-server program

Good day everyone, I'm doing a chat client-server program:server is to receive messages from clients through a TCP port and multicast them back to all clients through a UDP port. This is my client program. I'd not know why it just sends and receives msg from server once, then it stops. Is... (1 Reply)
Discussion started by: powermind
1 Replies

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

4. UNIX for Dummies Questions & Answers

Server/client chat

I want to make the following programm. Using the server/client model I want 2 client to connect to the server then the server sends back to the clients the ip address and a number of a poort in order to open a udp connection between clients without using the server? What I have done since now is... (2 Replies)
Discussion started by: kasma
2 Replies

5. Programming

How can i make two client to chat with each other?

How can i make two client to chat with each other.. without any work of server, i mean peer to peer i am making chat messenger on which all host will connect to/via server and if any host want private chat then thay work independently... this is code(attached) which i have made for server... (1 Reply)
Discussion started by: bt87
1 Replies

6. UNIX for Dummies Questions & Answers

Server-client chat with a bit more

The task is to create a server client chat that contains a few basic safeguards against floods etc and which is capable of issuing at least one or two commands on the client computer. Working samples of such arrangements abound but freeware/shareware samples are not readily available. A catch in... (3 Replies)
Discussion started by: Bobby
3 Replies

7. Homework & Coursework Questions

Help with server client chat system

request create a chat system in linux where a user type smth and all the other users connected to server get the message.then a user have to create join leave or delete a channel of chat if he created it.i did the server and the client but i dont know how to implemt the chat rooms.i was ... (1 Reply)
Discussion started by: demonmind
1 Replies

8. Programming

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 stdin--->parent(client) fd1--->pipe1-->child(server) fd1 bottom half pipe child(server) fd2---->pipe2--->parent(client) fd2--->stdout I need to have boundary structed message... (1 Reply)
Discussion started by: ouou
1 Replies

9. Programming

logic understanding for inter client chat server

hello everyone, i am making chat server in linux using c. i have made programs in which group chat can take place between multiple clients but i am not able to understand how to make 2 particular clients chat with each other. please help!!! (1 Reply)
Discussion started by: sweetbella
1 Replies

10. UNIX for Advanced & Expert Users

Linux chat with other users

Besides talk and write are there any other good programs for talking to a particular user on the same Linux system. (1 Reply)
Discussion started by: cokedude
1 Replies
PIPE(2) 						     Linux Programmer's Manual							   PIPE(2)

NAME
pipe, pipe2 - create pipe SYNOPSIS
#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE #include <unistd.h> int pipe2(int pipefd[2], int flags); DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For fur- ther details, see pipe(7). If flags is 0, then pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior: O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open file descriptions. Using this flag saves extra calls to fcntl(2) to achieve the same result. O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors. See the description of the same flag in open(2) for reasons why this may be useful. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE Too many file descriptors are in use by the process. ENFILE The system limit on the total number of open files has been reached. VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9. CONFORMING TO
pipe(): POSIX.1-2001. pipe2() is Linux-specific. EXAMPLE
The following program creates a pipe, and then fork(2)s to create a child process; the child inherits a duplicate set of file descriptors that refer to the same pipe. After the fork(2), each process closes the descriptors that it doesn't need for the pipe (see pipe(7)). The parent then writes the string contained in the program's command-line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, " ", 1); close(pipefd[0]); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } SEE ALSO
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2009-09-15 PIPE(2)
All times are GMT -4. The time now is 12:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy