Sunteți pe pagina 1din 9

Socket programming

Socket programming o  Let's turn some of our


protocols into applications.
Building web applications o  There are two types of
network applications:
1. Implementations of a
protocol standard
(RFC); and
2. proprietary network
applications (do not
conform to any existing
CS242 Computer Networks RFCs.)*
o  Examples in Python for
Department of Computer Science simplicity of code.
Wellesley College

Socket programming 7-2

Recall that ... When a socket is created ...


o  A socket is a door between an application-layer process
... it is assigned
and the transport layer protocol. an identifier, called
its port number ...

Applications live
on this side

... and is associated


Transport-layer with a particular
protocols be here transport-layer protocol

o  The choice of socket depends on which transport layer *Each port number is 16-bits, ranging from 0 to 65535. Those in the range
protocol best suits the applications needs. 0 to 1023 are called well-known port numbers. Take care with these.

Socket programming 7-3 Socket programming 7-4

1

Socket programming with UDP* Danger Will Robinson
UDP: There is no connection
Client process
(IP 149.130.206.19)
between client and server
creates socket o  no handshaking
on port 5770 ...
o  sender explicitly attaches
Server process IP address and port of
(IP 128.119.245.12) destination to each packet
... through which creates socket
datagrams are on part 80 ... o  server must extract IP
sent/received address, port of sender
... through which from received packet
datagrams are
sent/received
UDP: transmitted data may be
received out of order, or
lost

*User Datagram Protocol.


Socket programming 7-5 Socket programming 7-6

We build a proprietary We continue to build a proprietary


client-server application (UDP) client-server application (UDP)

1. Client reads ... and is passed


a line from to the client
the keyboard ... process to be 3. Server
printed. converts message
to uppercase ...
2. Server receives
the message
... and sends it 4. Datagram
through
to server via socket its socket arrives at client’s
and ... socket ...
... and sends the
modified message
to the client
via its socket

*User Datagram Protocol. *User Datagram Protocol.


Socket programming 7-7 Socket programming 7-8

2

Client/server socket interaction: UDP UDPClient.py Module contains
network
Server (running on hostid) Client from socket import * communications
methods
create socket, create socket, serverName = ‘tempest.wellesley.edu’
port=x, for clientSocket = serverPort = 12000
incoming request: socket(AF_INET,SOCK_DGRAM)
serverSocket =
socket(AF_INET,SOCK_DGRAM) clientSocket = socket(AF_INET, SOCK_DGRAM)
Create datagram with serverIP
and port=x; via clientSocket
read UDP segment from message = raw_input(‘Input lowercase sentence:’)
serverSocket

clientSocket.sendto(message,(serverName, serverPort))
write reply to
serverSocket
specifying client read reply from modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
clientSocket
host address,
port number close print modifiedMessage
clientSocket

clientSocket.close()

Socket programming 7-9 Socket programming 7-10

UDPClient.py UDPClient.py
from socket import * from socket import *
‘hostname’ can be
serverName = ‘tempest.wellesley.edu’ either an IP address serverName = ‘tempest.wellesley.edu’
serverPort = 12000 or the URL of the serverPort = 12000
server* Creates the client’s socket
clientSocket = socket(AF_INET, SOCK_DGRAM) clientSocket = socket(AF_INET, SOCK_DGRAM)
Underlying network IPv4 UPD socket

message = raw_input(‘Input lowercase sentence:’) message = raw_input(‘Input lowercase sentence:’)

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

modifiedMessage, serverAddress = clientSocket.recvfrom(2048) modifiedMessage, serverAddress = clientSocket.recvfrom(2048)

print modifiedMessage print modifiedMessage

clientSocket.close() clientSocket.close()
*In which case a DNS lookup will automatically be performed to get the IP.
Socket programming 7-11 Socket programming 7-12

3

UDPClient.py UDPClient.py
from socket import * from socket import *

serverName = ‘tempest.wellesley.edu’ serverName = ‘tempest.wellesley.edu’


serverPort = 12000 serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = raw_input(‘Input lowercase sentence:’)
Prompts user and reads in response
message = raw_input(‘Input lowercase sentence:’) Creates UDP packet containing message, addressed to serverName &
serverPort, and sends it into process’s clientSocket.
clientSocket.sendto(message,(serverName, serverPort)) clientSocket.sendto(message,(serverName, serverPort))

modifiedMessage, serverAddress = clientSocket.recvfrom(2048) modifiedMessage, serverAddress = clientSocket.recvfrom(2048)

print modifiedMessage print modifiedMessage

clientSocket.close() clientSocket.close()

Socket programming 7-13 Socket programming 7-14

UDPClient.py UDPClient.py
from socket import * from socket import *

serverName = ‘tempest.wellesley.edu’ serverName = ‘tempest.wellesley.edu’


serverPort = 12000 serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = raw_input(‘Input lowercase sentence:’)
message = raw_input(‘Input lowercase sentence:’)
clientSocket.sendto(message,(serverName, serverPort))
clientSocket.sendto(message,(serverName, serverPort))
When packet arrives, puts payload
Payload goes here into 2048 byte buffer ...
modifiedMessage, serverAddress = clientSocket.recvfrom(2048) modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
... and retrieves
print modifiedMessage return address print modifiedMessage
IP & port of Print and
source close up shop
clientSocket.close() clientSocket.close()

Socket programming 7-15 Socket programming 7-16

4

UDPServer.py UDPServer.py
Same old, same old,
except we don’t need to
know our own address
from socket import * from socket import *
serverPort = 12000 serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket = socket(AF_INET, SOCK_DGRAM)
Server assigns port 1200
serverSocket.bind((’’, serverPort)) serverSocket.bind((’’, serverPort)) to its serverSocket
(so others know where
print “The server is ready to receive” print “The server is ready to receive” to find it)

while 1: while 1:
message, clientAddress = clientSocket.recvfrom(2048) message, clientAddress = clientSocket.recvfrom(2048)

modifiedMessage = message.upper() modifiedMessage = message.upper()

serverSocket.sendto(modifiedMessage, clientAddress) serverSocket.sendto(modifiedMessage, clientAddress)

Socket programming 7-17 Socket programming 7-18

UDPServer.py UDPServer.py

from socket import * from socket import *


serverPort = 12000 serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket = socket(AF_INET, SOCK_DGRAM)

serverSocket.bind((’’, serverPort)) serverSocket.bind((’’, serverPort))

print “The server is ready to receive” Ready to rock & rock print “The server is ready to receive”
(forever & ever)
while 1: while 1:
message, clientAddress = clientSocket.recvfrom(2048) message, clientAddress = clientSocket.recvfrom(2048)

modifiedMessage = message.upper() modifiedMessage = message.upper() Been there,


done that
serverSocket.sendto(modifiedMessage, clientAddress) serverSocket.sendto(modifiedMessage, clientAddress)

Socket programming 7-19 Socket programming 7-20

5

Shall we take 'er out for a spin? Socket programming with TCP
o  Open a window and ssh o  Unlike UDP, TCP is a connection-oriented protocol, that is,
into my account on before the client & server can send data to each other, they
tempest. Why tempest?* must first establish a TCP connection.
o  Fire up the server:
python UDPServer.py !
TCP connection
o  Open a second window and
identified by ...
ssh into any machine.
o  Fire up the client:
python UDPClient.py!

... client’s IP address & port ... together with the server’s
number ... IP address & port number

*Because the second line of my code reads:


servername = tempest.wellesley.edu. !
Socket programming 7-21 Socket programming 7-22

TCP connection sockets Client/server socket interaction: TCP


o  The client process
Server (running on hostid) Client
initiates a TCP connection create socket,
to a server which is port=x, for
incoming request:
listening for visitors on a serverSocket =
welcoming socket()
Serversocket.! TCP create socket, connect
wait for incoming
o  When the server hears a connection request connection setup to serverIP, port=x
clientSocket =
client knock, the connectionSocket = Socket()
welcoming socket invokes serverSocket.accept()
its accept() method. send request using
o  A new door, the read request from clientSocket
connectionSocket
Connectionsocket, is
created solely for the write reply to
client s use. From the application's connectionSocket read reply from
clientSocket
perspective, this connection
is a direct virtual pipe close
connectionSocket
between client and servers sockets close
clientSocket
Socket programming 7-23 Socket programming 7-24

6

TCPClient.py TCPClient.py Second parameter
indicates this is a TCP socket*
from socket import * from socket import *
serverName = ‘tempest.wellesley.edu’ Nothing new here serverName = ‘tempest.wellesley.edu’
serverPort = 12000 serverPort = 12000

clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket = socket(AF_INET, SOCK_STREAM)

sentence = raw_input(‘Input lowercase sentence:’) sentence = raw_input(‘Input lowercase sentence:’)

clientSocket.send(sentence) clientSocket.send(sentence)

modifiedMessage = clientSocket.recv(1024) modifiedMessage = clientSocket.recv(1024)

print ‘From Server:’, modifiedMessage print ‘From Server:’, modifiedMessage

clientSocket.close() clientSocket.close()

*We are still not specifying a port number; the OS is doing this for us.
Socket programming 7-25 Socket programming 7-26

TCPClient.py TCPClient.py
from socket import * from socket import *
serverName = ‘tempest.wellesley.edu’ serverName = ‘tempest.wellesley.edu’
serverPort = 12000 serverPort = 12000

clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket = socket(AF_INET, SOCK_STREAM)

sentence = raw_input(‘Input lowercase sentence:’) sentence = raw_input(‘Input lowercase sentence:’)

clientSocket.send(sentence) We do not explicitly create and clientSocket.send(sentence) Pretty much same, except
address packet. Instead, we simply we don’t need return address
drop the bytes into TCP connection since our connection is still open

modifiedMessage = clientSocket.recv(1024) modifiedMessage = clientSocket.recv(1024)

print ‘From Server:’, modifiedMessage print ‘From Server:’, modifiedMessage

clientSocket.close() clientSocket.close()

Socket programming 7-27 Socket programming 7-28

7

TCPServer.py TCPServer.py
from socket import * from socket import *
Nothing new here
serverPort = 12000 serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind((’’, serverPort)) serverSocket.bind((’’, serverPort))
The server listens for a
TCP connection requests
serverSocket.listen(1) serverSocket.listen(1) from the client
print “The server is ready to receive” print “The server is ready to receive”

while 1: while 1:
connectionSocket, addr = serverSocket.accept() connectionSocket, addr = serverSocket.accept()

sentence = connectionSocket.recv(1024) sentence = connectionSocket.recv(1024)

capitalizedSentence = sentence.upper() capitalizedSentence = sentence.upper()

connectionSocket.send(capitalizedSentence) connectionSocket.send(capitalizedSentence)

connectionSocket.close() connectionSocket.close()
Socket programming 7-29 Socket programming 7-30

TCPServer.py TCPServer.py
from socket import * from socket import *
serverPort = 12000 serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind((’’, serverPort)) serverSocket.bind((’’, serverPort))
When a client knocks
serverSocket.listen(1) serverSocket.listen(1)
on the door, the accept
print “The server is ready to receive” () method creates a print “The server is ready to receive”
new socket dedicated
while 1: to this client. while 1:
connectionSocket, addr = serverSocket.accept() connectionSocket, addr = serverSocket.accept()

sentence = connectionSocket.recv(1024) sentence = connectionSocket.recv(1024)


Mostly the same,
capitalizedSentence = sentence.upper() capitalizedSentence = sentence.upper() except we close the
connectionSocket
connectionSocket.send(capitalizedSentence) connectionSocket.send(capitalizedSentence) when we’re done (the
serverSocket,
however, stays open)
connectionSocket.close() connectionSocket.close()
Socket programming 7-31 Socket programming 7-32

8

Shall we take 'er out for a spin? Today’s puzzler: A Wellscape server*
o  Hope we’re still logged o  Handles only one HTTP
into my account on request.
tempest and connected to o  Accepts and parses the
cs242. HTTP request.
o  Now type into the first o  Gets the requested file
window: from the server s file
python TCPServer.py ! system.
and o  Creates an HTTP
python TCPClient.py! response message
into the second. consisting of the
requested file preceded
by header lines.
o  Sends the response
directly to the client.
*Non ministrari, sed minstrare.
Socket programming 7-33 Socket programming 7-34

Implementing a Web server


1.  Create a
listenSocket and
listen.
Handshake to
2.  When someone listenSocket
knocks on the door,
create a dedicated listenSocket
connection socket.
Client s browser
3.  Read and parse the opens socket
request from the Connection
Socket
connection socket.

4.  Serve up the files Create input and output


through the streams attached to
connectionSocket
connection socket.
Socket programming 7-35