windows network programming

26
IT COOKBOOK - 1 - Windows Network Programming

Upload: lucien

Post on 05-Jan-2016

62 views

Category:

Documents


4 download

DESCRIPTION

Windows Network Programming. Chapter 01. Intro. to Network and Socket Programming. Goal. TCP/IP Protocol Basic concept of socket Windows socket overview Writing and executing of Windows socket application. Router. ①. End System (PC, PDA, ...). ②. ③. Router. ④. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 1 -

Windows Network Programming

Page 2: Windows Network Programming

Chapter 01.

Intro. to Network and

Socket Programming

Page 3: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 3 -

Goal

• TCP/IP Protocol• Basic concept of socket• Windows socket overview• Writing and executing of Windows

socket application

Page 4: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 4 -

Internet Configuration

End System(PC, PDA, ...)

Router

End System(PC, PDA, ...)

Router

Page 5: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 5 -

TCP/IP Protocol

TCP/IP Protocol(OS)

Application

Router Router

TCP/IP Protocol(OS)

Application

End System End System

Page 6: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 6 -

TCP/IP Protocol Structure (1/6)

Network Access Layer

Internet Layer

Transport Layer

Application Layer

Device DriverNetwork Hardware

IP

TCP, UDP

TELNET, FTP, HTTP,SMTP, MIME, SNMP, ...

• TCP/IP Protocol Structure– Layering Structure

Page 7: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 7 -

TCP/IP Protocol Structure (2/6)

• Network access layer– Role

• Data transmission through physical network

– Components• Network hardware + device driver

– Addressing• physical address(MAC address)• Command: ‘ipconfig /all’

– Example• Ethernet

Page 8: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 8 -

TCP/IP Protocol Structure(3/6)

• Internet layer– Role

• Packet routing

– Addressing• IP address

• Transport layer– Role

• Data transfer to end system

– Addressing• port number

– Examples• TCP(Transmission Control Protocol), UDP(User

Datagram Protocol)

Page 9: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 9 -

TCP/IP Protocol Structure(5/6)

• TCP and UDP

TCP UDP

Connection-oriented Protocol- Data transfer after connection setup

Connectionless Protocol- Data transfer winthout connection

Byte-stream service Datagram service

Reliable data transfer- Retransmission mechanism

Unreliable data transfer- No retransmission mechanism

1 to 1 communication(unicast)

1 to 1 communication(unicast)1 to N communication (broadcast),N to N Communication (multicast)

Page 10: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 10 -

TCP/IP Protocol Structure(6/6)

• Application layer– Role

• Including some application protocol and services• Providing various application service

– Examples• Telnet, FTP, HTTP, SMTP etc.

Page 11: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 11 -

Packet transfer Principle (1/5)

• Packet– Control information defined by each protocol

(IP address, port number, error check code etc.) + data

– Depending on the location, header and trailer

Page 12: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 12 -

Packet transfer Principle(2/5)

• Packet transfer at sender side

Ethernet

IP

TCP

Application data

dataTCP Header

dataTCP HeaderIP Header

TCP Header

IP Header

Ethernet Header data Ethernet trailer

Page 13: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 13 -

Packet transfer Principle(3/5)

• Packet transfer at receiver side

Ethernet

IP

TCP

Application data

dataTCP Header

dataTCP HeaderIP Header

TCP Header

IP Header

Ethernet Header

data Ethernet trailer

Page 14: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 14 -

Packet transfer Principle(5/5)

• Packet transfer using Internet protocol

Network Access Layer

Internet Layer

Transport Layer

Application Layer

Network Access Layer

Internet Layer

Transport Layer

Application Layer

Network Access Layer

Internet Layer

Network Access Layer

Internet Layer

End system Router Router End system

Page 15: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 15 -

IP address and port number (1/3)

• IP 주소– Unique identifier of a internet host– IPv4: 32bit, IPv6: 128bit

• example) 147.46.114.70• example) loopback address: 127.0.0.1

• Port number– Unique process identifier

Page 16: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 16 -

IP address and port number (2/3)

• IP address and port number

TCP

IP

process process

process

UDP

Port number(0~65535)

IP address

Page 17: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 17 -

Client/server model

• Client/server Model• Server first executed• When client send the request, server processes

the request• Example: web client/server

Server Client

waiting

connecting

Page 18: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 18 -

socket concept (1/5)

• Three points of view① Data type② communication end-point③ Network programming interface

Page 19: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 19 -

socket concept(2/5)

• Data type– Each of client and server is identified by a

separate socket address– Example of socket address data type

• Struct sockaddr_in, in_addr

// file creation

int fd = open("myfile", ...);

...

read(fd, ...) // Reading

write(fd, ...) // Writing

// socket creation

SOCKET sock = socket(...);

...

recv(sock, ...) // Receive

send(sock, ...) // Send

Page 20: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 20 -

socket concept(3/5)

• Communication end-point– Socket requires three components

• protocol(TCP/IP, UDP/IP)• sender IP address, sender port number• receiver IP address, receiver port number

Page 21: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 21 -

socket concept(4/5)

• Communication end-point(cont’d)

client

send (sock, ...)

server

recv (sock, ...)

data

<client socket>• protocol: TCP/IP• IP address: 147.46.114.70• port number: 12023

<server socket>

• protocol : TCP/IP• IP address: 61.72.244.22• port number: 9001

Page 22: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 22 -

socket concept(5/5)

• Network programming interface– Socket is the interface between application

layer and transport layer in TCP/IP protocol stack

application

TCP

IP

UDP

application application

Socket interface

ICMP, IGMP

Page 23: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 23 -

Windows sockets (1/3)

• Windows Sockets, Winsock- Originated by Berkeley Software

Distribution UNIX- Included as API(Application Programming

Interface) since Windows 95 version

Page 24: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 24 -

Windows sockets(2/3)

• Winsock supported by windows version

• Supported protocols– TCP/IP, IPv6(windows XP or above),

IrDA(windows 98 or above), Bluetooth(windows XP SP2 or above), IPX/SPX, ATM, DECNet, TP4(Not supported since windows 2000), DLC(TP4(Not supported since windows XP), NetBEUI(Not supported since windows XP)

OSWinsock ver.

Windows 95 1.1 (2.2)

Windows 98/Me, Windows NT/2000/XP/2003 Server, Windows Vista/2008 Server/7

2.2

Windows CE 1.1 (2.2)

Page 25: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 25 -

Windows sockets(3/3)

• Structure

Winsock 2.xApplication

Winsock 1.xApplication

WINSOCK.DLL (16 bit)WSOCK32.DLL (32 bit)

WS2_32.DLL (32 bit)

Winsock 1.x API

Winsock 2.x API

TCP/IP IrDA IPX/SPX

MSWSOCK.DLL

...Bluetooth

Winsock 1.x Extended API

Page 26: Windows Network Programming

IT COOKBOOKIT COOKBOOK

- 26 -

Winsock application practice

• Source code: – Class webpage

• Sample Winsock application(server.cpp)