Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Socket recv, sysread and Posix::read in sockets?

Tags:

perl

I find at least 3 ways to read from a nonblocking socket in perl

$socket->recv
$socket->sysread
POSIX::read($socket,...

looks like 3 different names to the same thing, I read the documentations but I can't find one huge differente. anyone?

like image 731
Tiago Peczenyj Avatar asked Dec 26 '22 14:12

Tiago Peczenyj


2 Answers

sysread is stream (TCP) oriented (it doesn't care about where one send ends and another begins), and recv is datagram (UDP) oriented (it does care).

POSIX::read works on file descriptors, whereas sysread works on file handles.

like image 150
ikegami Avatar answered May 15 '23 03:05

ikegami


The best source for documentation on recv() is man recvfrom - it is basically a perl interface to that system call. Note that recv() is usually used on sockets which are set up non-connection oriented (i.e. a UDP socket), but it may be also be used on connection oriented (i.e. TCP) sockets.

The man differences between read(), sysread() and POSIX::read() are:

  • read(...) takes a file handle and the IO is buffered
  • sysread(...) takes a file handle and the IO is not buffered
  • POSIX::read(...) takes a file descriptor and the IO is not buffered

A file descriptor is a value (a small integer) that is returned by POSIX::open(). Also, you can get the file descriptor of a perl file handle via the fileno() function.

like image 28
ErikR Avatar answered May 15 '23 02:05

ErikR