socket programming

33

Upload: divdix1

Post on 28-May-2017

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Socket Programming
Page 2: Socket Programming

What Each Layer Does

2Cisco Academy Program Semester 1

2

Page 3: Socket Programming

Application Layer• Gives end-user

applications access to network resources

• Where is it on my computer?– Workstation or Server

Service in MS Windows

3Graphic courtesy of http://www.hawkclan.com/zxonly/iso/slide2.html

3

Page 4: Socket Programming

Presentation Layer• Provides common

data formatting between communicating devices

• Components make sure the receiving station can read the transferred data

3

3Graphic courtesy of http://www.hawkclan.com/zxonly/iso/slide2.html

Page 5: Socket Programming

Session Layer• Allows applications

to maintain an ongoing session

• Example – NetBIOS• Where is it on my

computer?– Workstation and

Server Service (MS)– Windows Client for

NetWare (NetWare)

3

3Graphic courtesy of http://www.hawkclan.com/zxonly/iso/slide2.html

Page 6: Socket Programming

Transport Layer• Provides reliable data

delivery• It’s the TCP in TCP/IP• Receives info from

upper layers and segments it into packets

• Can provide error detection and correction

3

3Graphic courtesy of http://www.hawkclan.com/zxonly/iso/slide2.html

Page 7: Socket Programming

Network Layer• Provides network-

wide addressing and a mechanism to move packets between networks (routing)

• Responsibilities:– Network addressing– Routing

• Examples:– IP from TCP/IP– IPX from IPX/SPX

3

3Graphic courtesy of http://www.hawkclan.com/zxonly/iso/slide2.html

Page 8: Socket Programming

Network Addresses• Network-wide addresses• Used to transfer data across subnets• Used by routers for packet forwarding• Example:

– IP Address

• Where is it on my computer?– TCP/IP Software

Page 9: Socket Programming

Data Link Layer• Places data and

retrieves it from the physical layer and provides error detection capabilities

3

3Graphic courtesy of http://www.hawkclan.com/zxonly/iso/slide2.html

Page 10: Socket Programming

Sub-layers of the Data Link Layer• MAC (Media Access Control)

– Gives data to the NIC– Controls access to the media through:

• CSMA/CD Carrier Sense Multiple Access/Collision Detection

• Token passing• LLC (Logical Link Layer)

– Manages the data link interface (or Service Access Points (SAPs))

– Can detect some transmission errors using a Cyclic Redundancy Check (CRC). If the packet is bad the LLC will request the sender to resend that particular packet.

Page 11: Socket Programming

Physical Layer• Determines the specs

for all physical components– Cabling– Interconnect methods

(topology / devices)– Data encoding (bits to waves)– Electrical properties

• Examples:– Ethernet (IEEE 802.3)– Token Ring (IEEE 802.5)– Wireless (IEEE 802.11b)

3

3Graphic courtesy of http://www.hawkclan.com/zxonly/iso/slide2.html

Page 12: Socket Programming

Physical Layer (cont’d)• What are the Physical Layer components on

my computer?• NIC

– Network Interface Card– Has a unique 12 character Hexadecimal number

permanently burned into it at the manufacturer. – The number is the MAC Address/Physical address

of a computer• Cabling

– Twister Pair– Fiber Optic– Coax Cable

Page 13: Socket Programming

How Does It All Work Together• Each layer contains a Protocol

Data Unit (PDU)– PDU’s are used for peer-to-peer

contact between corresponding layers.

– Data is handled by the top three layers, then Segmented by the Transport layer.

– The Network layer places it into packets and the Data Link frames the packets for transmission.

– Physical layer coverts it to bits and sends it out over the media.

– The receiving computer reverses the process using the information contained in the PDU. 2

2Cisco Academy Program Semester 1

Page 14: Socket Programming

Evolution of the 7-Layers• Single Layer Model - First Communication

Between Computer Devices– Dedicated copper wire or radio link– Hardware & software inextricably intertwined– Single specification for all aspects of communication

Hardware&

Software

DEVICE A

Hardware&

Software

DEVICE B

1 http://www.howtheosimodelworks.com/

1

Page 15: Socket Programming

Evolution of the 7-Layers (2)• Two Layer Model

– Problem: Applications were being developed to run over ever-increasing number of media/signaling systems.

– Solution: Separate application aspects from technical (signaling and routing) aspects

– Application Layer: Concerned with user interface, file access and file transfer

Application

Technical Standards

Application

Technical Standards

11 http://www.howtheosimodelworks.com/

Page 16: Socket Programming

Evolution of the 7-Layers (3)• Four Layer Model - Network connectivity inherently

requires travel over intermediate devices (nodes)• Technical Standards Level divided into Network, Data-

link and Physical Layers

Network

Physical

Data-Link

Application

Network

Physical

Data-Link

Application

11 http://www.howtheosimodelworks.com/

Page 17: Socket Programming

Evolution of the 7-Layers (3) cont.• Physical Layer

–Describes physical aspects of network: cards, wires, etc–Specifies interconnect topologies and devices

• Network Layer–Defines a standard method for operating between nodes–Address scheme is defined (IP)–Accounts for varying topologies

• Data-Link–Works with Network Layer to translate logical addresses (IP) into hardware

addresses (MAC) for transmission–Defines a single link protocol for transfer between two nodes

Page 18: Socket Programming

Evolution of the 7-Layers (4)• Five Layer Model – Increase Quality of Service (QOS)

–Variable levels of data integrity in network–Additional data exchanges to ensure connectivity over worst conditions–Became the Transport Layer

Transport

Application

Network

Physical

Data-Link

Transport

Application

Network

Physical

Data-Link

1

1 http://www.howtheosimodelworks.com/

Page 19: Socket Programming

Evolution of the 7-Layers (5)• Six Layer Model -

Dialogue Control and Dialogue Separation

– Means of synchronizing transfer of data packets

– Allows for checkpointing to see if data arrives (at nodes and end stations)

– Became Session Layer

Transport

Network

Physical

Data-Link

Session

Application

Transport

Network

Physical

Data-Link

Session

Application

1

1 http://www.howtheosimodelworks.com/

Page 20: Socket Programming

Evolution of the 7-Layers (6)• The Seven Layer OSI

Model - Addition of Management and Security

– Standardizing notation or syntax for application messages (abstract syntax)

– Set of encoding rules (transfer syntax)

– Became the Presentation Layer

Presentation

Transport

Network

Physical

Data-Link

Session

Application

Presentation

Transport

Network

Physical

Data-Link

Session

Application

1

1 http://www.howtheosimodelworks.com/

Page 21: Socket Programming
Page 22: Socket Programming

Layers of software

IP

UDP and TCP

Sockets and APIs; marshalling

RMI

Applications and services

Page 23: Socket Programming

The Internet protocol• Every computer on the Internet

has a unique identifier, its Internet address (IP address)

• The Internet protocol (IP) routes packets from one computer to another– A router is a special-purpose

computer which acts as an intermediary between a pair of communicating computers

• An IP packet includes:– The identity of the sender

machine – i.e. it’s IP address– The identity of the machine to

which the packet should be delivered

– The packet contents – application data

• The maximum size permitted for an IP packet is 64Kb– In practice, this is too much for

many networks to deliver in one chunk and the IP packet must be broken down into fragments

– The IP protocol takes care of disassembling a packet into fragments and subsequently reassembling the IP packet

The Internet is a packet-switching network. Packets sent between two computers do not necessarily follow the same path.

Page 24: Socket Programming

IP as a basis for a communication channel

• Performance– Depends on the underlying networks used

• Reliability– No validity guarantees

• Where an incoming message buffer is full (at the destination computer or any intermediate router), the packet will be dropped

– No integrity guarantees• Packets may be corrupted as they travel through the network; any

packet may arrive more than once at the destination• Ordering

– No ordering guarantees – a sequence of packets may take different routes, incurring different transmission times

• Synchronicity– Over a public network (e.g. the Internet) , asynchronous– For a closed network, synchronous is possible

Page 25: Socket Programming

TCP communication

Virtual connection

TCP socket

IPTCP

Application

IPTCP

Application

The TCP layer adds significant overhead to provide reliable communication

Page 26: Socket Programming

Issues for TCP• Outstanding issues for TCP communication include:

– Matching of data items• Two processes connected by a stream must agree on the

contents and ordering of data transmitted over the stream– E.g. where one process writes an int followed by a double, the other

process must read an int and then a double• Where the two processes do not cooperate correctly, the

receiving process is likely to experience errors

– Blocking• The receiver process blocks until sufficient data is present in the

stream• The sender process can be blocked by TCP’s flow control

mechanism

Page 27: Socket Programming

Ports• A port serves as a

message source or destination– With the Internet protocols,

messages are sent to (Internet address, port) pairs

• A local port can be bound to no more than one process

• Processes may use multiple ports

Web server

Mail server

Web browser

Web browser

138.37.94.248

Port

Agreed (destination)

port

Internet address

Page 28: Socket Programming

Sockets• A socket is a programming

abstraction which provides an endpoint for communication

• The receiver process’ socket must be bound to a local port and the Internet address of the computer on which the receiver runs

• Messages sent to a particular Internet address and port number can be received only by a process whose socket is bound to that Internet address and port number

• A socket is associated with a transport protocol – either TCP or UDP

browser

138.37.94.248

browser

Port 80

Socket bound to (138.37.94.248, 80)

Web server

Page 29: Socket Programming

29

Winsock Server/Client Winsock Server/Client Communication Procedure Communication Procedure

• Datagram service - UDP, connectionless

Page 30: Socket Programming

30Communication Networks Research (CNR) Lab.EECS, KAIST

• Stream service - TCP, connection-oriented.Server

Client

Page 31: Socket Programming

Winsock API and Class• Winsock API

– Datagram service (UDP) API

Function name Operation host

socket Create a socket Client/Server

bind Assign a name to the created socket. Server

connect Connect to a server Client

sendto Transmit datagram Client/Server

recvfrom Receive datagram from winsock and save the address of client Client/Server

closesockt Close the socket Client/Server

Page 32: Socket Programming

Stream Service (TCP) APIFunction

name Operation host

socket Create a socket Client/Server

bind Assign a name to the created socket Server

listen Wait for a client's connection Server

connect Connect to a server Client

accept Accept the client's connection Server

send Transmit data by winsock Client/Server

recv Receive datagram from winsock Client/Server

closesockt Close the socket Client/Server

Page 33: Socket Programming

• Other Winsock API Function name Operations

setsockopt Set up the winsock option

WSAStartup Initialize the winsock

WSAAsyncSelect Set up the winsock in asynchronous mode

WSACancelBlockingCall Cancel calling the blocking winsock function