network programming in c #1: introduction ネットワークプログラミング c 言語...

45
Network Programming in C #1: Introduction ネネネネネネネネネネネネネ C ネネ ネネネ ネネネネネネネネネネ 「」 20 11 ネネネネ Rodney Van Meter [email protected]

Upload: serina-shepard

Post on 03-Jan-2016

53 views

Category:

Documents


0 download

DESCRIPTION

20 11 年秋学期 Rodney Van Meter [email protected]. Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」. Today’s Lecture. This course Goals Outline Staff introductions Basics of networking Exercise. 本講義の位置づけ (2 単位 ). 先端支援系. 創造支援系. 卒業プロジェクト. 創造技法 関連科目 (コンピュータ基礎と - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Network Programming in C#1: IntroductionネットワークプログラミングC言語第1回「イントロダクション」

20 11年秋学期Rodney Van [email protected]

Page 2: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Today’s Lecture

This course Goals Outline Staff introductions

Basics of networking Exercise

Page 3: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

本講義の位置づけ (2単位 )

創造実践 総合講座 先端発見関連科目 (インターネット)

シフト系

リフレッシャー科目

研究会

卒業プロジェクト先端支援系創造支援系

創造融発創造技法

関連科目(コンピュータ基礎とプログラミング)

プログラミングスキル

先端開拓関連科目(インターネットシステム構成法)

先端導入

関連科目(ネットワークアーキテクチャ)(インターネットオペレーション)

Page 4: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Goals Who can take this class?

Almost anyone Learn basic structure of network services Programming in C

Learn some of the “joseki” (定石 ) of C Build experience with programming

Doing is more important than listening! Software engineering in a team

Makefiles, version control, interfaces, modularity, documentation, debugging,

Page 5: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Who’s this for?

Who might want to take this class? Almost anyone working around IT Even if your goal is to be a VC or found an NGO,

understanding this helps you talk intelligently to networking people

Who should take this class? Anyone planning to work in computer

systems (network manager/sysadmin, application or OS developer, researcher)

Page 6: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Prerequisites (前提 ) Minimum

Have taken classes in Information Processing (情報処理 )

「コン基礎」 or 「インターネット」 Helpful

Basics of networks (IP addresses, etc.) Basic programming (editing, compiling,

conditionals, loops, functions) Without this, your learning curve will be steep!

For those with experience, first half of semester might be a little dull...

Page 7: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

English!

Lectures will be in English First step in GIGA Program

アイドントスピークイングリシュ! 大丈夫。俺も日本語を話せないから、一緒に頑張ろう!

This class is about doing, not listening Lectures are short, most is hands-on

TA & SAs all speak 日本語 Slides from prior years (and this year!)

available on my web page

Page 8: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

この授業の流れ C言語によるプログラミング入門1. システムプログラミング2. ミニプロジェクトのグループ決定3. ネットワーク関連のプログラミング4. ミニプロジェクト成果発表

Page 9: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Class Staff Faculty

Rodney Van Meter ([email protected]) Yoshifuji 吉藤

[email protected] Assistants

TA: Takayoshi Mibe [email protected]

SA: Yudai Yamagishi [email protected]

Page 10: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Class Plan

Tuesdays, 3rd (13:00-14:30), ο11 First 45 minutes or so: lecture Last 45 min.: hands-one work Last 3 weeks, project work only

Hands-on work every week End-of-term presentations: 1/20

Page 11: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

I Promise:

If you work hard, you will learn more in this class than most others here Many SFC classes are about breadth, this

one is about depth To pass, you will have to work hard!

Almost every week, you will have to work outside of class

TA, SAs and I are available to help English will be a tool, not a barrier

Page 12: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Term Project and Grading

Homeworks first half of semester Team project 2nd half of semester Your grade depends primarily on

project Team lives & dies together That’s the way it is in life!

Page 13: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Attendance Server

Every week, we will record attendance via the network

Do “by hand” first few weeks, then via program

Protocol simple to start with, more complicated later

Page 14: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Students’ Backgrounds What year? C programming experience? Java programming experience? Classes?

インターネットオペレーション インターネット ネットワークアーキテクチャ システムプログラミング

Page 15: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

C Background C programming experience?

Understand edit-compile-debug cycle? What environment (Unix, Windows,

IDE, make)? Functions? Pointers (difficult concept, incredibly

important and useful)? Memory management (hard!)?

Page 16: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

今期の授業スケジュール(予定) 第1回 9/30: Hello World! 第2回 10/7: Loops and control flow 第3回 10/14: Data structure and pointers 第4回 10/21: Pointers and Memory Management 第5回 10/28 : File I/O 第6回 11/4: IP Networking Basics 第7回 11/11: Basic Socket Programming 1 第8回 11/25: Basic Socket Programming 2 第9回 12/2: Naming: DNS Requests 第10回 12/9: RPC: Remote Procedure Calls 第11回 12/16: Project Programming 1 第1 2 回 1/6: Project Programming 2 第13回 1/13: Project Programming 3 第1 4 回 1/20: Project Demonstration

Page 17: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Programming Environment

CNS Linux:ccx00,ccx01

Login via SSH http://itc.sfc.keio.ac.jp/ssh/ PuTTY:

http://www.chiart.greenend.org.up/~sgtathm/putty/

http://hp.vector.co.jp/authors/VA024651/PuTTYkj.html

Submit homework via SFS

Page 18: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Programming Environment (2)

OK to use your own PC Windows: cygwin

注意: IPv6は動かないかも IPv6 important in this class,

cygwin support still weak? Linux, BSD, Mac: native environment

Page 19: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

ここから本編~ネットワークの基礎

Page 20: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Basics of Network Architecture

Protocols Layered protocol model What’s a network application?

Page 21: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Protocol

In order for two computers to talk, they have to share a language and follow a set of rules: a protocol is what they promise each otherWithout a protocol, they can’t talk to each

other!

Page 22: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Humans Use Protocols

One speaking English, the other Japanese (probably) doesn’t work

On phone, start with 「もしもし, xx です」 or “Hello”

Same for computersUNIX-Windows間の通信無線 -有線間の通信

1) 受話器を上げる

2) 電話番号を入力

3) 着信を通知する4) 受話器を上げる

5) 会話する6) どちらかが受話器を置く

7) 回線が切れる

電話の例

Page 23: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

OSI Layered Model

Physical Layer

Data Link Layer

Network Layer

Transport Layer

Session Layer

Presentation Layer

Application Layer

物理層

データリンク層

ネットワーク層

トランスポート層

セッション層

プレゼンテーション層

アプリケーション層

Signal

Frame

Packet

Data

・各プロトコルの関係を階層構造でモデル化

Page 24: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

1) Physical Layer Defines physical characteristics

Signal strength/voltage, how to make numbers, etc. 電波強度、符号変調方式

Media Optical fiber, copper wire, electrical lines,

satellite, radio... Usually standardized by some group( ITU-

T, ISO …, ) 規格

X.25, IEEE, ATM, ISDN… 光) SONET, FDH…

Page 25: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Data Link Layer 物理的な通信媒体に対応した、フレームをやり取りするためのプロトコルを規定する層

同期,送信権の制御 CSMA/CD

識別 MAC Address

誤り検出 CRC Checksum

Page 26: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

3) Network Layer

Independent of physical layer, how two hosts on Internet transfer data End-to-End

Path control (routing) IPv4, IPv6 Generally requires globally unique

address

Page 27: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

4) Transport Layer

Layer that processes in a host usually use

TCP, UDP Most protocols divide service by PORT

Page 28: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

5) Session Layer

One “session” – start to finish of communication between two apps Cooperatively managed between sender

and receiver Generally handled by applications

Most Internet apps one “session” is one transport-level connection

Page 29: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

6) Presentation Layer

セッションでやり取りされるデータの表現方法や符号化、暗号化などを規定する層

Byte Order (“Endianness”) Least/Most Significant Bit (LSB/MSB) Htonl(), Htons()

Usually handled by applications

Page 30: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

7) Application Layer

Agreed upon by two applications e.g., what Email contents look like

(headers, etc.)

Most applications handle layers 5 and up themselves

Page 31: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

ネットワーク層Network Layer

データリンク層Data Link Layer

物理層Physical Layer

Internet Layered Model

エンドノード 中継ノード エンドノード

送信元から中継ノード (ルータ )を通ってあて先に到達する

アプリケーション層

Application Layer

トランスポート層Transport Layer

ネットワーク層Network Layer

データリンク層Data Link Layer

物理層Physical Layer

アプリケーション層

Application Layer

トランスポート層Transport Layer

ネットワーク層Network Layer

データリンク層Data Link Layer

物理層Physical Layer

Page 32: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

アプリケーション層Application Layer

プレゼンテーション層Presentation Layer

セッション層Session Layer

トランスポート層Transport Layer

ネットワーク層Network Layer

データリンク層Data Link Layer

物理層Physical Layer

OSI Model & Internet Architecture

32

WebXML Webサービス

EmailNNTP・・・

WebXML Webサービス

EmailNNTP・・・

MessagingFile transferFile sharing

MessagingFile transferFile sharing

TCPTCP UDPUDP

IPIP

EthernetEthernet xDSLOptical fiber

xDSLOptical fiber

無線 LAN (WLAN)無線 LAN (WLAN)

Page 33: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

アプリケーション

TCP

IP

Ethernet/100M

CAT5e

アプリケーション

TCP

IP

Ethernet/100M

CAT5e

データデータ

例: HTTPヘッダ

プロトコルスタックとカプセル化

TCPヘッダ

IPヘッダ

信号へ

送信側 受信側

送信側 各層がそれぞれ必要な情報(ヘッダ)を付加して下層へ渡す

受信側各層はヘッダの情報をもとに処理を行い,そのヘッダ部分を取り除いて上層へ渡す

Ethernet Frame

データデータ

Page 34: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

ネットワークアプリケーションとは?

ARP

hardwareinterface

RARP

media

IPICMP IGMP

TCP UDP

networklayer

data linklayer

transportlayer

process process process process

Page 35: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

ネットワークアプリケーションとは?(IPv6)

NDP(ICMPv6)

hardwareinterface

NDP(ICMPv6)

media

IPv6ICMPv6 MLD

TCP UDP

networklayer

data linklayer

transportlayer

process process process process

Page 36: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

ここから実習~C言語に触れてみる

Page 37: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Practice

Getting started login to CNS via SSH Create “Hello world” program

with Makefile

“ ”こんなのできるぜ というたしなみのある人は帰っていいよ

Once this is done, you can go home!

Page 38: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

今日の環境 CNS環境

Linux:ccx00,ccx01

sshして上記のホストにログイン http://itc.sfc.keio.ac.jp/ssh/ PuTTY:

自分のホームディレクトリにファイルを置く

Page 39: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

はろーわーるど ???.c というファイル名 gccコマンド include printf a.outなどのおまじないを体験してみる

Page 40: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

hello.cの例#include <stdio.h>int main (int argc, char* argv[]) { printf("%s\n", "Hello World!");}

#include <stdio.h>main(){ printf(“Hello World!\n”);}

→ gcc hoge.c→ ./a.out

Page 41: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Compiling

gcc hoge.c a.outというファイルが生成される

または gcc –o hoge hoge.c

hogeというファイルが生成される

Page 42: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Makefile

再コンパイルの煩雑さを解消 コンパイル時は make と打つだけ

基本文法ターゲット : 依存するファイル<tab>コマンド

Page 43: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Makefileの例all: program

program : source1.o source2.ogcc -o program source1.o source2.o

source1.o : source1.cgcc -c source1.c

source2.o : source2.cgcc -c source2.c

Page 44: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Makefileの簡素化 .cから .oファイルの生成ルールall : source1.o source2.o

gcc -o program source1.o source2.o

コンパイルオプションの変数化<command>%gcc -Wall -g -c source1.c<Makefile>CFLAGS = -Wallgcc ${CFLAGS} -c source1.c

Page 45: Network Programming in C #1: Introduction ネットワークプログラミング C 言語 第1回「イントロダクション」

Makefile: hello.cへの適用例

all: hello

hello: hello.ogcc -o hello hello.o

hello.o: hello.cgcc -c hello.c