慶應義塾大学
2007年度 秋学期

ネットワーク・プログラミング(C言語)
Network Programming in C

2007年度秋学期 火曜日2時限
科目コード: 13070 / 2単位
カテゴリ:
開講場所:SFC
授業形態:講義
担当: Rodney Van Meter
E-mail: rdv@sfc.keio.ac.jp

Outline of This Lecture

はじめに
C: Start at the Beginning...

#include <stdio.h>

main()
{
  printf("Hello, world.\n");
}

Basic Modularity

Last week we went through the basics of modularity in C:

Fundamental Data Types

int a = 17;
char b = 'b';
double pi = 3.14159;
C also has the ability to manipulate individual bits, which is one of its primary attractions to systems developers, because it helps access hardware devices. Bitfields are usually stored in unsigned int or unsigned char types.

Data Structures

Programs use data structures to hold related pieces of data: the name, address, and age of a person, for example. In C, the principle means of defining data structures is through the keyword struct.
struct person {
  char name[50];
  char address[200];
  int age;
}

Execution Control Flow

Conditional Execution

#include <stdlib.h>
#include <time.h>

srandom(time(NULL));
if ( i = random() >= RAND_MAX / 2 ) {
  printf("big: %d\n",i);
} else {
  printf("little: %d\n",i);
}
#define RED 1
#define BLUE 2
#define GREEN 3

switch ( color ) {
case RED:
  printf("red\n");
  break;
case BLUE:
  printf("blue\n");
  break;
case GREEN:
  printf("green\n");
  break;
default:
  printf("unknown color\n");
}

Loops

#define LOOPS 10
int i, sum = 0;

for ( i = 0 ; i < LOOPS ; i++ ) {
  sum += i;
}
printf("sum: %d\n",sum);
#define LOOPS 10
int i = 0, sum = 0;

while ( i < LOOPS ) {
  i++;
  sum += i;
}
printf("sum: %d\n",sum);
#define LOOPS 10
int i = 0, sum = 0;

do {
  i++;
  sum += i;
} while ( i < LOOPS );

printf("sum: %d\n",sum);

Using the Preprocessor

So far, we have see only one use of the C preprocessor: the #include directive. You will frequently want to use the #define directive, as well:

#define PI 3.14159

Pointers

#include <stdio.h>

int a = 3;
int *ap = &a;

main()
{
  printf("ap: 0x%x &ap: 0x%x\n", ap, &ap);

  *ap = 5;
  printf("assigning...\n");
  printf("a: %d *ap: %d\n", a, *ap);
}
[rdv@localhost network-programming-in-c]$ gcc -o addr addr.c
[rdv@localhost network-programming-in-c]$ ./addr 
a: 3 *ap: 3
ap: 0x80496e4 &ap: 0x80496e8
assigning...
a: 5 *ap: 5

Recursion (and Memory Management)

unsigned int factorial(unsigned int n) {
    if (n <= 1) return 1;
    return n * factorial(n-1);
}
#include <errno.h>

struct element {
  struct element *next;
  int elementNumber;
}

struct element *MakeElementList(int n) {
  struct element *tmp = malloc(sizeof(struct element));

  if (!tmp) {
    perror("MakeElementList"); exit(-1);
  }
  if (!n) {
    tmp->next = NULL;
  else {
    tmp->next = MakeElementList(n-1);
  }
  tmp->elementNumber = n;
  return tmp;
}

Tools

宿題
Homework

This week's homework (submit via email):

  1. Begin your term project. Create three source files with the following functions: Compile and run your program. main() should call both SleepALittle() and OpenConnectionToServer(). The other two functions can be dummy functions for now.
  2. Above, we defined the function MakeElementList(). Write a program including the function FreeElementList(), using the library function free().

Next Lecture

第3回 10月22日
Lecture 3, October 16: IP Networking Basics

Note: I will be in L.A. and will lecture remotely. The room for the lecture has not yet been determined.

その他