466 Socket Programming
466 Socket Programming
192.168.23.100:143
web
server
OS mail
server
2
Communication - division of labor
• Network
– Gets data to the destination host
– Uses destination IP address
• Operating system
– Forwards data to a given "silo" based on port #
– E.g. All port 80 request go the web server
• Application
– Actually reads and writes to socket
– Implement the application specific magic
3
Port numbers
• Popular applications have known ports
– Server uses a well-known port, 0 - 1023
– Client uses a free temporary port, 1024 - 65535
• Assigned by the operating system
Port Service
21 File transfer protocol (FTP)
22 Secure shell (SSH)
23 Telenet
25 Simple mail transfer protocol (SMTP)
53 Domain name system (DNS)
80 Hypertext transfer protocol (HTTP)
110 Post office protocol (POP)
143 Internet message access protocol (IMAP)
443 HTTP secure (HTTPS)
4
Use of port number
OS mail
server
Requesting a 192.168.23.100:443
web
secure web page server
OS mail
server
OS mail
server
5
Sockets
• Socket API (applications programming interface)
– Originally in Berkeley Unix
• Thus: Berkeley sockets, BSD sockets
– De facto standard in all operating systems
–C:
• socket(), bind(), connect(), listen(),
accept(), send(), recv(), sendto(),
recvfrom(), close()
– Python:
• socket(), bind(), connect(), listen(),
accept(), send(), recv(), sentto(),
recvfrom(), close()
6
Sockets
• Socket API (applications programming interface)
– Java classes
• Socket
• ServerSocket
• InputStreamReader
• BufferedReader
• PrintWriter
7
High-level process
// Fire up connection // Initial socket setup
// to the server socket()
socket() bind()
connect() listen()
while (1)
// Exchange data {
while (!done) // Wait for new caller
{ accept()
send()
recv() // Exchange data
} while (!done)
{
// Shutdown recv()
close() send()
}
// Disconnect
close()
}
9
Socket interaction: UDP
server (running on serverIP) client
create socket, port = x: create socket:
serverSocket = clientSocket =
socket(AF_INET,SOCK_DGRAM) socket(AF_INET,SOCK_DGRAM)
write reply to
serverSocket read datagram from clientSocket
specifying
client address,
port number close
clientSocket
10
Python UDP client
from socket import * Include the socket library
serverName = 'hostname'
serverPort = 12000
IPv4 UDP transport
print modifiedMessage
clientSocket.close() Wait for
response
11
Python UDP server
from socket import *
Create IPv4 UDP socket
serverPort = 12000
serverSocket.sendto(modifiedMessage, clientAddress)
12
TCP socket programming
Client must contact server • When contacted by client,
• Server process must first be server TCP creates new
running socket for server process
• Server must have created to communicate with that
socket (door) that welcomes particular client
client’s contact – Allows server to talk
with multiple clients
Client contacts server by: – Source port numbers
• Creating TCP socket, used to distinguish
specifying IP address, port clients (more in Ch. 3)
number of server process
• When client creates socket: Application viewpoint:
client TCP establishes TCP provides reliable, in-order
connection to server TCP byte-stream transfer ("pipe")
between client and server
13
TCP: connection process
1. Server program starts up.
2. Starts listening on port 4242.
3. OS sends all inbound connection
requests to 4242 to the server
program.
modifiedSentence = clientSocket.recv(1024)
print 'From Server:', modifiedSentence
Close the TCP connection
clientSocket.close()
15
Python TCP server
from socket import *
Create TCP "welcoming" socket
serverPort = 12000
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
17