next up previous contents
Next: Some Network Examples Up: Clients and Servers Previous: The Server Side of

The Client Side of a Conversation

Basic clinet connection to a server is established as follows:

The socket() call for the client program is the same as that used in the server, client.pl:

$tcpProtocolNumber = getprotobyname('tcp') || 6;
socket(SOCKET, PF_INET(), SOCK_STREAM(), $tcpProtocolNumber)
    || die("socket: $!");

After the socket is created, the connect() function is called like this, connect.pl:

$port = 20001;
$internetPackedAddress = pack('Sna4x8', AF_INET(), $port, "\0\0\0\0");


connect(SOCKET, $internetPackedAddress) or die("connect: $!");

The packed address was explained in "The Server Side of a Conversation." The SOCKET parameter has no relation to the name used on the server machine. I use SOCKET on both sides for convenience.

The connect() function is a blocking function. This means that it will wait until the connection is completed. You can use the select() function to set non-blocking mode, but you'll need to look in the UNIX documentation to find out how. It's a bit complicated to explain here.

After the connection is made, you use the normal input/output functions or the send() and recv() functions to talk with the server.


next up previous contents
Next: Some Network Examples Up: Clients and Servers Previous: The Server Side of
dave@cs.cf.ac.uk