Top Banner
CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights Reserved Jack Lange University of Pittsburgh 1
20

CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

Jan 05, 2016

Download

Documents

Nancy Hood
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: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

CS1652September 12th, 2013

The slides are adapted from the publisher’s material All material copyright 1996-2009

J.F Kurose and K.W. Ross, All Rights Reserved

Jack Lange

University of Pittsburgh

1

Page 2: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 22

Socket programming

Socket APIr introduced in BSD4.1 UNIX,

1981

r explicitly created, used, released by apps

r client/server paradigm

r 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: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 23

Socket-programming using TCP

Socket: 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 byapplicationdeveloper

controlled byoperatingsystem

host orserver

internet

Page 4: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 24

Socket programming with TCPClient must contact server

r server process must first be running

r server must have created socket (door) that welcomes client’s contact

Client contacts server by:

r creating client-local TCP socket

r specifying IP address, port number of server process

r When client creates socket: client TCP establishes connection to server TCP

r 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: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 25

Client/server socket interaction: TCP

wait for incomingconnection requestconnectionSocket =accept(listenSocket)

create socket,port=x, forincoming request:

listenSocket = socket()

closeconnectionSocket close

clientSocket

Server (running on hostid) Client

read reply fromclientSocket

send request usingclientSocketread request from

connectionSocket

write reply toconnectionSocket

TCP connection setup

create socket,connect to hostid, port=xclientSocket =

socket()

connect to hostid, port=x

connect(clientSocket, dst)

Page 6: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 26

Client

process

client TCP socket

Stream jargon

r A stream is a sequence of characters that flow into or out of a process.

r An input stream is attached to some input source for the process, e.g., keyboard or socket.

r An output stream is attached to an output source, e.g., monitor or socket.

r fgets(), fputs(), getchar(), putchar()

Page 7: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 27

Socket programming with TCP

Example client-server app:1) client reads line from standard input, sends to server via

socket

2) server reads line from socket

3) server converts line to uppercase, sends back to client

4) client reads, prints modified line from socket

Page 8: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 28

Example: C client (TCP)int main(const int argc, const char** argv) { int s, port, len, res; char buf[MAX_LINE]; struct hostent *hp; struct sockaddr_in saddr;

if (argc < 3 || ((port = atoi(argv[2])) <= 0 || port > 65535)) error processing; if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) error processing;

if ((hp = gethostbyname(argv[1])) == NULL) error processing;

saddr.sin_family = AF_INET; memcpy(&saddr.sin_addr.s_addr, hp->h_addr, hp->length); saddr.sin_port = htons(port); if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) error processing;

createsocket

name lookupconnect to server

parameter checking

Page 9: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 29

Example: C client (TCP)

if (fgets(buf, sizeof(buf), stdin) == NULL) error processing; if ((res = write(s, buf, len)) <= 0) error processing;

if ((res = read(s, buf, sizeof(buf)-1) <= 0) error processing; buf[res] = 0; printf(“received: %s”, buf);

close(s); return 0;}

Send the line to server

Receive the line from server

Print out the line from server

Close socket

Read a line from stdin

Page 10: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 30

Example: C server (TCP)int main(const int argc, const char** argv){ int i, s, c, len, pos, res, port; char buf[MAX_LINE]; struct sockaddr_in saddr;

if (argc < 2 || ((port = atoi(argv[1])) <= 0 || port > 65535)) error processing;

if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) error processing;

memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(port); if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) error processing;

if (listen(s, 32) < 0) error processing;

Create socket

Reserve the port on interfaces

Declare it isis a server socket

Create a client connection queue

Page 11: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 31

Example: C server (TCP), cont

while ((c = accept(s, NULL, NULL)) >= 0) {

if ((len = read(c, buf, sizeof(buf) -1)) <= 0) error processing;

buf[len] = 0; for (i = 0; i < len; i++) { if (islower(buf[i])) buf[i] = toupper(buf[i]); }

if ((res = write(c, buf, len)) <= 0) error processing; close(c); } close(s); return(0);}

Read in line

from socket

Accept a new client connection

Write out lineto socket

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

Page 12: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 32

Example: What’s not covered?

r Error handling read() == 0 ?

r Socket buffer issue A kernel-level buffer to hold the user-level content before

sending/receiving Two buffers per socket: send/receive buffers Implication: write(s, buf, len) < len What about read?

r Feel free to reuse the code for assignment 1 Read socket programming primer linked at the course page

Page 13: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 33

Socket programming with UDP

UDP: no “connection” between client and server

r no handshaking

r sender explicitly attaches IP address and port of destination to each packet

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

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

application viewpoint

UDP provides unreliable transfer of groups of bytes (“datagrams”)

between client and server

Page 14: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 34

Client/server socket interaction: UDP

Server (running on hostid)

closeclientSocket

read datagram fromclientSocket

create socket,clientSocket = DatagramSocket()

Client

Create datagram with server IP andport=x; send datagram via clientSocket

create socket,port= x.serverSocket = DatagramSocket()

read datagram fromserverSocket

write reply toserverSocketspecifying client address,port number

Page 15: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 35

Example: C client (UDP)

Output: sends packet (recall

that TCP sent “byte stream”)

Input: receives packet (recall thatTCP received “byte stream”)

Client

process

client UDP socket

Page 16: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 36

Example: C client (UDP)int main(const int argc, const char** argv) { int s, port, len, res, fromlen; char buf[MAX_LINE]; struct hostent *hp; struct sockaddr_in saddr, raddr;

if (argc < 3 || ((port = atoi(argv[2])) <= 0 || port > 65535)) error processing; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) error processing;

if ((hp = gethostbyname(argv[1])) == NULL) error processing;

memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; memcpy(&saddr.sin_addr.s_addr, hp->h_addr, hp->h_length); saddr.sin_port = htons(port);

Create client socket

Translate hostname to IP

address using DNS

Page 17: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 37

Example: C client (UDP), cont. if (fgets(buf, sizeof(buf), stdin) == NULL) error processing; len = strlen(buf); if ((res = sendto(s, buf, len, 0, (struct sockaddr *)&saddr, sizeof(saddr))) < 0) error processing;

fromlen = sizeof(raddr); if ((res = recvfrom(s, buf, sizeof(buf)-1, 0, (struct sockaddr *)&raddr, &fromlen) < 0) error processing; buf[res] = 0; printf(“received: %s”, buf);

close(s); return 0;}

Create datagram with data-to-send,

length, IP addr, port

Send datagramto server

Read datagramfrom server

Page 18: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 38

Example: C server (UDP)int main(const int argc, const char** argv){ int i, s, len, res, port, fromlen; char buf[MAX_LINE]; struct sockaddr_in saddr, claddr;

if (argc < 2 || ((port = atoi(argv[1])) <= 0 || port > 65535)) error processing;

if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) error processing;

memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(port); if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) error processing;

Createa socket

Reserve a portto receive datagram

Page 19: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 39

Example: C server (UDP), cont while (1) { fromlen = sizeof(claddr); if ((len = recvfrom(s, buf, sizeof(buf)-1, 0, (struct sockaddr *)&claddr, &fromlen)) <= 0) error processing;

buf[len] = 0; for (i = 0; i < len; i++) { if (islower(buf[i])) buf[i] = toupper(buf[i]); }

if ((res = sendto(s, buf, len, 0, (struct sockaddr *)&claddr, fromlen)) <= 0) error processing; } close(s); return(0);}

Get IP addrport #, ofsender

End of while loop,loop back and wait foranother datagram

Create and send datagram to client

Receivedatagram

Page 20: CS1652 September 12th, 2013 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

2: Application Layer 40

Summary

r Application architectures client-server P2P Hybrid

r Internet transport service model TCP: connection-oriented, reliable UDP: unreliable, datagrams

r Socket programming socket(), read()/send(), write()/recv(),

sendto(), recvfrom(), close()