Communication Karens Home Page - Courses -kites Sockets (Haviland - - PDF document

communication
SMART_READER_LITE
LIVE PREVIEW

Communication Karens Home Page - Courses -kites Sockets (Haviland - - PDF document

Simple Web Request Communication Karens Home Page - Courses -kites Sockets (Haviland Ch. 10) Domain Name Servers This is getting complicated! 128.100.10.235 browser 128.100.10.235 browser server www.eecg. eecg utoronto.ca?


slide-1
SLIDE 1

1

Communication

Sockets (Haviland – Ch. 10)

Simple Web Request

Karen’s Home Page

  • Courses
  • kites

Domain Name Servers

browser local name server root name server “ca” name server “utoronto” name server “eecg” name server

www.eecg. utoronto.ca? ca server utoronto server eecg server 128.100.10.235 128.100.10.235 www.eecg. utoronto.ca? www.eecg. utoronto.ca? www.eecg. utoronto.ca? www.eecg. utoronto.ca?

This is getting complicated!

browser

local name server root name server “ca” name server “utoronto” name server “eecg” name server

server

slide-2
SLIDE 2

2

Routing

browser server

HSE-Toronto- ppp.sympatico.ca dis17-toronto64- fe10.bellnexxia.net sf2-bbup.gw. utoronto.ca utorgw-border-if.

  • net.on.ca
  • net-gw.dis1-toronto.

bellnexxia.net dis1-toronto-64- pos2.bellnexxia.net

Putting it together

browser server

local name server root name server “ca” name server “utoronto” name server “eecg” name server HSE-Toronto- ppp.sympatico.ca dis17-toronto64- fe10.bellnexxia.net sf2-bbup.gw. utoronto.ca utorgw-border-if.

  • net.on.ca
  • net-gw.dis1-toronto.

bellnexxia.net dis1-toronto-64- pos2.bellnexxia.net

How do we find the server?

Every computer on the Internet has an

Internet address.

Called an IP address (Internet Protocol) An IP address is 4 8-bit numbers separated

by dots. www.eecg.toronto.edu = 128.100.10.235

IP addressing

IP address is 32 bits. In theory, it’s enough to identify 2^32 = 4

billion locations

In practice, it’s much less – routing needs IP address is like a general road map. Each part of the address provides more

specific info.

Much like a regular address:

country,city,street, street #, apt #

slide-3
SLIDE 3

3

IP address format

In general it is of the form network.node Routing decisions make on network part Network part is bigger (more networks,

smaller # of nodes/network), or smaller (less networks, big # of nodes/network) depending

  • n the network class

5 classes: A, B, C, D and E

Address classes

Class A: 1 bit of 0, 7 bits network #, 24 bits node

number

126 possible class A addresses (0 reserved,

127 is loopback), with 2^24 = 16,777,214 nodes

given to very large organizations. Eg. ISPs

Address Classes Cont’d

Class B: 2 bits of 10, 14 bits of network #, 16

bits of node #

2^14 possible class B addresses, with 2^16 =

65,534 nodes.

Nearly depleted. Given to large institutions

like universities

Address Classes Cont’d

Class C: 3 bits of 110, 21 bits of network #, 8

bits of node #

2^21 = 2,097,150 class C addresses, with

254 nodes each

Class D: 224.0.0.0 to 239.255.255.255 –

reserved

Class E: 240.0.0.0 to 255.255.255.255 –

reserved

slide-4
SLIDE 4

4

Routing

Each network keeps track of 1st hop on the

route to all other networks

Knows which gateway to use for each other

network

“Store and forward” approach

Challenges to network communication

Dynamic connections

Network connections appear and disappear Messages may take different paths, so arrival

sequence not guaranteed.

Data loss

May drop/corrupt bits through unreliable

connection

Constricted paths

Different machines/technologies have differing

limits on packet size. A large packet may have problems

Protocols

Knowing how to find someone to deliver a

message is not enough

Need a set of rules to effectively

communicate

A protocol is an agreed upon way of doing

things

Many examples of protocols in real life:

Morse code,

TCP, UDP, IP

Protocols

Invoice: Customer: Karen Reid Order No: 5379 Qty: Unit Price Total 1 Athalon 219.00 219.00 2 128 MB 149.95 299.90 Subtotal 518.90 Tax 77.84 TOTAL 596.74 Karen Reid Feb 18, 2001 Payable to: CPUS are us $596.74 Five hundred ninety six 74/100 ____________

CPUS are us Karen Reid

  • Dept. of Computer Science

University of Toronto Karen Reid CPUS are us 0 College Street Toronto Ontario M5S 3G4

We deliver! Courier

slide-5
SLIDE 5

5

IP as a protocol

Data to be sent is divided into packets A packet has at least 3 basic elements: source,

destination, and data

Can always identify where a packet came from,

where it’s going, and how big it is

Does not guarantee arrival of packet at destination.

Nor does it guarantee the ordering of arrival.

But if a packet arrives, then its integrity is

guaranteed

Connection oriented vs. connectionless

Connection oriented (a.k.a) virtual circuit is

like a telephone connection.

Send unformatted, uninterrupted stream of

characters to the same location

TCP

Connectionless model is like mail – you aren’t

sure if it got there

Good for broadcast messages UDP

UDP – a connectionless protocol

UDP = user datagram protocol A.K.A. Unreliable datagram protocol ☺ Built on top of IP Low overhead No guarantee of delivery Good for broadcast messages

TCP – transmission control protocol

Built on top of IP (Hence TCP/IP) Connection oriented Guarantees delivery Adds sequencing and acknowledgement info

to each packet

Cooperates to ensure data stream received is

reconstructed in the same sequence as the

  • riginal
slide-6
SLIDE 6

6

3-way handshake SYN

Here’s some data Got it

TCP Connection

Hi 128.100.10.25 Connection port 1? Hi 128.100.10.128 Let’s talk

  • kay

Send me a file Here’s some more Got it I’m done I’m done too ack ack fin fin

Sockets

One form of communication between

processes.

Similar to pipes, except sockets can be used

between processes on different machines.

Use file descriptors to refer to sockets. Built on top of TCP layer

Socket Types

Two main categories of sockets

UNIX domain: both processes on the same

machine

INET domain: processes on different machines

Three main types of sockets:

SOCK_STREAM: the one we will use SOCK_DGRAM: for connectionless sockets SOCK_RAW

Addresses and Ports

A socket pair is the two endpoints of the

connection.

An endpoint is identified by an IP address

and a port.

IPv4 addresses are 4 8-bit numbers:

128.100.31.156 = penguin 128.100.31.4 = eddie

Ports

because multiple processes can communicate

with a single machine we need another identifier.

try nslookup

slide-7
SLIDE 7

7

More on Ports

Well-known ports: 0-1023

80 = web 22 = ssh 23 = telnet

Registered ports: 1024-49151

2709 = supermon 26000 = quake

Dynamic (private) ports: 49152-65535

You should pick ports in this range to avoid

  • verlap

21 = ftp 25 = smtp (mail) 194 = irc

www.iana.org

Connection-Oriented

Server

Create a socket: socket() Assign a name to a socket: bind() Establish a queue for connections: listen() Get a connection from the queue: accept()

Client

Create a socket: socket() Initiate a connection: connect()

Server side

int socket(int family, int type, int protocol);

family specifies protocol family:

AF_INET – Internet domain for networking AF_UNIX – Unix domain (process on the same machine)

type

SOCK_STREAM, SOCK_DGRAM

protocol

set to 0 except for RAW sockets

returns a socket descriptor

Code thus far (server and client)

#include <ctypes.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main(void) { int sockfd; if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { //handle error, quit } //server: bind, listen for incoming connections, accept //connections //client: connect to the server, start send & receive

slide-8
SLIDE 8

8

bind to a name

int bind(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

  • Associates the network address of a machine with a socket
  • sockfd – returned by socket
  • struct sockaddr_in{

sa_family sin_family; /*AF_INET */ in_port sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; }

  • sin_addr can be set to INADDR_ANY to communicate with any

host

  • Set sin_addr like so:

.sin_addr.s_addr=inet_addr(“197.124.10.1”);

Set up queue in kernel

int listen(int sockfd, int backlog)

after calling listen, a socket is ready to

accept connections

prepares a queue in the kernel where

partially completed connections wait to be accepted.

backlog is the maximum number of partially

completed connections that the kernel should queue.

Code (server side):

//... Call socket() #define SIZE sizeof(struct sockaddr_in) struct sockaddr_in server; if( bind(sockfd,(struct sockaddr*)&server,SIZE)==-1) { //handle error, quit } //start listening for connections if( listen(sockfd,5) == -1) { //handle error, quit } // todo: accept a connection, spawn child to deal // with the connection

Complete the connection

int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);

blocks waiting for a connection (from the queue) returns a new descriptor which refers to the TCP

connection with the client

sockfd is the listening socket cliaddr is the address of the client reads and writes on the connection will use the

socket returned by accept

slide-9
SLIDE 9

9

Client side

socket() – same as server, to say “how” we are

going to talk int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

the kernel will choose a dynamic port and source IP

address.

returns 0 on success and -1 on failure setting errno. initiates the three-way handshake.

Client code

//setup socket with socket() //connect to the server struct sockaddr_in server= {AF_INET,7000}; server.sin_addr.s_addr = inet_addr(“206.45.10.2”); if(connect(sockfd,(struct sockaddr *)&server,SIZE)==-1) { //handle error, quit } //send and receive data

TCP: Three-way handshake

client server socket connect (blocks) connect returns socket,bind,listen accept(blocks) accept returns

SYN J SYN K, ack J+1 ack K+1

sequence number = J sequence number = K

read() write() close() socket() connect() write() read() close()

block until connection from client Connection establishment (3-way handshake) data transfer end-of-file notification

socket() bind() listen() accept() TCP Server TCP Client

slide-10
SLIDE 10

10

Sending and receiving data

ssize_t recv(int sockfd, void *buffer,size_t length, int flags); ssize_t send(int sockfd, void *buffer,size_t length, int flags);

Full duplex communication Normally, set flags to 0 (this makes recv,send identical to

read, write)

recv returns 0 (end of file) when the other end is dead

(through a close)

Remember to close() the connection when done

Server side

//socket initialized as shown earlier while(1) { //accept connection if((newsockfd=accept(sockfd,NULL,NULL)) == -1) { //handle error } //spawn a child to deal with the connection if(fork()==0){ while(recv(newsockfd,&c,1,0) > 0) { c=toupper(c); send(newsockfd,&c,1,0); } } }

Byte order

Big-endian Little-endian Intel is little-endian, and Sparc is big-endian 00 01 64 C1 A A+1 A+2 A+3 91,329 = 00 01 64 C1 A+3 A+2 A+1 A 91,329 =

Network byte order

To communicate between machines with

unknown or different “endian-ness” we convert numbers to network byte order (big- endian) before we send them.

There are functions provided to do this:

unsigned long htonl(unsigned long) unsigned short htons(unsigned short) unsigned long ntohl(unsigned long) unsigned short ntohs(unsigned short)