Top Banner
Lecture7 1 Chapter 2 outline 2.1 Principles of app layer protocols clients and servers app requirements 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS 2.6 Socket programming with TCP 2.7 Socket programming with UDP 2.8 Building a Web server 2.9 Content distribution Network Web caching Content distribution networks P2P file sharing
41

Chapter 2 outline

Mar 19, 2016

Download

Documents

daxia

2.1 Principles of app layer protocols clients and servers app requirements 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS. 2.6 Socket programming with TCP 2.7 Socket programming with UDP 2.8 Building a Web server 2.9 Content distribution Network Web caching - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 2 outline

Lecture7 1

Chapter 2 outline 2.1 Principles of app

layer protocols clients and servers app requirements

2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail

SMTP, POP3, IMAP 2.5 DNS

2.6 Socket programming with TCP

2.7 Socket programming with UDP

2.8 Building a Web server

2.9 Content distribution Network Web caching Content distribution

networks P2P file sharing

Page 2: Chapter 2 outline

Lecture7 2

Socket programming

Socket API introduced in BSD4.1 UNIX,

1981 explicitly created, used,

released by apps client/server paradigm two types of transport

service via socket API: unreliable datagram reliable, byte stream-

oriented

a host-local, application-created,

OS-controlled interface (a “door”) into which

application process can both send and

receive messages to/from another

application process

socket

Goal: learn how to build client/server application that communicate using sockets

Page 3: Chapter 2 outline

Lecture7 3

Socket-programming using TCPSocket: a door between application process and

end-end-transport protocol (UDP or TCP)TCP service: reliable transfer of bytes from one

process to another

process

TCP withbuffers,

variables

socket

controlled byapplicationdeveloper

controlled byoperating

system

host orserver

process

TCP withbuffers,

variables

socket

controlled byapplicationdevelopercontrolled byoperatingsystem

host orserver

internet

Page 4: Chapter 2 outline

Lecture7 4

Socket programming with TCPClient must contact server server process must first

be running server must have created

socket (door) that welcomes client’s contact

Client contacts server by: creating client-local TCP

socket specifying IP address, port

number of server process When client creates socket:

client TCP establishes connection to server TCP

When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk

with multiple clients source port numbers

used to distinguish clients (more in Chap 3)

TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server

application viewpoint

Page 5: Chapter 2 outline

Lecture7 5

Stream jargon A stream is a sequence of characters that flow

into or out of a process. An input stream is attached to some input

source for the process, eg, keyboard or socket. An output stream is attached to an output

source, eg, monitor or socket.

Page 6: Chapter 2 outline

Lecture7 6

Socket programming with TCPExample client-server app:1) client reads line from

standard input (inFromUser stream) , sends to server via socket (outToServer stream)

2) server reads line from socket3) server converts line to

uppercase, sends back to client

4) client reads, prints modified line from socket (inFromServer stream)

outT

oSer

ver

to network from network

inFr

omS

erve

r

inFr

omU

ser

keyboard monitor

Process

clientSocket

inputstream

inputstream

outputstream

TCPsocket

Clientprocess

client TCP socket

Page 7: Chapter 2 outline

Lecture7 7

Client/server socket interaction: TCP

wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()

create socket,port=x, forincoming request:welcomeSocket =

ServerSocket()

create socket,connect to hostid, port=xclientSocket =

Socket()

closeconnectionSocket

read reply fromclientSocket

closeclientSocket

Server (running on hostid) Client

send request usingclientSocketread request from

connectionSocket

write reply toconnectionSocket

TCP connection setup

Page 8: Chapter 2 outline

Lecture7 8

Example: Java client (TCP)import java.io.*; import java.net.*; class TCPClient {

public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("hostname", 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

Createinput stream

Create client socket,

connect to serverCreate

output streamattached to socket

Page 9: Chapter 2 outline

Lecture7 9

Example: Java client (TCP), cont.

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close(); } }

Createinput stream

attached to socket

Send lineto server

Read linefrom server

Page 10: Chapter 2 outline

Lecture7 10

Example: Java server (TCP)import java.io.*; import java.net.*;

class TCPServer {

public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence;

ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

Createwelcoming socket

at port 6789Wait, on welcoming

socket for contactby client

Create inputstream, attached

to socket

Page 11: Chapter 2 outline

Lecture7 11

Example: Java server (TCP), cont

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence); } } }

Read in linefrom socket

Create outputstream,

attached to socket

Write out lineto socket

End of while loop,loop back and wait foranother client connection

Page 12: Chapter 2 outline

Lecture7 12

Chapter 2 outline 2.1 Principles of app

layer protocols clients and servers app requirements

2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail

SMTP, POP3, IMAP 2.5 DNS

2.6 Socket programming with TCP

2.7 Socket programming with UDP

2.8 Building a Web server

2.9 Content distribution Network Web caching Content distribution

networks P2P file sharing

Page 13: Chapter 2 outline

Lecture7 13

Socket programming with UDP

UDP: no “connection” between client and server

no handshaking sender explicitly attaches

IP address and port of destination to each packet

server must extract IP address, port of sender from received packet

UDP: transmitted data may be received out of order, or lost

application viewpointUDP provides unreliable transfer

of groups of bytes (“datagrams”) between client and server

Page 14: Chapter 2 outline

Lecture7 14

Client/server socket interaction: UDP

closeclientSocket

Server (running on hostid)

read reply fromclientSocket

create socket,clientSocket = DatagramSocket()

Client

Create, address (hostid, port=x,send datagram request using clientSocket

create socket,port=x, forincoming request:serverSocket = DatagramSocket()

read request fromserverSocket

write reply toserverSocketspecifying clienthost address,port number

Page 15: Chapter 2 outline

Lecture7 15

Example: Java client (UDP)

send

Pac

ket

to network from network

rece

iveP

acke

t

inFr

omU

ser

keyboard monitor

Process

clientSocket

UDPpacket

inputstream

UDPpacket

UDPsocket

Output: sends packet (TCP sent “byte stream”)

Input: receives packet (TCP received “byte stream”)

Clientprocess

client UDP socket

Page 16: Chapter 2 outline

Lecture7 16

Example: Java client (UDP)import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine();

sendData = sentence.getBytes();

Createinput stream

Create client socket

Translate hostname to IP

address using DNS

Page 17: Chapter 2 outline

Lecture7 17

Example: Java client (UDP), cont.

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); }

}

Create datagram with data-to-send,

length, IP addr, port

Send datagramto server

Read datagramfrom server

Page 18: Chapter 2 outline

Lecture7 18

Example: Java server (UDP)import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

serverSocket.receive(receivePacket);

Createdatagram socket

at port 9876

Create space forreceived datagram

Receivedatagra

m

Page 19: Chapter 2 outline

Lecture7 19

Example: Java server (UDP), cont

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } }

}

Get IP addrport #, of

sender

Write out datagramto socket

End of while loop,loop back and wait foranother datagram

Create datagramto send to client

Page 20: Chapter 2 outline

Lecture7 20

Building a simple Web server handles one HTTP

request accepts the request parses header obtains requested file

from server’s file system

creates HTTP response message: header lines + file

sends response to client

after creating server, you can request file using a browser (eg IE explorer)

see text for details If you are familiar

with Java, practice this by yourself

Page 21: Chapter 2 outline

Lecture7 21

Socket programming: referencesJava-tutorials: “All About Sockets” (Sun tutorial),http://java.sun.com/docs/books/tutorial/

networking/sockets/ “Socket Programming in Java: a tutorial,”http://www.javaworld.com/javaworld/jw-12-1996/jw-

12-sockets.html

Page 22: Chapter 2 outline

Lecture7 22

Socket programming API Unix systems System calls Proj1: simple HTTP client/server

Using UNIX BSD sockets C-language tutorial: “Unix Network Programming” (J. Kurose),http://manic.cs.umass.edu/~amldemo/courseware/

intro Beej's Guide to Network Programminghttp://www.ecst.csuchico.edu/~beej/guide/net/ See useful links fore more infor on class web page

Page 23: Chapter 2 outline

Lecture7 23

Unix Programming: Mechanism UNIX system calls and library routines

(functions called from C/C++ programs)

man <function name>

A word on style: check all return codes

if ((code = syscall()) < 0) {perror("syscall");

}

Page 24: Chapter 2 outline

Lecture7 24

Creating Sockets#include <sys/types.h>#include <sys/socket.h>int socket(int domain, int type, int protocol);

- creates an endpoint for communication- return value: -1 if an error occurs;

otherwise the return value is a descriptor referencing the socket

Page 25: Chapter 2 outline

Lecture7 25

Creating Sockets: Parameters domain : address family (protocol

family) determine address structure e.g. AF_UNIX, AF_INET, AF_INET6 we will use AF_INET only

type : service of a socket e.g. SOCK_DGRAM provides unreliable,

connectionless service e.g. SOCK_STREAM provides connection-

oriented reliable byte-stream service

Page 26: Chapter 2 outline

Lecture7 26

Creating Sockets: Parameters (cont.)

protocol : specifies particular protocol Usually already defined by domain and type

(e.g. TCP for AF_INET and SOCK_STREAM; UDP for AF_INET and SOCK_DGRAM)

we will use 0 (default protocol) Example

if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) {

perror(“socket”);exit(1);

}

Page 27: Chapter 2 outline

Lecture7 27

Binding the Address for a Socket

#include <sys/types.h>#include <sys/socket.h>int bind(int sd, struct sockaddr *my_addr,

socklen_t addrlen);

- assigns the local address of a socket- return value: -1 if an error occurs;

otherwise 0

Page 28: Chapter 2 outline

Lecture7 28

Socket Address Several types of addresses We will use sockaddr_in (<netinet/in.h>)

struct sockaddr_in {sa_family_t sin_family; /*AF_INET*/uint16_t sin_port; /* network order*/

struct in_addr sin_addr;};struct in_addr {

uint32_t s_addr; /* network order*/};

Two types of byte ordering: little endian, and big endian (network order)

Page 29: Chapter 2 outline

Lecture7 29

Internet Addresses and Ports sin_port

16 bits 0-1024 reserved for system well-known ports are important If you specify 0, the OS picks a port

s_addr 32 bits INADDR_ANY for any local interface address

Page 30: Chapter 2 outline

Lecture7 30

Internet Addresses and Ports: Example

struct sockaddr_in myaddr;

bzero( (char*)myaddr, sizeof(myaddr) );/*initialize*/myaddr.sin_family = AF_INET;myaddr.sin_port = htons(80); /* bind to HTTP port*/myaddr.sin_addr.s_addr = htos(INADDR_ANY); /* any address*/

if ( (bind(sockfd, (struct sockaddr*)&myaddr, sizeof(myaddr)) < 0 ) {perror(“bind”);exit(1);

}

Page 31: Chapter 2 outline

Lecture7 31

Set a Socket in the Listening State (Server)

#include <sys/types.h>#include <sys/socket.h>int listen(int sd, int backlog);

- Specify the willingness to accept new connection- backlog : specify the number of pending

connections- return value: -1 if an error occurs; otherwise 0

Page 32: Chapter 2 outline

Lecture7 32

Initialize Connection Setup (Client)#include <sys/types.h>#include <sys/socket.h>int connect(int sd, const struct sockaddr *serv_addr,

socklen_t addrlen);

- For SOCK_STREAM, initialize connection to the server; for SOCK_DGRAM, just set the destination address and set the socket in connected state

- return value: -1 if an error occurs; otherwise 0

Page 33: Chapter 2 outline

Lecture7 33

Accept a Connection (Server)#include <sys/types.h>#include <sys/socket.h>int accept(int sd, struct sockaddr *peer_addr,

socklen_t addrlen);

- remove the first connection from the pending connection queue, create a new socket in connected state, the original sd is not changed and still in listening state

- return value: -1 if an error occurs; otherwise the descriptor of the newly connected socket

Page 34: Chapter 2 outline

Lecture7 34

Read/Write to a Socket read()/write() of the file interface for

connected-oriented Socket specific system call

send()/sendto()/sendmsg() recv()/recvfrom()/recvmsg()

Page 35: Chapter 2 outline

Lecture7 35

Read from a socket by using read()#include <unistd.h>ssize_t read(int sockfd, void *buf, size_t

count);

- read up to count from the socket- return value: -1 if an error occurs; 0 if end

of file; otherwise number of bytes read

Page 36: Chapter 2 outline

Lecture7 36

Write to a socket by using write()#include <unistd.h>ssize_t write(int sockfd, const void *buf,

size_t count);

- write up to count to the socket- return value: -1 if an error occurs;

otherwise number of bytes write

Page 37: Chapter 2 outline

Lecture7 37

Send to a Socket#include <sys/types.h>#include <sys/socket.h>int send(int sd, const void *msg, size_t len, int flags);int sendto(int sd, const void *msg, size_t len, int

flags, const struct sockaddr *to, socklen_t tolen);int sendmsg(int sd, const struct msghdr *msg, int

flags)

- return value: -1 if an error occurs; otherwise the number of bytes sent

Page 38: Chapter 2 outline

Lecture7 38

Receive from a Socket#include <sys/types.h>#include <sys/socket.h>int recv(int sd, void *buf, size_t len, int flags);int recvfrom(int sd, void *buf, size_t len, int flags,

struct sockaddr *from, socklen_t fromlen);int recvmsg(int sd, struct msghdr *msg, int flags);

- return value: -1 if an error occurs; otherwise the number of bytes received

Page 39: Chapter 2 outline

Lecture7 39

Close a Socket#include <unistd.h>int close(int sd);- return value: -1 if an error occurs; otherwise 0

#include <sys/socket.h>int shutdown(int sd, int how);- how : if 0, no further receives; if 1, no further sends; if

2, no further sends or receives- return value: -1 if an error occurs; otherwise 0

Page 40: Chapter 2 outline

Lecture7 40

Support Routines: Network/Host Order#include <netinet/in.h>

unsigned long int htonl(unsigned long int hostlong);unsigned short int htons(unsigned short int

hostshort);

unsigned long int ntohl(unsigned long int networklong);

unsigned short int ntohs(unsigned short int networkshort);

Page 41: Chapter 2 outline

Lecture7 41

DNS Service#include <netdb.h>extern int h_errno;

struct hostent *gethostbyname(const char *name);

Struct hostent {char *h_name; // official namechar **h_aliases; // a list of aliasesint h_addrtype;int h_length;char **h_addr_list;

}#define h_addr h_addr_list[0]- return value: NULL if fails