The readings for April 24, 2017 are:
You can skip parts 6 and 7 and the section about RPC.
Beej's Guide to Network Programming
You can skip the parts about datagram sockets and slightly advanced techniques.
The focus of this reading is to introduce basic system calls dealing with sockets in C.
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.
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.
In your reading11/README.md
, response to the following questions:
Identify which system calls you would use to perform the following action:
Creating a file descriptor or endpoint for communicating over the network.
Lookup the address information for a particular machine or service.
Assign an address to a server network file descriptor (aka assign a name to a socket).
Convert the network address structure into corresponding host and service strings.
Establish a client network file descriptor to a specified address.
Mark network file descriptor as waiting for incoming connections.
Create a new network file descriptor based on a incoming connection.
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
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:
Compile with no warnings (be sure to have -Wall
in your CFLAGS
).
Not contain any memory errors (as reported by Valgrind).
Check if system calls fail (when appropriate).
Consider using the following system calls and functions:
Use getaddrinfo lookup the address information of a remote host.
Use socket to create a network file descriptor.
Use connect to connect the network file descriptor to the remote host.
Use fdopen to convert the file descriptor to a stream.
Your code should resemble the Simple Stream Client from your reading.
If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.
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:
Makefile
README.md
ncat.c