LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Write command in named pipes (https://www.linuxquestions.org/questions/programming-9/write-command-in-named-pipes-396627/)

sahel 12-26-2005 02:23 PM

Write command in named pipes
 
Hi:)
I have a problem.
i am writing this program. It works between Client and The server. The Server Listens to a namedpipe .whenever a client comes up and wants to login , The client is asked for it's userid. This user id has to be written in the named pipe(that has already been opened) and be sent to the server. The server is going to creat another exclusive named pipe with this information for this user.
My question is that i don't know how to write userid in named pipe and how to create exclusive named pipe for this user according to this userid.
Thanks so much

bulliver 12-26-2005 02:48 PM

First of all. what language are you developing in?

Second, do you not intend to use this over the network? If so you may want to redesign using sockets now, as it will be more difficult to do it later if you need your app networked.

Third, to send data to a pipe you simply open it as a file object and write to it. Here's a trivial example using bash and python:
Code:

...from shell 1
# mkfifo pipe
# cat pipe

...from shell 2
# python
>>> p = open("./pipe","w")
>>> p.write("hello pipe\n")
>>> p.flush()

After this you will see "hello pipe" in your first shell.

sahel 12-26-2005 03:18 PM

Hi
first of all thank you.
I am using C .

bulliver 12-26-2005 03:31 PM

Quote:

printf("login name?")
scanf("%s",str);
wirte ("./server",str,strlen(str));
Sure. It's just a regular file object. You can operate on it as you can any other normal file. The only difference is that both ends must be open for data to pass, ie server must be open for reading and client must be open for writing.

Quote:

mk=mkfifo("here i want to use the username as the filename.how?) , 0777)
Well, you are getting the name of the user in your read call right? So just assign it to a variable and pass that to your mkfifo call. Depending on your code, you may have to clean it up a bit. I would imagine at the very least you will need to 'chomp' the newline character.

sahel 12-26-2005 03:43 PM

Thank you for the responce

bigearsbilly 12-28-2005 05:32 AM

You send data over the pipe. You decide what it means.
If it's a user id then it's a user id.

you can call your named pipe whatever you want.
(lay off /dev/tty /dev/sound /boot/vmlinuz tho)

I agree though, may just as well use sockets, not much more work but
more flexible approach. fifo's are ok for shell scripts.

Hko 12-28-2005 06:02 AM

Quote:

Originally Posted by bulliver
Sure. It's just a regular file object. You can operate on it as you can any other normal file. The only difference is that both ends must be open for data to pass, ie server must be open for reading and client must be open for writing.

And: The reading process must actually read away any data coming in from the pipe, because buffers for pipes are fixed as well as small.

bigearsbilly 12-28-2005 06:05 AM

and don't forget to send a newline to flush the data through each time.
you'll sit there waiting for hours!


All times are GMT -5. The time now is 02:33 AM.