[comp.lang.perl] using sendto and recvfrom in perl

tonyg@cs.uq.oz.au (Tony Gedge) (06/13/91)

I want to use the system calls "recvfrom" and "sendto" in a perl script.
Has anyone put some nice wrappers around these system calls?

My current version appears to be a *bit* buggy.

This is under SunOS 4.1 and using perl version 4.0 patchlevel 10

here's my code - ignore the fact that I'm not returning any data as yet...

--- <snip> --- <snip> --- <snip> --- <snip> --- <snip> --- <snip> --- <snip> 
sub sendto {
	local($sock,$msg,$len,$flags,$to,$tolen) = @_;
	local($result) = 1;

	$len		= $len+0;				# Force to be an integer
	$tolen		= $tolen+0;				# Force to be an integer

	if (syscall(SYS_sendto,fileno($sock),$msg,$len,$flags,$to,$tolen) < 0) {
		print "sendto: $!\n";
		$result = 0;
	}
	$result;
}

sub recvfrom {
	local($sock,$msg,$len,$flags,$from,$fromlen) = @_;
	local($result) = 1;

	$len		= $len+0;				# Force to be an integer
	$msg		= pack("a$len",'');		# Null pad to correct length
	$from		= pack("a$fromlen",'');	# Null pad to correct length
	$fromlen	= pack("n",$fromlen);	# number, but stored as string

	if (syscall(SYS_recvfrom,fileno($sock),$msg,$len,$flags,$from,$fromlen) < 0) {
		print "recvfrom: $!\n";
		$result = 0;
	}
	$result;
}

1;
--- <snip> --- <snip> --- <snip> --- <snip> --- <snip> --- <snip> --- <snip> 

Tony Gedge.

--
 -------------------------------------------------------------------------
| Computer Science Department,        | tonyg@cs.uq.oz.au   (Tony Gedge)  |
| University of Queensland, Australia.| "cc stands for Cryptic Crossword" |
 -------------------------------------------------------------------------

tchrist@convex.COM (Tom Christiansen) (06/16/91)

From the keyboard of tonyg@cs.uq.oz.au:
:I want to use the system calls "recvfrom" and "sendto" in a perl script.
:Has anyone put some nice wrappers around these system calls?
:
:My current version appears to be a *bit* buggy.
:
:This is under SunOS 4.1 and using perl version 4.0 patchlevel 10
:
:here's my code - ignore the fact that I'm not returning any data as yet...

Use send for sendto: just add another parameter.  Your syscall for recvfrom
looks a little questionable on the $from -- seems like an odd way to make
a sockaddr.  I haven't looked too hard though.  

--tom

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (06/17/91)

In article <WEG.91Jun16134353@convx1.convx1.ccit.arizona.edu> weg@convx1.ccit.arizona.edu (Eythan Weg) writes:
: In article <1991Jun16.045207.6561@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
: 
:    From the keyboard of tonyg@cs.uq.oz.au:
:    :I want to use the system calls "recvfrom" and "sendto" in a perl script.
:    :Has anyone put some nice wrappers around these system calls?
:    :
:    :My current version appears to be a *bit* buggy.
:    :
:    :This is under SunOS 4.1 and using perl version 4.0 patchlevel 10
:    :
:    :here's my code - ignore the fact that I'm not returning any data as yet...
: 
:    Use send for sendto: just add another parameter.  Your syscall for recvfrom
:    looks a little questionable on the $from -- seems like an odd way to make
:    a sockaddr.  I haven't looked too hard though.  
: 
: I have actually tried to use send and recv between datagram sockets.
: It worked beautifully.  Trying to do the same with connected stream-sockets
: failed with recv error.  Is perl expected to recv?

Perl always does a recvfrom() when you say recv().  (Look at eval.c if
you don't believe me.)  If your implementation of sockets can't do recvfrom()
on a stream socket, it's busted.

It would certainly be possible to work around such bogosity, but I usually
rely on othere's to send me the patches...

But anyway, there's no need to use syscall to get at sendto() or recvfrom().
(Note that Perl's recv() was partially busted in the late 3.0 era, so be sure
to use a recent version.)

Larry