Client/Server Computing Foundations: Sockets

Tom Kelliher, CS 318

Mar. 27, 2000

Administrivia

Announcements

Midterm in one week.

Assignment

Read 25.1--25.7 (C version of the Perl client/server application.)

Read 24.8.

Read Chapter 29.

From Last Time

Client/server interactions between a web server and a browser.

Outline

  1. Client/Server dialogue

  2. Small client/server example: runs, code walk-through.

  3. Perl socket functions.

Coming Up

Continuation of sockets programming, web server project.

Example Client/Server Application

A Client-Server Dialogue

Example Runs

  1. Examples interacting with each other.

  2. Server interacting with telnet.

  3. Client interacting with daytime.

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
Mon Mar 27 12:19:25 EST 2000
Tom Kelliher