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

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

2007年度秋学期 火曜日2時限
科目コード: 13070 / 2単位
カテゴリ:
開講場所:SFC
授業形態:講義
担当: Rodney Van Meter
E-mail: rdv@sfc.keio.ac.jp
Today is the ninth anniversary of the death of Jon Postel. Jon, we miss you, and we miss your guidance.

Jon Postel

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

Outline of This Lecture

Review of last week's homework

Problem 2 in the homework last week asked you to do the following:

Your solution should look something like this:

#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;
}

void PrintElementList(struct element *in) {
  printf("elementNumber: %d\n",in->elementNumber);
  if (in->next)
    PrintElementList(in->next);
}

void FreeElementList(struct element *in) {
  if (in->next)
    FreeElementList(in->next);
  free(in);
}

main()
{
  struct element *mylist;

  tmp = MakeElementList(10);
  if (!tmp)
    exit(-1);
  PrintElementList(tmp);
  FreeElementList(tmp);
}

Tools and the Software Development Process

We will not have time to go into these topics in detail, but they are worth mentioning:

The TCP/IP Protocol Stack in the Internet

"Network programming" is a broad title. It could refer to any networking protocol: DECNET, SNA, OSI, ATM. In this class, we will focus only on Internet Protocol (IP) networking, and the TCP/IP suite of related protocols built around it.

From Wikipedia:

IP protocol stack diagram

Internet Protocol addressing (v4/v6)

The Internet is known as a packet switched network, rather than circuit switched. Data on the Internet is transferred in IP packets. Each packet is identified by its source and its destination. Normally, the source and destination identifiers each contain: The lowest layer we will concern ourselves with (for the moment, at least) is the network layer, which in our case is the Internet protocol. From Wikipedia, the IPv4 packet header:

IPv4 packet header

And the IPv6 packet header:

IPv6 packet header

After the IP header (whether v4 or v6), comes the transport-layer header. The only part of the transport-layer header we are interested in at the moment is the port number.

Request/Response (Client/Server) Operation

Most Internet-based applications behave in a request/response manner. Typically, though not always, requests are small, and responses are large. Most of the time, the requestor is a client, such as regular PC, and the responder is a server, such as a web server.

Datagram versus stream

When the response to a request is expected to be long, an application will usually want to open a stream. For simple, short requests, a datagram may be good enough. We must learn how to do both types of network operations.

宿題
Homework

This week's homework (submit via email):

  1. Last week, you were supposed to create three source files and link them together for your program. This week, we are going to add unit test. We need two more source code files: Each of those should include a main() function that calls each function in the corresponding source file to test that it works. Now you must compile three separate programs: hotpotato, mylibtest, and networkingtest. Show that each one runs.
  2. The homework "answer" at the top of this page does not actually work. Please copy that to your computer, and debug it. (You may also want to use this as an opportunity to try a source code control system, such as RCS, and my preference is that you send me the solution to this problem as a diff or rcsdiff from the existing program.)

Next Lecture

第4回 10月23日
Lecture 4, October 23: Basic socket programming 1

Additional Information

その他