networked applications: refresh on socketsrefresh on sockets

22
Networked Applications: Refresh on Sockets Refresh on Sockets Topics Programmer’s view of the Internet Sockets interface End System: Computer on the ‘Net Internet –2– Also known as a “host”…

Upload: others

Post on 23-Jul-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Networked Applications: Refresh on SocketsRefresh on Sockets

Networked Applications:Refresh on SocketsRefresh on Sockets

TopicsProgrammer’s view of the Internet Sockets interface

End System: Computer on the ‘Net

Internet

– 2 –Also known as a “host”…

Page 2: Networked Applications: Refresh on SocketsRefresh on Sockets

Clients and Servers

Client programRunning on end host

Server programRunning on end hostRunning on end host

Requests service

E g Web browser

Running on end host

Provides service

E g Web serverE.g., Web browser E.g., Web server

GET /index.html

– 3 –“Site under construction”

Client-Server Communication

Client “sometimes on”Initiates a request to the

Server is “always on”Services requests from q

server when interested

E.g., Web browser on your laptop or cell phone

qmany client hosts

E.g., Web server for the www.cnn.com web sitep p p

Doesn’t communicate directly with other clients

Needs to know the server’s

Doesn’t initiate contact with the clients

Needs a fixed well-knownNeeds to know the server s address

Needs a fixed, well-known address

– 4 –

Page 3: Networked Applications: Refresh on SocketsRefresh on Sockets

A Programmer’s View of the Internet

1. Hosts are mapped to a set of 32-bit IP addresses.128.2.203.179

2 The set of IP addresses is mapped to a set of identifiers2. The set of IP addresses is mapped to a set of identifiers called Internet domain names.

128.2.203.179 is mapped to www.cs.cmu.edu

3. Internet sockets are communication endpoints.

4. A process on one Internet host can communicate with a process on another Internet host over a connection

– 5 –

process on another Internet host over a connection.

Internet Sockets

Sending message from one process to anotherMessage must traverse the underlying networkg y g

Process sends and receives through a “socket”In essence, the doorway leading in/out of the house

Socket as an Application Programming InterfaceSupports the creation of network applications

User process User process

socket socketOperatingSystem

OperatingSystem

– 6 –

System System

Page 4: Networked Applications: Refresh on SocketsRefresh on Sockets

Using Ports to Identify Services

Client host

Server host 128.2.194.242

Service request forWeb server

(port 80)

Client host q128.2.194.242:80

(i.e., the Web server)OSClient

Echo server(port 7)

Service request forWeb server

(port 80)

Service request for128.2.194.242:7

(i.e., the echo server)OSClient

– 7 –

Echo server(port 7)

Internet Connections

Clients and servers communicate by sending streams of bytes over connections.

Connections are point-to-point, full-duplex (2-way communication), and reliable.

Client socket address128.2.194.242:51213

Server socket address208.216.181.15:80

Connection socket pair(128.2.194.242:51213, 208.216.181.15:80)

Server(port 80)

Client

Client host address128.2.194.242

Server host address208.216.181.15

Note: 51213 is an Note: 80 is a well-known port

– 8 –

ephemeral port allocatedby the kernel

associated with Web servers

Page 5: Networked Applications: Refresh on SocketsRefresh on Sockets

Knowing What Port Number To Use

Popular applications have well-known portsE.g., port 80 for Web and port 25 for e-mailg , p p

See http://www.iana.org/assignments/port-numbers

Well-known vs. ephemeral portsServer has a well-known port (e.g., port 80)

Between 0 and 1023

Client picks an unused ephemeral (i.e., temporary) portClient picks an unused ephemeral (i.e., temporary) portBetween 1024 and 65535

Uniquely identifying the traffic between the hostsTwo IP addresses and two port numbers

Underlying transport protocol (e.g., TCP or UDP)

– 9 –

Port Numbers are Unique on Each Host

Port number uniquely identifies the socketCannot use same port number twice with same addressp

Otherwise, the OS can’t demultiplex packets correctly

Operating system enforces uniquenessOS keeps track of which port numbers are in use

Doesn’t let the second program use the port number

E l t W b i hiExample: two Web servers running on a machineThey cannot both use port “80”, the standard port #

So, the second one might use a non-standard port #So, the second one might use a non standard port #

E.g., http://www.cnn.com:8080

– 10 –

Page 6: Networked Applications: Refresh on SocketsRefresh on Sockets

Typical Client Program

Prepare to communicateCreate a socketCreate a socket

Determine server address and port number

Initiate the connection to the serverInitiate the connection to the server

Exchange data with the serverWrite data to the socketWrite data to the socket

Read data from the socket

Do stuff with the data (e g render a Web page)Do stuff with the data (e.g., render a Web page)

Close the socket

– 11 –

Servers Differ From Clients

Passive openPrepare to accept connectionsPrepare to accept connections

… but don’t actually establish

until hearing from a client… until hearing from a client

Hearing from multiple clientsAllowing a backlog of waiting clientsAllowing a backlog of waiting clients

... in case several try to communicate at once

C t k t f h li tCreate a socket for each clientUpon accepting a new client

t k t f th i ti

– 12 –

… create a new socket for the communication

Page 7: Networked Applications: Refresh on SocketsRefresh on Sockets

Typical Server Program

Prepare to communicateCreate a socketAssociate local address and port with the socket

Wait to hear from a client (passive open)I di t h li t i iti t itIndicate how many clients-in-waiting to permitAccept an incoming connection from a client

Exchange data with the client over new socketExchange data with the client over new socketReceive data from the socketDo stuff to handle the request (e.g., get a file)Send data to the socketSend data to the socketClose the socket

Repeat with the next connection request

– 13 –

p q

Java Sockets InterfaceClient Server

ServerSocket

Connectionrequest

accept

read

Socket

write

q

writeread

Await connectionrequest fromnext client

read

close

close EOF

– 14 –

close

Page 8: Networked Applications: Refresh on SocketsRefresh on Sockets

C Sockets InterfaceC Sockets Interface

More complexMore complex

– 15 –

C Sockets Interface

socket()

Server

socket()

bind()

Cli tlisten()

Client

socket()

accept()

block

connect()

it ()read()

processrequest

write()

– 16 –

write()

q

read()

Page 9: Networked Applications: Refresh on SocketsRefresh on Sockets

C Sockets Interfacesocket

First step in making a network connection: create a socket

bindThe server binds the socket so it can listen

li tlistenThe server listens on the socket for connection request from client

connectThe client tries to connects to the server (connection request sent over the socket))

acceptThe accepts the connection request: connection established

– 17 –

Help with C Sockets Interface Client Server

socket socket

bind

li t

open_listenfd

open_clientfd

listen

acceptconnect

Connectionrequest

readwrite

Await connectionrequest from

read

writeread

close EOF

request fromnext client

– 18 –closeSee(nethelp.c, nethelp.h)

(available on class website)

Page 10: Networked Applications: Refresh on SocketsRefresh on Sockets

Echo Client: Main Routine#include “nethelp.h"

/* usage: ./echoclient host port */int main(int argc, char **argv){

int clientfd, port; char *host, buf[MAXLINE];

M t b i l t d host = argv[1]; port = atoi(argv[2]);

clientfd = open_clientfd(host, port);

Must be implemented.Not built-in.

while (fgets(buf, MAXLINE, stdin) != NULL) {write(clientfd, buf, strlen(buf));n = (clientfd, buf, MAXLINE);readlinewrite(1, buf, n);

}close(clientfd);exit(0);

– 19 –

}

Echo Server: Main Routineint main(int argc, char **argv) {

int listenfd, connfd, port, clientlen;struct sockaddr clientaddr;

port = atoi(argv[1]); /* the server listens on a port passed on the command line */

clientlen = sizeof(clientaddr);li t fd li t fd( t)listenfd = open_listenfd(port);

while (1) {connfd = accept(listenfd, &clientaddr, &clientlen);

/* Service client */h ( fd) /* Service client */close(connfd); /* Close connection with client */

}}

echo(connfd);

Must be implemented.

– 20 –

Not built-in.

Page 11: Networked Applications: Refresh on SocketsRefresh on Sockets

Echo Server: Main Loop

The server loops endlessly, waiting for connection requests, then reading input from the client, and echoing the input back to the client.

main() {

/* create and configure the listening socket */

while(1) {/* accept(): wait for a connection request *//* echo(): read and echo input lines from client til EOF *//* close(): close the connection */

}}

– 21 –

Echo Server: acceptaccept() blocks waiting for a connection request.

int listenfd; /* listening descriptor */i t fd /* t d d i t */int connfd; /* connected descriptor */struct sockaddr_in clientaddr;int clientlen;

li tl i f( li t dd )

accept returns a connected descriptor (connfd) with the

clientlen = sizeof(clientaddr); connfd = accept(listenfd, (SA *)&clientaddr, &clientlen);

accept returns a connected descriptor (connfd) with the same properties as the listening descriptor (listenfd)

Returns when the connection between client and server is created and ready for I/O transferscreated and ready for I/O transfers.

All I/O with the client will be done via the connected socket.

accept also fills in client’s IP address.

– 22 –

accept also fills in client s IP address.

Page 12: Networked Applications: Refresh on SocketsRefresh on Sockets

Echo Server: accept Illustrated

listenfd(3)

Client

1. Server blocks in accept, waiting for connection request on listeningServer request on listening descriptor listenfd.clientfd

listenfd(3)

Client Server2. Client makes connection request by calling and blocking in

Connectionrequest

clientfd connect.

listenfd(3)

Client Server

3. Server returns connfd from accept. Client returns from connect. Connection is now established between clientfd

– 23 –

clientfd established between clientfdand connfd.

connfd(4)

Connected vs. Listening Descriptors

Listening descriptorEnd point for client connection requests.p q

Created once and exists for lifetime of the server.

Connected descriptorEnd point of the connection between client and server.

A new descriptor is created each time the server accepts a connection request from a client.q

Exists only as long as it takes to service client.

Why the distinction?Allows for concurrent servers that can communicate over many client connections simultaneously.

E.g., Each time we receive a new request, we create a new

– 24 –

g , q ,thread to handle the request.

Page 13: Networked Applications: Refresh on SocketsRefresh on Sockets

Echo Server: echo

The server reads and echoes text lines until EOF (end-of-file) is encountered.

EOF notification caused by client calling close(clientfd).

IMPORTANT: EOF is a condition, not a particular data byte.

void echo(int connfd) {

i tsize_t n; char buf[MAXLINE];

while((n = readline(connfd, buf, MAXLINE)) != 0) { i tf(" i d %d b t \ " )printf("server received %d bytes\n", n);

write(connfd, buf, n); }

}

– 25 –

Testing Servers Using telnet

The telnet program is invaluable for testing servers that transmit ASCII strings over Internet connections

Our simple echo server

Web servers

Mail serversMail servers

UUsage: bash$ telnet <host> <portnumber>

Creates a connection with a server running on <host> and glistening on port <portnumber>.

– 26 –

Page 14: Networked Applications: Refresh on SocketsRefresh on Sockets

Testing the Echo Server With telnetbash$ echoserver 5000

In a separate terminal window:

bash$ telnet tanner 5000Trying 128.2.222.85...Connected to tanner.csc.villanova.eduEscape character is '^]'.p ]123123Connection closed by foreign host.

bash$

– 27 –

Server: One Request at a Time?

Serializing requests is inefficient

Server can process just one request at a timeServer can process just one request at a time

All other clients must wait until previous one is done

Implement multi-threaded serversImplement multi threaded servers

Start a new thread to handle each request

– 28 –

Page 15: Networked Applications: Refresh on SocketsRefresh on Sockets

Client and Server: Cleaning House

Once the connection is openBoth sides and read and write

Two unidirectional streams of data

In practice, client writes first, and server reads

th it d li t d d… then server writes, and client reads, and so on

Closing down the connectionEither side can close the connectionEither side can close the connection

… using the close() system call

What about the data still “in flight”gData in flight still reaches the other end

So, server can close() before client finishing reading

– 29 –

One Annoying Thing: Byte Order

Hosts differ in how they store dataE.g., four-byte number (byte3, byte2, byte1, byte0)g , y ( y , y , y , y )

Little endian (“little end comes first”) Intel PCs!!!Low-order byte stored at the lowest memory location

Byte0, byte1, byte2, byte3

Big endian (“big end comes first”)High-order byte stored at lowest memory location

Byte3, byte2, byte1, byte 0

Makes it more difficult to write portable codeMakes it more difficult to write portable codeClient may be big or little endian machine

Server may be big or little endian machine

– 30 –

Page 16: Networked Applications: Refresh on SocketsRefresh on Sockets

Endian Example: Where is the Byte?

31 24 23 16 15 8 7 0

1 2 3 4 5 6 7 8

+3 +2 +1 +0

1000 78

+1 +0

1000 781000 78

8 bits memory 16 bits Memory 32 bits Memory

1000

1004

1008

781000

1002

1004

781000

1001

1002

78

Little-Endian

+0 +1 +2 +3+0 +1

100C10061003

1000

1004

1008

781000

1002

1004

781000

1001

1002Big-Endian

78

– 31 –

100C10061003

IP is Big Endian

But, what byte order is used “on the wire”That is, what do the network protocol use?, p

The Internet Protocols picked one conventionIP is big endian (aka “network byte order”)

Writing portable code require conversionUse htons() and htonl() to convert to network byte order

Use ntohs() and ntohl() to convert to host order

Hides details of what kind of machine you’re onU th t ll h di / i i d t t tUse the system calls when sending/receiving data structures longer than one byte

– 32 –

Page 17: Networked Applications: Refresh on SocketsRefresh on Sockets

Why Can’t Sockets Hide These Details?

Dealing with endian differences is tediousCouldn’t the socket implementation deal with thisp

… by swapping the bytes as needed?

No, swapping depends on the data typeTwo-byte short int: (byte 1, byte 0) vs. (byte 0, byte 1)

Four-byte long int: (byte 3, byte 2, byte 1, byte 0) vs. (byte 0, byte 1, byte 2, byte 3), y , y 3)

String of one-byte charters: (char 0, char 1, char 2, …) in both cases

Socket layer doesn’t know the data typesSees the data as simply a buffer pointer and a length

Doesn’t have enough information to do the swapping

– 33 –

Doesn t have enough information to do the swapping

The Web as an Example Cli t/S A li tiClient/Server Application

Page 18: Networked Applications: Refresh on SocketsRefresh on Sockets

The Web: URL, HTML, and HTTP

Uniform Resource Locator (URL)A pointer to a “black box” that accepts request methodsp p q

Formatted string with protocol (e.g., http), server name (e.g., www.cnn.com), and resource name (coolpic.jpg)

H T t M k L (HTML)HyperText Markup Language (HTML)Representation of hyptertext documents in ASCII format

Format text, reference images, embed hyperlinks, g , yp

Interpreted by Web browsers when rendering a page

HyperText Transfer Protocol (HTTP)Client-server protocol for transferring resources

Client sends request and server sends response

– 35 –

Example: HyperText Transfer Protocol

GET /~mdamian/CSC8560 / HTTP/1.1Host: www csc villanova eduHost: www.csc.villanova.edu<CRLF> Request

HTTP/1 1 200 OKHTTP/1.1 200 OKDate: Mon, 27 Aug 2012 08:09:03 GMTServer: Apache/1.3.27 (Unix)L t M difi d S 26 A 2012 15 45 05 GMTLast-Modified: Sun, 26 Aug 2012 15:45:05 GMTContent-Type: text/plainContent-Length: 259

Response

– 36 –

g<CRLF>…

Page 19: Networked Applications: Refresh on SocketsRefresh on Sockets

Components: Clients, Proxies, Servers

ClientsSend requests and receive responsesq p

Browsers, spiders, and agents

ServersReceive requests and send responses

Store or generate the responses

P i ( “HTTP P ” i t!)Proxies (see “HTTP Proxy” assignment!)Act as a server for the client, and a client to the server

Perform extra functions such as anonymization, logging, blockingPerform extra functions such as anonymization, logging, blocking of access, caching, etc.

– 37 –

Example Client: Web Browser

Generating HTTP requestsUser types URL, clicks a hyperlink, or selects bookmarkyp , yp ,

User clicks “reload”, or “submit” on a Web page

Automatic downloading of embedded images

Layout of responseParsing HTML and rendering the Web page

Invoking helper applications (e g Acrobat PowerPoint)Invoking helper applications (e.g., Acrobat, PowerPoint)

Maintaining a cacheStoring recently-viewed objectsStoring recently viewed objects

Checking that cached objects are fresh

– 38 –

Page 20: Networked Applications: Refresh on SocketsRefresh on Sockets

Client: Typical Web Transaction

User clicks on a hyperlinkhttp://www.cnn.com/index.htmlp

Browser learns the IP addressInvokes gethostbyname(www.cnn.com)

And gets a return value of 64.236.16.20

Browser creates socket and connects to serverOS selects an ephemeral port for client side

Contacts 64.236.16.20 on port 80

Browser writes the HTTP request into the socketBrowser writes the HTTP request into the socket“GET /index.html HTTP/1.1Host: www.cnn.com

CRLF ”

– 39 –

<CRLF>”

In Fact, Try This at a UNIX Prompt…

telnet www.cnn.com 80GET /index html HTTP/1 1GET /index.html HTTP/1.1<CRLF>

– 40 –

And you’ll see the response…

Page 21: Networked Applications: Refresh on SocketsRefresh on Sockets

Client: Typical Web Transaction (Cont)

Browser parses the HTTP response message

Extract the URL for each embedded imageExtract the URL for each embedded image

Create new sockets and send new requests

Render the Web page, including the images

Opportunities for caching in the browserOpportunities for caching in the browser

HTML file

Each embedded image

IP address of the Web site

– 41 –

Web Server

Web site vs. Web serverWeb site: collections of Web pages associated with aWeb site: collections of Web pages associated with a particular host name

Web server: program that satisfies client requests for p g qWeb resources

Handling a client requestg qAccept the socket

Read and parse the HTTP request message

Translate the URL to a filename

Determine whether the request is authorized

– 42 –

Generate and transmit the response

Page 22: Networked Applications: Refresh on SocketsRefresh on Sockets

Web Proxy

See assignment.

– 43 –