5

Click here to load reader

L1Python

Embed Size (px)

Citation preview

Page 1: L1Python

8/9/2019 L1Python

http://slidepdf.com/reader/full/l1python 1/5

1

1

The Application Layer:Socket Programming I

CSC 249

Sept 28, 2012 

All material copyright 1996-2012J.F Kurose and K.W. Ross, All Rights Reserved

2

Overview

UNIX Socket API overview! 

Socket Programming" 

Function calls

Example

! Discuss TCP vs. UDP"

 

UDP: unreliable datagram 

TCP: reliable, byte stream-oriented

3

Socket Programming

Internet

controlled

by OS

controlled byapp developer

transport

application

physical

link 

network 

 process

transport

application

physical

link 

network 

 processsocket

Application layer communication via the transport layer

 goal: learn how to build client/server applications thatcommunicate using sockets 

socket: door between application process and end-end-

transport protocol

4

Socket API Overview! 

Socket Programming Procedures"

 

Socket()" 

Bind()

Listen()

"  Accept()" 

Connect()

Send and receive procedures"

 

Close()

And for DNS…"

 

getHostByName

getServByName

getProtoByName

Page 2: L1Python

8/9/2019 L1Python

http://slidepdf.com/reader/full/l1python 2/5

2

5

TCPFlowChart

socket()

 bind()

listen()

accept()

recv()

send()

socket()

 bind()

connect 

send()

recv()

SERVER

CLIENT

6

UDP

FlowChartsocket()

 bind()

recvfrom()

sendto()

socket()

 bind()

sendto()

recvfrom()

SERVER CLIENT

7

Procedures: Socket()

descriptor = socket(protoFamily, type)" 

Creates a socket and returns an integerdescriptor

"  ProtoFamily – refers to Family of protocols thatthis protocol belongs to, for TCP/IP usePF_INET

Type – SOCK_STREAM, SOCK_DGRAM•  SOCK_STREAM – Connection Oriented (TCP)

• 

SOCK_DGRAM – Connectionless (UDP)

8

Socket-programming using TCP 

Socket: an interface between application process andthe transport protocol (UCP or TCP)

TCP service: reliable transfer of bytes from oneprocess to another 

Internet

controlled

by OS

controlled byapp developer

transport

application

physical

link 

network 

 process

transport

application

physical

link 

network 

 processsocket

TCP: define/reserve

variables and buffers

Page 3: L1Python

8/9/2019 L1Python

http://slidepdf.com/reader/full/l1python 3/5

3

9

A first, simple socket example 

Client-server simple application: 1) The client reads a line from

standard input (inFromUser stream), sends it to server viasocket (outToServer  stream)

2) The server reads the line from itssocket

3) The server converts the line touppercase, and sends it back to theclient

4) The client reads the modified linefrom its socket (inFromServer  stream) and prints it to standardoutput

    o   u     t     T    o     S    e    r   v    e    r

tonetwork from network

     i    n     F    r    o    m     S    e    r   v    e    r

     i    n     F    r    o    m     U    s    e    r

keyboard monitor 

Process

clientSocket

inputstream

inputstream

outputstream

TCPsocket

Client

process 

client TCPsocket

Client/server socket interaction: TCP

10

Client contacts server

server process must first berunning

server must have createdsocket (door) that acceptsclient’s contact

Client contacts server by:

Creating TCP socket,"  Reserve required memory

"  Specify server IP address andport number

Have client TCP layerestablish connection toserver TCP layer (via TCPhandshaking, chap 3)

Server creates new  socket!

 

when contacted by clie nt, forserver process to communicatewith that particular client

allows server to talk withmultiple clients

"  source port numbers used todistinguish clients (more inChap 3) 

TCP provides reliable, in-orderbyte-stream transfer (“pipe”)between client and server

Application viewpoint:

11

Client/server socket interaction: TCP 

wait for incomingconnection request 

create socket,port=x, for

incoming request:

serverSocket = socket() 

create socket,connect to hostid , port=x 

closeconnectionSocket 

read reply fromclientSocket 

closeclientSocket 

Server (running on hostid )  Client 

send request usingclientSocket read request from

connectionSocket 

write reply toconnectionSocket 

TCPconnection setup 

connectionSocket =serverSocket.accept() 

clientSocket = socket() 

 Application Layer 2-12

Example app: TCP client 

from socket import *

serverName = ’servername’

serverPort = 12000

clientSocket = socket(AF_INET, SOCK_STREAM)

clientSocket.connect((serverName,serverPort))

sentence = raw_input(‘Input lowercase sentence:’)

clientSocket.send(sentence)

modifiedSentence = clientSocket.recv(1024)

print ‘From Server:’, modifiedSentence

clientSocket.close()

Python TCPClient

create TCP socket forserver, remote port 12000

No need to attach servername, port

Page 4: L1Python

8/9/2019 L1Python

http://slidepdf.com/reader/full/l1python 4/5

4

 Application Layer 2-13

Example app: TCP server 

from socket import *serverPort = 12000

serverSocket = socket(AF_INET,SOCK_STREAM)

serverSocket.bind((‘’,serverPort))

serverSocket.listen(1)

print ‘The server is ready to receive’

while 1:

connectionSocket, addr = serverSocket.accept()

sentence = connectionSocket.recv(1024)

capitalizedSentence = sentence.upper()

connectionSocket.send(capitalizedSentence)

connectionSocket.close() 

Python TCPServer

create TCP welcomingsocket

server begins listening forincoming TCP requests

loop forever

server waits on accept()for incoming requests, new

socket created on return

read bytes from socket (butnot address as in UDP)

close connection to thisclient (but not  welcoming

socket)

14

Socket programming with UDP  

UDP: no “connection” between client and server ! 

no handshaking

sender explicitly attaches IP address and port ofdestination to each packet

server must extract IP address, port of sender fromreceived packet

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

application viewpoint 

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

between client and server  

Client/server socket interaction: UDP 

closeclientSocket 

read datagram fromclientSocket 

create socket:

clientSocket =socket(AF_INET,SOCK_DGRAM) 

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

clientSocket 

create socket, port= x: 

serverSocket =socket(AF_INET,SOCK_DGRAM) 

read datagram fromserverSocket 

write reply to

serverSocket

specifying

client address,

port number  

 Application 2-15

server (running on serverIP) client

16

Example: UDP client 

    s    e    n      d      P    a    c      k    e     t

to network from network

    r    e    c    e      i    v    e      P    a    c      k    e     t

      i    n      F    r    o    m      U    s    e    r

keyboard moni tor 

Process

clientSocket

UDP

packet

input

stream

UDP

packet

UDP

socket

Output: sendspacket (recall

that TCP sent“byte stream”) 

Input: receivespacket (recall thatTCP received“byte stream”) 

Client

process 

client UDPsocket

Page 5: L1Python

8/9/2019 L1Python

http://slidepdf.com/reader/full/l1python 5/5

5

 Application Layer 2-17

Example app: UDP client 

from socket import *

serverName = ‘hostname’

serverPort = 12000

clientSocket = socket(socket.AF_INET,

socket.SOCK_DGRAM)

message = raw_input(’Input lowercase sentence:’)

clientSocket.sendto(message,(serverName, serverPort))

modifiedMessage, serverAddress =

clientSocket.recvfrom(2048)

print modifiedMessage

clientSocket.close()

Python UDPClient

include Python’s socketlibrary

create UDP socket forserver

get user keyboardinput

 Attach server name, port tomessage; send into socket

print out received stringand close socket

read reply characters fromsocket into string

 Application Layer 2-18

Example app: UDP server 

from socket import *

serverPort = 12000

serverSocket = socket(AF_INET, SOCK_DGRAM)

serverSocket.bind(('', serverPort))

print “The server is ready to receive”

while 1:

message, clientAddress = serverSocket.recvfrom(2048)

modifiedMessage = message.upper()

serverSocket.sendto(modifiedMessage, clientAddress)

Python UDPServer

create UDP socket

bind socket to local portnumber 12000

loop forever

Read from UDP socket intomessage, getting client’s

address (client IP and port)

send upper case stringback to this client

19

Socket Summary

! Sockets defined" 

Through analogy to a “door”•  An interface between the application and

the Internet

Through examples with socket-APIprogramming

" Through understanding the differencesbetween TCP and UDP sockets