Network programming @ Noppa | @ wiki - Updated: — Jussi Laakkonen 2012/08/20 17:40 View page as slide show

CT30A5001 - Network Programming

Introduction exercises on 31.8.2012

2012 - 2013

M.Sc Jussi Laakkonen (Room: 2517)

Exercise and contact times


  • 1st period (Weeks 35,37,39,41) - Fridays 1215 - 1400
  • 2nd period (Weeks 43,44,46) - Fridays 1215 - 1400

Additionally: two days (4.12.2011 & 5.12.2011) are reserved for Home examination demonstration.

Reception hour: Wednesday 1500 - 1600 (Room: 2517)


  • Six (6) programming assignments
  • One home examination.
  • It is required to attend to minimum of 5 out of 6 exercises.
    • Introduction lectures do not count.
    • All 6 assignments must be returned.
  • At every exercise:
    • Couple of persons will be selected to present and EXPLAIN their code
    • Discussion about solutions, possible enhancements, etc.

Requirements for assignments

  • C language, GCC compiler, Linux (can be run on 6218 computers)
    • Compiles without errors or warnings (it would be good to follow ANSI C99 but not required on this course, use -std=gnu99)
    • Program runs without errors
  • Comment your code (add name, student number and email to every file)
  • Document your work (README: how to run, what has been done, own ideas)
  • Use a makefile (mandatory)
  • Use good structure for your code (divide code to functions and files)

Requirements for applications

  • Program accepts appropriate parameters and handles them correctly.
  • Program understands required commands and discards unknown commands.
  • Connections can be created and maintained properly.
  • Program can keep track of its state (connected, disconnected, sending data, receiving data etc.).
  • Program discards (or reports) if invalid data is received from network.
  • Program fulfills given assignment requirements.

Evaluation of assignments

  • Maximum number of points per assignment is 11
  • Bonus points can be gained from:
    • Implementing OPTIONAL features in assignment description
    • Own ideas and/or implementations
    • Answering to given guestions, well written and well reasoned answers might be rewarded.

Sending an assignment

  • Put your code into SVN repository at
  • Your login name is mapped to folder with your student number (read/write)
  • Code will be checked out at deadline
    • only then, not before, not after
  • Put into following directories:
    • A<n>, where <n> is the assignment number
    • E.g. Assignment 1 into A1, assignment 2 into A2, etc.
    • Except Home Examination, it goes into HE directory

The directory content

  • The directory should contain:
    • Code (documented in english or in finnish)
    • Headers
    • Readme (in english or in finnish)
    • Makefile

Getting started

Before starting any assignment, read the instruction section from course main pages:

See also additional documents, tutorials etc. from wiki pages:

1. Editors

  • There are plenty of options, choose the one you like/know
  • Vim/Emacs/gedit/nano/…
    • Note: Emacs might seem hard in the beginning..
    • Note: nano might not show all characters → annoying “bug hunt”
  • See text_editors for information and tutorials
  • Use e.g. helloworld.tar.gz for testing

1.1 Vi / Vim

  • Terminal editor, starts in normal mode (editing not enabled)
  • There is also Vim with GUI: gvim
  • Usage: vim sourcecode.c or vi sourcecode.c
  • Some basic commands:
    a      - append (start editing)
    i      - insert (start editing)
    esc    - quit editing
    :w     - save file
    :help  - help
    :q     - close help 
    :wq    - save and quit
  • Esc -key quits current activity

1.2 Gedit

  • Graphical editor, start with gedit
    • E.g. gedit source1.c source2.c header.h
    • E.g with wildcards gedit *.c (all files ending with .c)
  • Display line numbers, matching brackets, etc.
    • Edit → Preferences → View
  • Enable automatic intendation:
    • Edit → Preferences → Editor
  • Enable external commands such as make:
    • Edit → Preferences → External tools
    • Configure plugin for your purposes..
  • Creates ~ -temp files, delete them before sending the assignment.

1.3 Editor hints

  • When using computers remotely use terminal editor (Vim/Emacs/nano)
  • Learn about screen -command (man screen)
    • Start command in new terminal screen:
      screen -S ScreenName ProgramToRun
      • E.g. start Vim in screen named EDITOR:
        screen -S EDITOR vim
    • Detach screen session: Ctrl-A Ctrl-D
    • Resume session, e.g. resume editor:
      screen -r EDITOR

2. GCC

  • C-compiler (Gnu Project C and C++ Compiler)
  • See man gcc or go to
  • Create a small test program and try to compile it.
  • What flags we need for compiling c-code? How about debugging?
  • Example (using helloworld.tar.gz):
    gcc helloworld.c -o helloworld

3. Make and makefiles

  • “The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them.”
  • See man make
  • Makefiles:
    • Describes relationships among program.
    • Example:
         gcc helloworld.c -o helloworld
    • Remember: indentation with tab not spaces
    • Compile with:

4. GDB

  • The Gnu Debugger, can:
    • Start the program, give parameters
      gdb --args program arguments
    • Stop program on specified conditions
    • Examine what has happened
    • Change things in the program
  • When something goes wrong, use this to see what went wrong (debug).
    • E.g. “mystic” segmentation faults etc.
  • See man gdb for quick info (try ddd also, debugger with GUI)
  • Use -g switch when compiling with gcc to enable debug symbols

4.1 GDB commands

  • list = lists loaded code (list x = lists code around point x)
  • break n/try = sets breakpoint to line n / breakpoint to function try
  • step = runs next line of code
  • next = same as previous but does not enter functions
  • continue = continue execution, stops at next breakpoint
  • where = shows current position
  • quit = quits debugging
  • Ctrl –c ends running program

4.2 Use GDB

  • Add an infinite loop to your test program and debug it with GDB, e.g.
  • Stop the program in GDB with Ctrl-C (sends SIGINT)
  • Run your program step by step with GDB
  • Add variables to test program and learn about how to print the values of these variables in GDB

5. Valgrind

  • A program for debugging and profiling
  • Memory debugger
  • See man valgrind
  • Test if your program has memory leaks, e.g.
    valgrind ./helloworld
  • Add a memory leak of one byte:
    #include <stdlib.h>
    /* some code */
    char *cpointer = (char *)malloc(sizeof(char));
  • See what valgrind reports after this. How to remove this memory leak?

Use computers remotely: rules

When using these computers you must follow the rules set by university and department administrators.

See departmental information for students:

See information about class 6218:

Remote usage - howto


Connecting with ssh outside university:

  1. Connect with ssh to with your university account (connections from Lnet are allowed).
    • E.g. ssh
  2. After login connect with ssh to selected class 6218 computer

Remote usage - graphical

If you have X-server running on your computer you can use graphical applications via ssh:

  • Use X11 forwarding (in order to use graphical applications remotely), use e.g. ssh -X
  • Enable X11 forwarding for every connection.
  • Note: if using cygwin, use -Y instead of -X

Helpful sources, books..

  • Beej's Guide to Network Programming (read the usage of getaddrinfo() carefully, for getting local IP addresses see also getifaddrs())
  • IPv6 Network Programming, Jun-Ichiro Itojun Hagino
    • Chapters: 1.4, 2.1, 2.2, 2.3, 4.1, 4.2, 4.3, 4.4, 4.5, 4.7
  • TCP/IP illustrated, W. Richard Stevens
  • TCP/IP Sockets in C: Practical Guide for Programmers, Michael J. Donahoo, Kenneth L. Calvert
  • UNIX Network Programming: The Sockets Networking API Volume 1, W. Richard Stevens,Bill Fenner,Andrew M. Rudoff
  • But be aware and beware: “GNU is not UNIX and Linux is not UNIX” && “Unix is a Linux-like operating system”, Jon “Maddog” Hall

Example codes

Hello World!

Simple Hello World example including makefile.

NEW: udpexample2012.tar.gz

OLD: helloworld.tar.gz

  • Extract:
    tar xzf helloworld.tar.gz
    • creates a directory “helloworld”
  • See README-file in helloworld -directory

UDP client and server

Simple UDP client and server example.

  • Server listens a character from some client and quits.
  • Client sends a character to given address and quits.

Unpack: udpexample2012.tar.gz

  • creates a folder “udpexample”
  • build with make

TCP client and server

Simple TCP client and server example.

  • Server listens for incoming connections and disconnects them right after accept().
  • Server prints address information of the connected client.
  • Client just connects and quits right after disconnection.

Unpack: tcpexample2012.tar.gz

  • Creates a folder “tcpexample”
  • build with make