$boolean = socket (SOCKET,$domain,$type,$protocol) Given a filehandle name , a domain, a type, and a protocol, socket() creates a new socket and associates it with the named filehandle. On success, the function returns a true value. On error, socket() returns undef and leaves the error message in $! . The domain, type, and protocol are all small integers. Appropriate values for the first two are constants defined in the Socket module, but the protocol value must be determined at run time by calling getprotobyname () . For creating TCP sockets, the idiom is typically socket(SOCK,AF_INET,SOCK_STREAM, scalar getprotobyname("tcp")) Here we force getprotobyname() into a scalar context in order to return a single function result containing the protocol number. $boolean = connect (SOCK,$dest_addr) The connect() function attempts to connect a connection-oriented socket to the indicated destination address. The socket must already have been created with socket() , and the packed destination address created by sockaddr_in() or equivalent. The system will automatically choose an ephemeral port to use for the socket's local address. On success, connect() returns a true value. Otherwise, connect() returns false and $! is set to the system error code explaining the problem. It is illegal for a connection-oriented socket to call connect() more than once; if another call is attempted, it results in an EISCONN ("Transport endpoint is already connected") error. $boolean = close (SOCK) The close() call works with sockets just like it does with ordinary filehandles. The socket is closed for business. Once closed, the socket can no longer be read from or written to. On success, close() returns a true value. Otherwise, it returns undef and leaves an error message in $! . The effect of close() on the other end of the connection is similar to the effect of closing a pipe. After the socket is closed, any further reads on the socket at the other end return an end of file (EOF). Any further writes result in a PIPE exception. $boolean = shutdown (SOCK, $how) shutdown() is a more precise version of close() that allows you to control which part of the bidirectional connection to shut down. The first argument is a connected socket. The second argument, $how , is a small integer that indicates which direction to shut down. As summarized in Table 4.1, a $how of 0 closes the socket for further reads, a value of 1 closes the socket for writes, and a value of 2 closes the socket for both reading and writing (like close() ). A nonzero return value indicates that the shutdown was successful. |