meta data for this page
  •  

This is an old revision of the document!


Network programming @ Noppa | @ wiki - Updated: — Jussi Laakkonen 2011/09/02 11:47 View page as slide show

CT30A5000 - Network Programming

Introduction exercises on 2.9.2011

2011 - 2012

M.Sc Jussi Laakkonen (Room: 2517)

Exercise and contact times

Exercises:

  • 1st period (Weeks 35,37,39,40) - Fridays 1415 - 1600
  • 2nd period (Weeks 43,45,47) - Fridays 1415 - 1600

Additionally: two days (8.12.2011 & 9.12.2011) are reserved for Home examination demonstration.

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

Arrangements

  • Six (6) programming assignments
  • One home examination.
  • INVIDUAL WORK ! NO COPYING !
  • 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 makefile
  • 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

  • Send the assignment before deadline! Late return is always 0 points.
    • Each assignment must be returned in order to pass the course.
    • Last minute changes (e.g. fixing bugs):
      • The “buggy” version must be returned by deadline.
      • Must be returned within 6 hours from the deadline. Only one bugfix version can be returned!
      • WITH documentation of changes.
  • Send to assistant as an email attachment: gzipped tar-ball (file.tar.gz) (see man tar)

The assignment file

  • Gzipped tar-ball
  • Naming of the file: studentnumber_firstname_lastname.tar.gz
    • The top level of your archive should be just one directory, which is named as archive itself.
    • The directory should contain:
      • Code
      • Headers
      • Readme
      • Makefile
      • NO BINARY FILES

Getting started

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

http://www2.it.lut.fi/wiki/doku.php/courses/ct30a5000/start#instructions

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

http://www2.it.lut.fi/wiki/doku.php/courses/ct30a5000/start#documents_standards_tutorials

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 Vim

  • Terminal editor, starts in normal mode (editing not enabled)
  • There is also Vim with GUI: gvim
  • Usage: vim 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 gcc.gnu.org
  • 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:
      all:
         gcc helloworld.c -o helloworld
    • Remember: indentation with tab not spaces
    • Compile with:
      make

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.
    while(1)
    {
      /*code*/
    }
  • 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:

http://www2.it.lut.fi/wiki/doku.php/admin/studentinfo

See information about class 6218:

http://www2.it.lut.fi/wiki/doku.php/admin/pentinkulma

Remote usage - howto

Connecting with ssh outside university:

  1. Connect with ssh to ssh.lut.fi with your university account (connections from Lnet are allowed).
    • E.g. ssh 0123456@ssh.lut.fi
  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 0123456@ssh.lut.fi
    • Note: DO NOT RUN ANY OTHER PROGRAMS THAN SSH, SCP OR RLOGIN ON ssh.lut.fi machine
  • Enable X11 forwarding for every connection.
  • Note: if using cygwin, use -Y instead of -X

Helpful sources, books..

  • Beej's Guide to Network Programming (do not use getaddrinfo() for getting local IP addresses, use getifaddrs() instead)
  • TCP/IP illustrated
  • TCP/IP Sockets in C: Practical Guide for Programmers
  • Linux Socket Programming by Example
  • UNIX Network Programming: The Sockets Networking API Volume 1
  • 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.

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: udpexample.tar.gz

  • creates a folder “udpexample”
  • build with make