Network Programming


Sockets

Socket types

1) SOCK_STREAM - similar to pipes

2) SOCK_DGRAM - only garantee that message borders will be preserverd when read (but lower overhead)

Domains

1) PF_INET - socket is identified by host (IP address) and port

2) PF_UNIX - .. by filename (ex. /tmp/mysock)

Domains and types are identified by symbolic names above (that are mapped to numeric constants) which are constants exported by Socket and IO::Socket.

Protocols

There’s rarely more than one protocol for the given domain and type of socket.

1) tcp

2) udp

Protocols have names that correspond to numbers used by the OS. getprotobyname() (built into Perl) returns these numbers:

$ perl -le 'print "$_ -> ", scalar getprotobyname $_ for qw(tcp udp)'
tcp -> 6
udp -> 17

Perl’s built-in functions

Workflows

Typical SERVER

  1. socket()
  2. bind() and listen()
  3. loop in a blocking accept() waiting for incoming connections

Typical CLIENT

  1. socket() and connect()

.. datagram clients don’t need to connect(); they specify the destination as argument to send()

UDP client bind() vs connect()

Sources

comments powered by Disqus