Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA114      CA170

CA668      CA669      Projects


6.1.3 Sockets

Sockets are a service provided by transport layer.
Set of primitives to enable a bi-directional comms link between A and B.

Primitive socket commands in TCP.

  1. Server side: Server startup executes SOCKET, BIND, LISTEN.
    LISTEN - allocate queue for multiple simultaneous clients.

    ACCEPT - suspend server until request. When client request arrives: ACCEPT returns.
    Start new socket (thread or process) with same properties as original, this handles the request,
    server goes on waiting on original socket.
    If new request arrives while spawning thread for this one, it is queued.
    If queue full it is refused.

  2. Client side: SOCKET to create. Then CONNECT. When this returns the socket is open.

    Both sides can now SEND, RECEIVE.

    Connection not released until both sides do CLOSE. Typically client does it, server acks.


Server code in C

A simple file server in C, explained in 6.1.4:

Infinite loop. Can only be stopped by external kill (end process).
ACCEPT returns - client has connected.
Can both read from and write to the "socket address" sa.

Client sends the file name it wants.
Server writes the file to the socket and then closes the socket.
Back to infinite loop: ACCEPT - suspend waiting for next request.

Client code in C

The client for the file server:


$ client host filename 
returns file contents to stdout.

ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.