0% found this document useful (0 votes)
34 views

466 Socket Programming

Uploaded by

D Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

466 Socket Programming

Uploaded by

D Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Socket programming

192.168.23.100:143
web
server

OS mail
server

Computer Networking: A Top Down Approach


6th edition
Jim Kurose, Keith Ross Some materials copyright 1996-2012
Addison-Wesley J.F Kurose and K.W. Ross, All Rights Reserved
Overview application

• Chapter 2: Application Layer transport

– Many familiar services operate here network


• Web, email, Skype, P2P file sharing link
– Socket programming
physical
• Socket programming
– In Python
– UDP
– TCP

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

Requesting a non- 192.168.23.100:80


web
secure web page server

OS mail
server

Requesting a 192.168.23.100:443
web
secure web page server

OS mail
server

Requesting new 192.168.23.100:143


web
email messages 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()
}

Client program Server program 8


Socket programming: UDP
UDP: no "connection" between client & server
• No handshaking before sending data
• Sender explicitly attaches IP destination address and
port # to each packet
• Receiver extracts sender IP address and port # from
received packet

UDP: data may be lost or received out-of-order


Application viewpoint:
• UDP provides unreliable transfer of groups of bytes
("datagrams") between client and server

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)

Create datagram with server IP and port=x;


send datagram via clientSocket
read datagram from
serverSocket

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

clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)

Send off data


message = raw_input('Input lowercase sentence:')
to server+port

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

modifiedMessage, serverAddress = clientSocket.recvfrom(2048)

print modifiedMessage
clientSocket.close() Wait for
response
11
Python UDP server
from socket import *
Create IPv4 UDP socket
serverPort = 12000

serverSocket = socket(AF_INET, SOCK_DGRAM)


Listen on specific port #
serverSocket.bind(('', serverPort))

print "The server is ready to receive" Blocks until message arrives


Gets data as well as who sent it
while 1:
message, clientAddress = serverSocket.recvfrom(2048)

Send processed data back


modifiedMessage = message.upper()

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.

4. Client program starts up


5. Requests connection to server IP
address on port 4242.

6. Server establishes a socket


connection to client, but on
different port (2789)
7. Server can listen for new clients
on the 4242 port number.
14
Python TCP client
from socket import *

serverName = 'servername' TCP transport


serverPort = 12000
Make the
clientSocket = socket(AF_INET, SOCK_STREAM) connection
clientSocket.connect((serverName,serverPort))

sentence = raw_input('Input lowercase sentence:')


Send some data (no
clientSocket.send(sentence) need to say to who!)

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

serverSocket = socket(AF_INET,SOCK_STREAM) Begin listens for incoming


serverSocket.bind(('',serverPort)) TCP connections

serverSocket.listen(1) Block until somebody


print 'The server is ready to receive' comes knocking, socket
created on return
while 1:
connectionSocket, addr = serverSocket.accept()

sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)

connectionSocket.close() Close the client's TCP connection,


not the welcoming socket 16
Summary
• Socket programming
– Berkley socket API
– UDP
• Just fire off messages towards destinations
• They may not get there
• They may arrive out of order
– TCP
• Establish a reliable byte-stream between two hosts
• Data always arrives
• Data arrives in order

17

You might also like