Socket Functions Related to Outgoing Connections


 
Network Programming with Perl
By Lincoln  D.  Stein
Slots : 1
Table of Contents
Chapter  4.   The TCP Protocol

    Content

Socket Functions Related to Outgoing Connections

We'll now look at the functions related to creating sockets and establishing outgoing TCP connections in more detail.

$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.

Table 4.1. Shutdown() Values
Value of HOW Description
Closes socket for reading
1 Closes socket for writing
2 Closes socket completely

In addition to its ability to half-close a socket, shutdown() has one other advantage over close() . If the process has called fork() at any point, there may be copies of the socket filehandle in the original process's children. A conventional close() on any of the copies does not actually close the socket until all other copies are closed as well (filehandles have the same behavior). In consequence, the client at the other end of the connection won't receive an EOF until both the parent and its child process(es) have closed their copies. In contrast, shutdown() closes all copies of the socket, sending the EOF immediately. We'll take advantage of this feature several times in the course of this book.


   
Top


Network Programming with Perl
Network Programming with Perl
ISBN: 0201615711
EAN: 2147483647
Year: 2000
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net