Client/Server Computing Foundations: Sockets

Tom Kelliher, CS 318

Mar. 29, 2000

Administrivia

Announcements

Midterm in one week.

Assignment

Midterm readings: Chs. 1--5, 7--12, 14--16, 23--25, 29.

From Last Time

Client/server communication diagram, client/server example code.

Outline

  1. Client/server code walk-throughs.

  2. Perl socket functions.

Coming Up

Midterm review.

Example Client/Server Application

Code Walk-Through

server.pl. Note the following:

  1. Taint checking is good, but optional.

  2. The SIGTERM handler.

  3. Untainting a variable: regular expressions.

  4. $!: errno.

client.pl. Note:

  1. Length of the receive buffer.

  2. Getting a port number by name, /etc/services.

  3. Message fragmentation.

Perl Socket Functions

Check return values!!!

socket()

Create a socket.

socket(SOCKETHANDLE, PF_INET, SOCK_STREAM, getprotobyname("tcp"));

setsockopt(SOCKETHANDLE, SOL_SOCKET, SO_REUSEADDR, 1);
Parameters:
  1. Socket handle.

  2. The other parameters should be used as-is.

  3. Returns success/failure.

bind()

Bind a socket to a port and IP address.

bind(SOCKETHANDLE, sockaddr_in($sport, INADDR_ANY));
Parameters:
  1. Socket handle.

  2. Port number.

  3. Bind to any available IP address on host.

  4. Returns success/failure.

listen()

Set length of socket's request queue.

listen(SOCKETHANDLE, SOMAXCONN)
Parameters:
  1. Socket handle.

  2. 64(?) queued connections.

accept()

Wait for and accept a connection request from a client.

accept(CONNECTION, SOCKETHANDLE)
Parameters:
  1. Socket handle used for communicating.

  2. Socket handle used for listening.

  3. Returns packed address of client. Use sockaddr_in() to decode.

send()

Send a message through a socket.

send(CONNECTION, $message, 0);
Parameters:
  1. Socket handle used for communicating.

  2. Message. Keep under 1,400 bytes.

  3. Zero.

  4. Returns the number of bytes sent or the undefined value on an error.

close()

Close a socket.

close(CONNECTION);
Parameters:
  1. Socket handle used for communicating.

connect()

Connect to a server.

connect(SHANDLE, sockaddr($sport, $sipaddr));
Parameters:
  1. Socket handle.

  2. Server, port, IP address.

  3. Returns success/failure.

recv()

Receive a (portion of a) message.

recv(SHANDLE, $message, $length, 0);
Parameters:
  1. Socket handle.

  2. Message buffer.

  3. Maximum number of bytes to receive.

  4. Zero.

  5. Returns success/failure.

Re-walk the client/server code at this point.



Thomas P. Kelliher
Wed Mar 29 12:38:30 EST 2000
Tom Kelliher