Pipes vs Socket Pairs
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Pipes vs Socket Pairs




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Pipes vs Socket Pairs  
grid


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-13-05 01:05 PM

Hi,
I was reading a text on IPC's and found this detail

"You can view socketpairs as an extension of pipes. Where a pipe can be
viewed as a pair of connected sockets for one-way stream communication,
a socketpair can be viewed as a pair of connected sockets for two-way
stream communication."

I suppose pipes are also bidirectional and can be used in place of
socketpairs.I tried running a program and it works fine :
-----
#define DATA1 "In Xanadu, did Kublai Khan..."
#define DATA2 "A stately pleasure dome decree..."

main()
{
int sockets[2], child;
char buf[1024];

if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
perror("opening stream socket pair");
exit(1);
}
/* Replaced the above 4 lines with below code for pipes
*        if (pipe(sockets) < 0) {
*               perror("opening stream socket pair");
*                exit(1);
*        }
*/
if ((child = fork()) == -1)
perror("fork");
else if (child) {       /* This is the parent. */
close(sockets[0]);
if (read(sockets[1], buf, sizeof(buf)) < 0)
perror("reading stream message");
printf("-->%s\n", buf);
if (write(sockets[1], DATA2, sizeof(DATA2)) < 0)
perror("writing stream message");
close(sockets[1]);
} else {                /* This is the child. */
close(sockets[1]);
if (write(sockets[0], DATA1, sizeof(DATA1)) < 0)
perror("writing stream message");
if (read(sockets[0], buf, sizeof(buf)) < 0)
perror("reading stream message");
printf("-->%s\n", buf);
close(sockets[0]);
}
}

Can anyone comment on this ? Of course it can get a bit messy if we try
to use the same pipe , so its always cleaner to have 2 pipes before the
fork.But then again , socket pairs will also have the same problems.
Are socket pairs used as extensively as pipes ?

TIA
~





[ Post a follow-up to this message ]



    Re: Pipes vs Socket Pairs  
Patrick Plattes


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-13-05 01:05 PM

hi

grid wrote:
> I suppose pipes are also bidirectional and can be used in place of
> socketpairs.I tried running a program and it works fine :

not for me:

patrick@Dragon:~/studium/informatik/sys/pipe$ ./pipe
writing stream message: Bad file descriptor
reading stream message: Bad file descriptor
-->D
patrick@Dragon:~/studium/informatik/sys/pipe$ -->A stately pleasure dome
decree...

[ Linux version 2.6.12-rc4 (root@Dragon) (gcc version 3.3.5 (Debian
1:3.3.5-12)) ]

have a nice day,
patrick





[ Post a follow-up to this message ]



    Re: Pipes vs Socket Pairs  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-13-05 01:05 PM

grid <prohit99@gmail.com> wrote:
>    I was reading a text on IPC's and found this detail

> "You can view socketpairs as an extension of pipes. Where a pipe can be
> viewed as a pair of connected sockets for one-way stream communication,
> a socketpair can be viewed as a pair of connected sockets for two-way
> stream communication."

> I suppose pipes are also bidirectional and can be used in place of
> socketpairs.I tried running a program and it works fine :

Some systems have bidirectional pipes, so on these it will work - and
yours seems to be one of them. But you can't rely on that always being
the case. So if you want to be sure the program is going to work on all
systems you better restrict yourself to using pipes only in a single
direction.
Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: Pipes vs Socket Pairs  
Casper H.S. Dik


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-14-05 06:23 PM

Patrick Plattes <newsgroup@erdbeere.net> writes:

>hi

>grid wrote: 
[vbcol=seagreen]
>not for me:

>patrick@Dragon:~/studium/informatik/sys/pipe$ ./pipe
>writing stream message: Bad file descriptor
>reading stream message: Bad file descriptor

System Vr4 pipes are bidirectional.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.





[ Post a follow-up to this message ]



    Re: Pipes vs Socket Pairs  
James Antill


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-14-05 06:23 PM

On Fri, 13 May 2005 17:26:28 +0530, grid wrote:

> Hi,
>    I was reading a text on IPC's and found this detail
>
> "You can view socketpairs as an extension of pipes. Where a pipe can be
> viewed as a pair of connected sockets for one-way stream communication,
> a socketpair can be viewed as a pair of connected sockets for two-way
> stream communication."

This is a bad description, sockets obey "man 7 socket" pipes do not. The
fact pipes are also unidirectional can be the least of your worries.

--
James Antill -- james@and.org
http://www.and.org/vstr/httpd






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 02:27 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register