Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   named pipes question (http://www.programmingforums.org/showthread.php?t=12638)

cwl157 Feb 21st, 2007 8:04 PM

named pipes question
 
I want to be able to open 2 way communications between people using named pipes (they are created using the mkfifo command). I am able to get one way communications to work by entering
:

cat > mypipe
on the one user login and
:

cat < mypipe
on the other user login. However, this is only 1 way communication. I also need it to go the other way too. I have tried to make another pipe and then do the same thing but reverse so it would read and write from and to the other screen but it seems like only one pipe is run at a time. Is there a way for one of the pipes to run in the background or something so i can have 2 way communication at the same time? Thanks. By the way i am using BASH and both users are logged into the same machine from remote locations.

a thing Feb 21st, 2007 9:40 PM

The name FIFO implies that this can't be done.

Perhaps Plan 9 can do what you want. :beard:

Is this the entire purpose of the program or just a part of it? If it's just a part, do tell what the whole program will do. If that's the whole program, why not just use IRC or talk?

cwl157 Feb 21st, 2007 10:07 PM

well the purpose of the program is to make a chat program between 2 users logged onto the same machine. However, each user can talk to more than one person but no conference calls so only 2 way communication. It starts by recognizing the word send and then a user name is entered after that and then a named pipe connection is made and a message is sent and then the receiving user receives the message and can send back. And the user can be talking to multiple users at the same time. However, as you said the more i think about it the more i do not think this is possible since when a pipe is set up to send and receive it is always waiting to send or always waiting to receive so how do you receive on the end thats waiting to send without breaking out of the pipe and entering a new one to receive? The only way i can see how to do it is to type send and the user name every time you want to send something because then after the send it would break out so you have the option to recieve. However, i was told this is unacceptable. Aside from the 2 way communication a log has to be made of the conversation and emailed to both parties after the conversation is over. And the use of talk is not acceptable either. I know i don't think this is a very thought out program either. Anyone have any ideas?

a thing Feb 21st, 2007 11:46 PM

I still think using an existing solution like IRC, Jabber, talk, or even email would be best.

But just because this is fun, perhaps each end could have its own FIFO. But I'm not so sure about this after a little experimentation.

cwl157 Feb 22nd, 2007 9:21 AM

yea i tried to use 2 pipes and couldn't get it to work.

jim mcnamara Feb 22nd, 2007 3:10 PM

ksh (the shell ) supports exactly what you want - it's called co-processes.
talk also does what you want. Both work from the command line.

Both of these are part of any POSIX1b (IEEE 1003.1 2004) implementation - including Linux.

cwl157 Feb 22nd, 2007 3:36 PM

i have to use bash shell. and i have kind of a start but i still do not know how to get 2 way communication.

cwl157 Feb 22nd, 2007 4:42 PM

By start i mean i have figured out how to check the command line arguments and make sure a user name is entered but the communication thing im still lost. like isn't there a way for one of the pipes to run in the background or something so when you type it will still send it and then have recieve running the foreground or something? I was told the & can be used to do this but haven't been able to get anything to work.

cwl157 Feb 23rd, 2007 2:04 AM

alright i've made a lot of progress. i figured out how to get 2 way communication by having receive run in the background however, with the way send and receive are set up its like the second one created receives from its same pipe that it sends to it just prints on the screen twice instead of printing on the screen thats supposed to be receiving. One way communication works fine and 2 way would work if i could figure out how to get it to go to a different pipe or something. I thought to keep the multiple conversations and pipes straight i would just name the pipe after the user that's being sent the message however when the recipient sends a message back it uses the same pipe and therefore just prints out twice on the same screen instead of once on the other screen. I guess its like going in a circle or something? Any ideas on how to make it send to the other pipe? Also, this all only works if the same user is logged in at 2 different places. Any ideas on how to make it work with one user sending it to another because i couldn't get any kind of communication to work user to user. Heres what i have:
send.sh the send program
:

#!/usr/bin/bash

if [ "$1" = "" ]
then
  w | cut -f1 -d ' ';
  echo "Please enter a user name";
  read uname;
  #uname="$1";
  echo $uname;
else
  uname="$1";
  #echo $1;
  echo $uname;
fi
echo $uname;
mkfifo $uname
#mkfifo mypipe5
cat > $uname


receive.sh the receive program
:

#!/usr/bin/bash

pipeName=$(echo $(whoami));
#echo "pipename after assignment is"
#pipeName="mypipe5"
echo $pipeName
echo $pipeName;
mkfifo $pipeName;
cat < $pipeName&

So basically a circle is being created if send.sh is run after receive.sh is run because since the user names are both the same the pipes are both the same also. Is there a way to append like a 1 or something at the end of one of them so the names are different? However, wouldn't this make it impossible to receive for one user because the pipe that is being wrote to and read from would be different?


All times are GMT -5. The time now is 9:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC