Next: Clients and Servers
Up: Networking with Perl
Previous: Networking with Perl
Sockets are the low-level links that enable Internet
conversations and many other network services. There are a many
functions that deal with sockets. Fortunately, you don't normally need to
deal with them all. A small subset is all you need to get started. This
section will focus in on those aspects of sockets that are useful in
Perl. There will be whole areas of sockets that are not dealt with here.
A small selection of useful Perl socket functions is given below:
- accept(NEWSOCKET,
SOCKET)
- -- Accepts a socket connection from clients waiting for a
connection. The original socket, SOCKET, is left along, and a new
socket is created for the remote process to talk with. SOCKET must
have already been opened using the socket() function. Returns true
if it succeeded, false otherwise.
- bind(SOCKET, PACKED_ADDRESS)
- -- Binds a
network address to the socket handle. Returns true if it succeeded, false
otherwise.
- connect(SOCKET, PACKED_ADDRESS)
- -- Attempts to connect to a
socket. Returns true if it succeeded, false otherwise.
getpeername(SOCKET)Returns the packed address of the remote side of the
connection. This function can be used to reject connections for security
reasons, if needed.
- getsockname(SOCKET)
- -- Returns the packed address of the
local side of the connection.
- getsockopt(SOCKET, LEVEL, OPTNAME)
- -- Returns
the socket option requested, or undefined if there is an error.
- listen(SOCKET, QUEUESIZE)
- -- Creates a queue for SOCKET with
QUEUESIZE slots. Returns true if it succeeded, false otherwise.
- recv(SOCKET, BUFFER, LEN, FLAGS)
- -- Attempts to receive
LENGTH bytes of data into a buffer from SOCKET. Returns the
address of the sender, or the undefined value if there's an error.
BUFFER will be grown or shrunk to the length actually read. However,
you must initalize BUFFER before use. For example my($buffer) =
'';.
- select(RBITS, WBITS, EBITS, TIMEOUT)
- -- Examines file descriptors
to see if they are ready or if they have exception conditions pending.
- send(SOCKET, BUFFER, FLAGS, [TO])
- -- Sends a message to a socket.
On unconnected sockets you must specify a destination (the TO
parameter). Returns the number of characters sent, or the undefined value
if there is an error.
- setsockopt(SOCKET, LEVEL, OPTNAME, OPTVAL)
- -- Sets
the socket option requested. Returns undefined if there is an error.
OPTVAL may be specified as undefined if you don't want to pass an
argument.
- shutdown(SOCKET, HOW)
- -- Shuts down a socket connection in the
manner indicated by HOW. If HOW = 0, all incoming information will
be ignomiles. If HOW = 1, all outgoing information will be stopped.
If HOW = 2, then both sending and receiving is disallowed.
- socket(SOCKET, DOMAIN,TYPE, PROTOCOL)
- -- Opens a specific
TYPE of socket and attaches it to the name SOCKET. Returns true
if successful, false if not.
- socketpair(SOCK1, SOCK2, DOMAIN, TYPE, PROTO)
- -
Creates an unnamed pair of sockets in the specified domain, of the
specified type. Returns true if successful, false if not.
Note
If you are interested in knowing everything about sockets, you need to
get your hands on some UNIX documentation. The Perl set of socket
functions are pretty much a duplication of those available using the C
language under UNIX. Only the parameters are different because Perl
data structures are handled differently. You can find UNIX documentation
at http://www.delorie.com/gnu/docs/ on the World Wide Web.
Next: Clients and Servers
Up: Networking with Perl
Previous: Networking with Perl
dave@cs.cf.ac.uk