Sockets: Client/Server Communication

While not limited to Unix-derived operating systems (e.g. WinSock on PCs provides socket support, as do some VMS libraries), you may not have sockets on your system, in which this section probably isn't going to do you much good. With sockets, you can do both virtual circuits (i.e. TCP streams) and datagrams (i.e. UDP packets). You may be able to do even more depending on your system.

The Perl function calls for dealing with sockets have the same names as the corresponding system calls in C, but their arguments tend to differ for two reasons: first, Perl filehandles work differently than C file descriptors. Second, Perl already knows the length of its strings, so you don't need to pass that information.

One of the major problems with old socket code in Perl was that it used hard-coded values for some of the constants, which severely hurt portability. If you ever see code that does anything like explicitly setting $AF_INET = 2, you know you're in for big trouble: An immeasurably superior approach is to use the Socket module, which more reliably grants access to various constants and functions you'll need.

overview / signals / fifos / pipes / safe-pipes / open2 / AF_INET / AF_UNIX / udp / sys5