View page as slide show FIXME

CT30A5002 - Games and Networking

Introduction exercises on 30.9.2014

2014 - 2015

M.Sc Jussi Laakkonen (Room: 2517)

Exercise and contact times


  • 1st period (Weeks 40 and 42) - On Tuesdays 1215 - 1400
  • 2nd period (Weeks 44-49) - On Tuesdays 1215 - 1400
  • 3rd period NO EXERCISES

Additionally: a day (17.2.2015) is reserved for 3rd assignment (group work) demonstration.

Reception hour: Wednesday 1615 - 1715 (Room: 2517)


  • Exercises with small tasks
    • Twice on 1st period (week 42)
    • Weekly on 2nd period
  • Two individual programming assignments
    • Will be briefly looked at the exercises
  • One larger group work.
    • Every group has to demonstrate the work
  • At every exercise:
    • Couple of persons will be selected to present and EXPLAIN their solution
    • Discussion about solutions, possible enhancements, etc.
    • 2 activity points for continuous evaluation / exercise

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 individual assignment is 15
  • 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.
  • Group work is 40 points.

Sending an assignment / weekly exercise

  • 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 / before exercises
    • only then, not before, not after
  • Put into following directories:
    • Exercises:
      • Ex<n>, where <n> = assignment number
      • E.g. Exercise 3 into Ex3, 4 into Ex4, etc.
    • Assignments:
      • A<n>, where <n> = assignment number
      • E.g. Assignment 1 into A1, assignment 2 into A2, etc.
      • Except the group work - a group access will be granted later

The directory content

  • The directory should contain:
    • Code (documented in english or in finnish)
    • Headers
    • Readme or answers (in english or in finnish) as plain text
    • 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”
    • Graphical IDEs are fine - do not expect any support in problem situations regarding the IDE use
  • 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:

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?

Computer use: 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:

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 (e-book), 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