Readings

The readings for April 24, 2017 are:

  1. System Programming Wiki

    You can skip parts 6 and 7 and the section about RPC.

  2. Beej's Guide to Network Programming

    You can skip the parts about datagram sockets and slightly advanced techniques.

TL;DR

The focus of this reading is to introduce basic system calls dealing with sockets in C.

Optional Resources

  1. Let's Build A Web Server. Part 1, Part 2, and Part 3

    This is a series of articles that describe how to build a HTTP server in Python. It has much more information than you need for Project 02, but it provides a lot of illustrations and explanations. Read this to get an idea of what you will be doing, but don't get bogged down by the details.

  2. Learn Socket Programming in C from Scratch

    This is free online course from Udemy that covers most of the same things we will in class. Moreover, they also cover building a HTTP server as well.

Activities

In your reading11/README.md, response to the following questions:

  1. Identify which system calls you would use to perform the following action:

    1. Creating a file descriptor or endpoint for communicating over the network.

    2. Lookup the address information for a particular machine or service.

    3. Assign an address to a server network file descriptor (aka assign a name to a socket).

    4. Convert the network address structure into corresponding host and service strings.

    5. Establish a client network file descriptor to a specified address.

    6. Mark network file descriptor as waiting for incoming connections.

    7. Create a new network file descriptor based on a incoming connection.

  2. Write the C equivalent, ncat.c, to the Python code below (ncat.py):

    #!/usr/bin/env python2.7
    
    import socket
    import sys
    
    # Parse command line options
    try:
        HOST = sys.argv[1]
        PORT = int(sys.argv[2])
    except IndexError:
        print >>sys.stderr, "Usage: {} HOST PORT".format(sys.argv[0])
        sys.exit(1)
    
    # Create socket and connect to specified HOST and PORT
    csocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    csocket.connect((HOST, PORT))
    cstream = csocket.makefile('w+')
    
    # Read from stdin and write to socket
    for line in sys.stdin:
        cstream.write(line)
    
    # Cleanup
    csocket.close()
    

    You should be able to use the program as follows:

    # In first terminal, start local server using real nc
    $ nc -l 9000
    
    # In second terminal, run ncat.py to send copy of itself
    $ ./ncat.py localhost 9000 < ncat.py
    

    Makefile

    Be sure to include a Makefile that builds the ncat.c program:

    # Build ncat program
    $ make
    Compiling ncat.o...
    Linking ncat...
    
    # Test ncat program
    $ nc -l 9000                   # In one terminal
    
    $ ./ncat localhost 9000 < ncat.c  # In another terminal
    

    Your program must:

    1. Compile with no warnings (be sure to have -Wall in your CFLAGS).

    2. Not contain any memory errors (as reported by Valgrind).

    3. Check if system calls fail (when appropriate).

    Hints

    Consider using the following system calls and functions:

    Your code should resemble the Simple Stream Client from your reading.

Feedback

If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.

Submission

To submit your assignment, please commit your work to the reading11 folder in your assignments GitLab repository. Your reading11 folder should only contain the following files: