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


 
Thread Tools Search this Thread
Top Forums Programming kill() function problem in client-server ipc message using 2 FIFOs
# 1  
Old 06-10-2011
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 mesg_len+mesg_type+mesg_data

The function is that if user input "Knock Knock" on stdin, which directs to client, client send this message to server through FIFO1, server compares the string, if matches with "Knock Knock", then server reply message "Who's there?" to client through FIFO2, and client write this message to the stdout.

Last edited by ouou; 06-11-2011 at 03:25 PM..
# 2  
Old 06-10-2011
Quote:
Originally Posted by ouou
I want to have a message send & receive through 2 uni-direction FIFO
Pipes didn't work for ya?
Quote:
I need to have boundary structed message mesg_len+mesg_type+mesg_data
Then pipes don't sound like what you want. Packet sockets would send the exact size of message you want and not buffer.
Quote:
I need help on kill() signals when client types "exit".It seems the client process still live after call kill(). So i have to type ctrl+c to end

The requirement is to terminate all signals,exit program when client type "exit". please help

Please help me. Thanks a lot!
Why do you color everything manually? I can't just quote your message, I have to do work to get rid of that junk.
Code:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/stat.h>
 
#define MAX_BUF 100
#define MAXMESGDATA (MAX_BUF - 2* sizeof(long))
#define MESGHDRSIZE (sizeof(struct mymesg)-MAXMESGDATA)
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
 
struct mymesg{
long mesg_len; //byte in mesg_data
long mesg_type; //message type
char mesg_data[MAXMESGDATA];
};
 
ssize_t mesg_send (int fd, struct mymesg *mptr){
return (write (fd,mptr,MESGHDRSIZE + mptr->mesg_len));
}
 
ssize_t mesg_recv(int fd,struct mymesg *mptr){
   size_t len;
   ssize_t n;
if ((n=read(fd,mptr,MESGHDRSIZE))==0) {//read hear first, get len of data
return0; //end of file
   } elseif (n!=MESGHDRSIZE){
       printf("message header: expected %d, got %d\n", MESGHDRSIZE,n);
       exit(1);
   }
if ((len=mptr->mesg_len)>0)
   {
if ((n=read(fd,mptr->mesg_data,len))!=len)
       {
           printf("message data: expected %d, got %d\n", len,n);
           exit(1);
       }
   }
return len;
}
 
void client(int readfd,int writefd){
   size_t len;
   ssize_t n;
struct mymesg mesg;
for (;;)
   {
       printf("\nClient:");
       fgets(mesg.mesg_data,MAXMESGDATA,stdin);//read mesg
       len=strlen(mesg.mesg_data);
 
if (mesg.mesg_data[len-1]=='\n') //ignore newline
           len--;
       mesg.mesg_len=len;
       mesg.mesg_type=1;
 
       mesg_send(writefd,&mesg);//write to IPC channel
//read from IPC,write to std output
if((n=mesg_recv(readfd,&mesg))>0)
           write(STDOUT_FILENO,mesg.mesg_data,n);
   }
}
 
void server(int readfd,int writefd){
   ssize_t n;
struct mymesg mesg;
for(;;)
   {
       mesg.mesg_type=1;
//read from IPC channel
if ((n=mesg_recv(readfd,&mesg))==0){
           printf("Message missing");
           exit(1);
       }
 
       mesg.mesg_data[n]='\0';
       mesg.mesg_len=strlen(mesg.mesg_data);
char* str=NULL;
 
if (strcasecmp ("Knock Knock", mesg.mesg_data)==0){
            str="Server:Who's there?";
            strcpy(mesg.mesg_data,str);
            mesg.mesg_len=strlen(str)-1;
           }
elseif(strcasecmp ("Eric", mesg.mesg_data)==0){
           str="Server:Eric,Welcome!";
           strcpy(mesg.mesg_data,str);
           mesg.mesg_len=strlen(str)-1;
           }
elseif(strcasecmp ("Exit", mesg.mesg_data)==0){
           kill(getpid(),SIGTERM); // getpid() gets your OWN pid!
           kill(getppid(),SIGTERM); // After you're dead, you can't kill anything else.
           exit(0);
           }
       mesg_send(writefd,&mesg);
   }
}
 
int main(int argc, char ** argv){
/*MAXMESGDATA== 92 bytes; sizeof(struct mymesg)== 100 bytes 
     2* sizeof(long)== 8 bytes; MESGHDRSIZE ==8 bytes*/
 
int readfd,writefd;
   pid_t childpid;
//create 2 FIFOs
if ((mkfifo(FIFO1,FILE_MODE)<0) && (errno!=EEXIST)){
       printf("can't create %s",FIFO1);
       exit(1);
   }
if ((mkfifo(FIFO2,FILE_MODE)<0) && (errno!=EEXIST)){
       printf("can't create %s",FIFO1);
       unlink(FIFO1);
       exit(1);
   }
if ((childpid=fork()==0)){//child
       readfd=open(FIFO1,O_RDONLY,0);
       writefd=open(FIFO2,O_WRONLY,0);
       server(readfd,writefd);
       exit(0);
   }
//parent
   writefd=open(FIFO1,O_WRONLY,0);
   readfd=open(FIFO2,O_RDONLY,0);
   client(readfd,writefd);
 
   waitpid(childpid,NULL,0);
   close(readfd);
   close(writefd);
   unlink(FIFO1);
   unlink(FIFO2);
return EXIT_SUCCESS;
}

# 3  
Old 06-10-2011
Thanks for your continue help.

I tried this before, just leave the kill (getppid(),SIGTERM) there,it can't terminate the program. in my main(), I put waitpid(childpid,NULL,0);

please help


elseif(strcasecmp ("Exit", mesg.mesg_data)==0){
// kill(getpid(),SIGTERM); // getpid() gets your OWN pid!
kill(getppid(),SIGTERM); // After you're dead, you can't kill anything else.
exit(0);
}

Last edited by ouou; 06-11-2011 at 01:36 PM..
# 4  
Old 06-10-2011
Have you checked the return values of kill()? try perror.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

IPC Message Queue. msgrcv doesnt work..

Hi everybody, this is the situation. there is a programm XYZ which opens a message queue with the key 47110815 and waits for a SIGUSR1. After receiving this signal it sends a message with type 100 and a number (as ASCII) in the message-body. I have to write a prog which frist sends the... (1 Reply)
Discussion started by: daredevil82m
1 Replies

3. Programming

Client/Server Socket Application - Preventing Client from quitting on server crash

Problem - Linux Client/Server Socket Application: Preventing Client from quitting on server crash Hi, I am writing a Linux socket Server and Client using TCP protocol on Ubuntu 9.04 x64. I am having problem trying to implement a scenario where the client should keep running even when the... (2 Replies)
Discussion started by: varun.nagpaal
2 Replies

4. SuSE

SLES - 10 -DFLT_MSGMAX - Message queues in /usr/src/linux/ipc/mqueue.c

Folks, Does anyone know the magic to balancing out the: DFLT_MSGMAX and the DFLT_MSGSIZEMAX values? Is there some magical formula... We have a home grown program that is choking we think because of these values (the 10 and the 8192 defaults) Thanks, thomas B. #define... (0 Replies)
Discussion started by: lacakid
0 Replies

5. Programming

Server - Client application problem

hi all I'm beginner in CORBA Server-Client appliction development. My server- client application was worked well and i have tested it too. Due to some Network problem we have rebooted our dedicated server , then i restart my corba service, application in the server it started... (0 Replies)
Discussion started by: VijayakumarN
0 Replies

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

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

8. HP-UX

posix ipc message queue

Hello, My question is related to "pipcs -qa" command under HP-UX 11i PA-RISC 64 bits. We have a little C program that creates posix ipc message queues using the mq_open() system function. The program fail with 'No space left on device' error when we create big queues. What is the system... (6 Replies)
Discussion started by: cadanir
6 Replies

9. Programming

forks, ipc, fifos, update issues...

Hi, so I've got this program("main") that fork executes another ("user"). These programs communicate through fifos. One communication is a spawn call, where user passes an executable, main forks and executes it. So, I'm keeping track of all my processes using a task table. After the fork (for... (6 Replies)
Discussion started by: Funktar
6 Replies

10. Programming

Praying for help: FIFOs, IPC

omg i need help so bad. I've been working on a school project for what seems like an eternity and i'm close to deadline. Using FIFO's (i ahve to) to communicate between parent and child proc's. Right now I'm stuck on a read/write. fifomsg is a struct with int length and char message fields. ... (5 Replies)
Discussion started by: Funktar
5 Replies
Login or Register to Ask a Question