www.gibmonks.com

  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 17.  Socket Programming


Client Sockets

A client opens a socket by specifying the host address and port number for the server of the socket. The host address gives the network location (i.e., which computer), and the port selects a particular server from all the possible servers that may be running on that host. For example, HTTP servers typically use port 80, while FTP servers use port 20. The following example shows how to open a client socket to a Web server:

set s [socket www.scriptics.com 80]

There are two forms for host names. The previous example uses a domain name: www.scriptics.com. You can also specify raw IP addresses, which are specified with four dot-separated integers (e.g., 128.15.115.32). A domain name is mapped into a raw IP address by the system software, and it is almost always a better idea to use a domain name in case the IP address assignment for the host changes. This can happen when hosts are upgraded or they move to a different part of the network. As of Tcl 8.2, there is no direct access from Tcl to the DNS service that maps host names to IP addresses. You'll need to use Scotty to get DNS access.

Some systems also provide symbolic names for well-known port numbers. For example, instead of using 20 for the FTP service, you can use ftp. On UNIX systems, the well-known port numbers are listed in the file named /etc/services.

Client Socket Options

The socket command accepts some optional arguments when opening the client-side socket. The general form of the command is:

socket ?-async? ?-myaddr address? ?-myport myport? host port

Ordinarily the address and port on the client side are chosen automatically. If your computer has multiple network interfaces, you can select one with the -myaddr option. The address value can be a domain name or an IP address. If your application needs a specific client port, it can choose one with the -myport option. If the port is in use, the socket command will raise an error.

The -async option causes connection to happen in the background, and the socket command returns immediately. The socket becomes writable when the connection completes, or fails. You can use fileevent to get a callback when this occurs. This is shown in Example 17-1. If you use the socket before the connection completes, and the socket is in blocking mode, then Tcl automatically blocks and waits for the connection to complete. If the socket is in nonblocking mode, attempts to use the socket return immediately. The gets and read commands would return -1, and fblocked would return 1 in this situation.

In some cases, it can take a long time to open the connection to the server. Usually this occurs when the server host is down, and it may take longer than you want for the connection to time out. The following example sets up a timer with after so that you can choose your own timeout limit on the connection:

Example 17-1 Opening a client socket with a timeout.
proc Socket_Client {host port timeout} {
   global connected
   after $timeout {set connected timeout}
   set sock [socket -async $host $port]
   fileevent $sock w {set connected ok}
   vwait connected
   if {$connected == "timeout"} {
      return -code error timeout
   } else {
      return $sock
   }
}

      Previous section   Next section
    Top