UNIX datagram sockets


J

John Kelly

Stein has an example of UNIX datagram sockets in his Networking book,
but it includes other ideas that, to me, made it hard to see the forest
for the trees. So I distilled it down to the bare essential elements
related to socket setup.

I use a loop with 100,000 iterations to test performance, and get about
40,000 round trips per second. A similar C solution gets about 55,000
round trips per second. Because syscall and context switch overhead is
a limiting factor, the Perl code performs well compared to C.

Please ignore bad practices in this code, it's only intended to show how
to set up the datagram sockets, which to me, seemed mysterious without a
simple example.


--- CLIENT ---

#!/usr/bin/perl

use strict;
use warnings;
use IO::Socket::UNIX;

my $peer = '/tmp/server.sock';
my $node = '/tmp/client1.sock';
unlink $node;

my $sock = IO::Socket::UNIX->new (
Local => $node,
Peer => $peer,
Type => SOCK_DGRAM
) or die "$!";

my $tn;
my $data;

for ($tn = 1; $tn < 100000; $tn++) {

send ($sock, '1234567890_blah_blah_blah', 0) or die "$!";
recv ($sock, $data, 100, 0) or die "$!";

}

print "tn=$tn\n";



--- SERVER ---

#!/usr/bin/perl

use strict;
use warnings;
use IO::Socket::UNIX;

my $node = '/tmp/server.sock';
unlink $node;

my $sock = IO::Socket::UNIX->new (
Local => $node,
Type => SOCK_DGRAM
) or die "$!";

my $peer;
my $data;

while (1) {

$peer = recv ($sock, $data, 100, 0);
send ($sock, $data, 0, $peer) || warn "$!";

}
 
Ad

Advertisements

U

Uri Guttman

JK> Stein has an example of UNIX datagram sockets in his Networking book,
JK> but it includes other ideas that, to me, made it hard to see the forest
JK> for the trees. So I distilled it down to the bare essential elements
JK> related to socket setup.

JK> I use a loop with 100,000 iterations to test performance, and get about
JK> 40,000 round trips per second. A similar C solution gets about 55,000
JK> round trips per second. Because syscall and context switch overhead is
JK> a limiting factor, the Perl code performs well compared to C.

so what is your actual question?

uri
 
J

John Kelly

JK> Stein has an example of UNIX datagram sockets in his Networking book,
JK> but it includes other ideas that, to me, made it hard to see the forest
JK> for the trees. So I distilled it down to the bare essential elements
JK> related to socket setup.

JK> I use a loop with 100,000 iterations to test performance, and get about
JK> 40,000 round trips per second. A similar C solution gets about 55,000
JK> round trips per second. Because syscall and context switch overhead is
JK> a limiting factor, the Perl code performs well compared to C.

so what is your actual question?

Do you expect all posts to ask for help? Why?
 
U

Uri Guttman

JK> Stein has an example of UNIX datagram sockets in his Networking book,
JK> but it includes other ideas that, to me, made it hard to see the forest
JK> for the trees. So I distilled it down to the bare essential elements
JK> related to socket setup.JK> I use a loop with 100,000 iterations to test performance, and get about
JK> 40,000 round trips per second. A similar C solution gets about 55,000
JK> round trips per second. Because syscall and context switch overhead is
JK> a limiting factor, the Perl code performs well compared to C.
JK> Do you expect all posts to ask for help? Why?

because they usually do. nothing you posted was signifigant as it was
basic udp code that you can get from modules, books, documentation,
etc. it wasn't particularly interesting even as a basic example. so if
you had a reason other than asking a question, you could have stated
that. seeing none, i had to ask why you posted it or you missed asking a
question.

uri
 
J

John Kelly

JK> Do you expect all posts to ask for help? Why?

because they usually do. nothing you posted was signifigant as it was
basic udp code that you can get from modules, books, documentation,
etc. it wasn't particularly interesting

I didn't realize you are the only person who ever reads this ng. How
rude of me to invade your territory! :-D
 
U

Uri Guttman

JK> On Wed, 23 Jun 2010 01:50:46 -0400, "Uri Guttman" <[email protected]>
JK> wrote:

JK> Do you expect all posts to ask for help? Why?
JK> I didn't realize you are the only person who ever reads this ng. How
JK> rude of me to invade your territory! :-D

not rude at all. but an odd posting in any case. seriously, what was
your point? education? illumination?

uri
 
Ad

Advertisements

J

John Kelly

because they usually do. nothing you posted was signifigant as it was
basic udp code that you can get from modules, books, documentation,
etc. it wasn't particularly interesting
not rude at all. but an odd posting in any case. seriously, what was
your point? education? illumination?

It's easy to find INET socket example code, but harder to find UNIX
socket example code. Some of the few I found didn't work at all, and
Stein's example didn't explain the relationship between the Local and
Peer arguments when using SOCK_DGRAM.

Also, his example used sockaddr_un to get the server address, which is
not necessary if you're only talking to a single server. It's clearer
to simply specify Peer on the call to IO::Socket::UNIX.

But without prior experience on that particular problem, I didn't know
that. I had to learn by reading, trial and error, and I spent much more
time on it than I wanted to.

Not everyone has time to become a sockets or Perl expert, and finding an
easy recipe saves time. So I posted mine.

Once upon a time, Usenet users were friendly and generous. Maybe those
days are gone now, but I remember when ...
 
U

Uri Guttman

JK> It's easy to find INET socket example code, but harder to find UNIX
JK> socket example code. Some of the few I found didn't work at all, and
JK> Stein's example didn't explain the relationship between the Local and
JK> Peer arguments when using SOCK_DGRAM.

you can also use sendto and not need a peer argument.

JK> But without prior experience on that particular problem, I didn't know
JK> that. I had to learn by reading, trial and error, and I spent much more
JK> time on it than I wanted to.

JK> Not everyone has time to become a sockets or Perl expert, and finding an
JK> easy recipe saves time. So I posted mine.

then you should say that. it wasn't clear what the purpose of your post
was.

JK> Once upon a time, Usenet users were friendly and generous. Maybe those
JK> days are gone now, but I remember when ...

and you are helping how with your comments? i just asked a question
which took you 3 replies to answer, why did you post this. instead you
flamed me until you actually answered it. i never attacked you, just
asked you why you posted it. simple.

uri
 
J

John Kelly

JK> Once upon a time, Usenet users were friendly and generous. Maybe those
JK> days are gone now, but I remember when ...

and you are helping how with your comments? i just asked a question
which took you 3 replies to answer, why did you post this. instead you
flamed me until you actually answered it. i never attacked you,

Yes you did, you said my recipe was not interesting, implying my post
was worthless to anyone who might read it.

Some ngs are dominated by territorial control freaks who get upset when
someone unknown comes along and posts without bowing and scraping before
the self appointed elite.

But the way I see it, that's their problem, not mine.
 
U

Uri Guttman

JK> On Wed, 23 Jun 2010 14:15:14 -0400, "Uri Guttman" <[email protected]>
JK> wrote:

JK> Once upon a time, Usenet users were friendly and generous. Maybe those
JK> days are gone now, but I remember when ...
JK> Yes you did, you said my recipe was not interesting, implying my post
JK> was worthless to anyone who might read it.

it wasn't interesting to me as it had NO CONTEXT. just code with no
reason for posting. if you said, i had issues with writing this and here
is why i did this, it might have been interesting. but you just posted
some simple socket code with no explanation of why it might be
interesting.

uri
 
Ad

Advertisements

U

Uri Guttman

R> And that made me stop reading. Next!

another reason it wasn't interesting to me.

uri
 
J

John Kelly

JK> Yes you did, you said my recipe was not interesting, implying my post
JK> was worthless to anyone who might read it.

it wasn't interesting to me as it had NO CONTEXT

Then you could have just ignored it. But you decided to pick a fight
instead. Happy now?
 
U

Uri Guttman

JK> Yes you did, you said my recipe was not interesting, implying my post
JK> was worthless to anyone who might read it.
JK> Then you could have just ignored it. But you decided to pick a fight
JK> instead. Happy now?

me thinks you picked the fight. i just asked a question. you are the
prickly skinned one here. i still haven't attacked you and you are
attacking me. look in the mirror if you can. show me one line where i
said anything about you. i just wanted to know why you posted it. but
you don't seem to get that yet. over and out.

uri
 
J

John Kelly

me thinks you picked the fight. i just asked a question. you are the
prickly skinned one here. i still haven't attacked you and you are
attacking me. look in the mirror if you can. show me one line where i
said anything about you. i just wanted to know why you posted it. but
you don't seem to get that yet. over and out.

I explained why I posted it, but you didn't like that explanation, and
you continued arguing. If you want to act like a troll, then so be it.
 
J

John Kelly

And that made me stop reading. Next!

They say that with Perl, there's more than one way to do it. But I say
that with Perl, there's more than one way to shoot yourself in the foot.
 
Ad

Advertisements

X

Xho Jingleheimerschmidt

John said:
It's easy to find INET socket example code, but harder to find UNIX
socket example code. Some of the few I found didn't work at all, and
Stein's example didn't explain the relationship between the Local and
Peer arguments when using SOCK_DGRAM.

Unfortunately your example doesn't seem to explain that relationship,
either.


Xho
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top