socket programming - knuain.knu.ac.kr/course/computernetworks2017/resources...2017-03-27 1 socket...
TRANSCRIPT
2017-03-27
1
Socket
programming
Socket programming
What is a socket?
goal: learn how to build client/server applications that communicate using
sockets
socket: door between application process and end-end-transport protocol
2-2
Internet
controlled
by OS
controlled byapp developer
transport
application
physical
link
network
process
transport
application
physical
link
network
processsocket
2017-03-27
2
What is a socket?
Socket is an interface between application and network (the lower levels of the protocol stack) The application creates a socket
The socket type dictates the style of communication
reliable vs. best effort
connection-oriented vs. connectionless
Once a socket is setup the application can pass data to the socket for network transmission
receive data from the socket (transmitted through the network, received from some other host)
3
Most popular types of sockets
• TCP socket
• Type: SOCK_STREAM
• reliable delivery
• in-order guaranteed
• connection-oriented
• bidirectional
UDP socket
Type: SOCK_DGRAM
unreliable delivery
no order guarantees
no notion of “connection” –app indicates destination
for each packet
can send or receive
2017-03-27
3
Ports
5
• Each host machine has an IP address (or more!)
• Each host has 65,536 ports (2?)
• As you know some ports are reserved for specific apps
• 20,21: FTP
• 23: Telnet
• 80: HTTP
• see RFC 1700 (about 2000 ports are reserved)
Port 0
Port 1
Port 65535
A socket provides an interface to send data to/from the network through a port
Addresses, Ports and Sockets
Like apartments and mailboxes
You are the application
Your apartment building address is the address
Your mailbox is the port
The post-office is the network
The socket is the key that gives you access to the right
mailbox (one difference: assume outgoing mail is placed by
you in your mailbox)
Q: How do you choose which port a socket connects to?
6
2017-03-27
4
Socket programming
2-7
Application Example:1. Client reads a line of characters (data) from its
keyboard and sends the data to the server.2. The server receives the data and converts
characters to uppercase.3. The server sends the modified data to the client.4. The client receives the modified data and displays
the line on its screen.
Ports
8
• Each host machine has an IP address (or more!)
• Each host has 65,536 ports (2?)
• As you know some ports are reserved for specific apps
• 20,21: FTP
• 23: Telnet
• 80: HTTP
• see RFC 1700 (about 2000 ports are reserved)
Port 0
Port 1
Port 65535
A socket provides an interface to send data to/from the network through a port
2017-03-27
5
Addresses, Ports and Sockets
Like apartments and mailboxes
You are the application
Your apartment building address is the address
Your mailbox is the port
The post-office is the network
The socket is the key that gives you access to the right
mailbox (one difference: assume outgoing mail is placed by
you in your mailbox)
Q: How do you choose which port a socket connects to?
9
Socket programming with UDP
UDP: no “connection” between client & server
no handshaking before sending data
sender explicitly attaches IP destination address and port # to each packet
rcvr extracts sender IP address and port# from received packet
UDP: transmitted data may be lost or received out-of-order
Application viewpoint:
UDP provides unreliable transfer of groups of bytes (“datagrams”) between
client and server
2-10
2017-03-27
6
Client/server socket interaction: UDP
close
clientSocket
read datagram from
clientSocket
create socket:
clientSocket =socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
clientSocket
create socket, port= x:
serverSocket =socket(AF_INET,SOCK_DGRAM)
read datagram from
serverSocket
write reply to
serverSocket
specifying
client address,
port number
Application 2-11
server (running on serverIP) client
2-12
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 UDPClientinclude Python’s socket library
create UDP socket for
server
get user keyboardinput
Attach server name, port to
message; send into socket
print out received string
and close socket
read reply characters from
socket into string
2017-03-27
7
2-13
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 port
number 12000
loop forever
Read from UDP socket into message, getting client’s address (client IP and port)
send upper case string
back to this client
Socket programming with TCP
client must contact server
server process must first be running
server must have created socket (door) that welcomes client’s contact
client contacts server by:
Creating TCP socket, specifying IP address, port number of server process
when client creates socket: client TCP establishes connection to server TCP
when contacted by client, server TCP creates new socket for server process to communicate with that particular client
allows server to talk with multiple clients
source port numbers used to distinguish clients
2-14
TCP provides reliable, in-orderbyte-stream transfer (“pipe”) between client and server
application viewpoint:
2017-03-27
8
Client/server socket interaction: TCP
2-15
wait for incoming
connection requestconnectionSocket =
serverSocket.accept()
create socket,port=x, for incoming
request:serverSocket = socket()
create socket,connect to hostid, port=x
clientSocket = socket()
server (running on hostid) client
send request using
clientSocketread request from
connectionSocket
write reply to
connectionSocket
TCP connection setup
close
connectionSocket
read reply from
clientSocket
close
clientSocket
2-16
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 for
server, remote port 12000
No need to attach server
name, port
2017-03-27
9
2-17
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 welcoming
socket
server begins listening for
incoming TCP requests
loop forever
server waits on accept()for incoming requests, new socket created on return
read bytes from socket (but
not address as in UDP)
close connection to this
client (but not welcoming
socket)
The bind function
The bind function associates and (can exclusively) reserves a
port for use by the socket
int status = bind(sockid, &addrport, size);
status: error status, = -1 if bind failed
sockid: integer, socket descriptor
addrport: struct sockaddr, the (IP) address and port of the machine
(address usually set to INADDR_ANY – chooses a local address)
size: the size (in bytes) of the addrport structure
bind can be skipped for both types of sockets. When and
why?
18
2017-03-27
10
19
Skipping the bind
SOCK_DGRAM:
if only sending, no need to bind. The OS finds a port each
time the socket sends a pkt
if receiving, need to bind
SOCK_STREAM:
destination determined during conn. setup
don’t need to know port sending from (during connection
setup, receiving end is informed of port)
On the connecting end
• When connecting to another host (i.e., connecting end is the client and the receiving end is the server), the OS automatically assigns a free port for the outgoing connection.
• During connection setup, receiving end is informed of port)
• You can however bind to a specific port if need be.
20
2017-03-27
11
Connection Setup
A connection occurs between two ends
Server: waits for an active participant to request connection
Client: initiates connection request to passive side
Once connection is established, server and client ends
are “similar”
both can send & receive data
either can terminate the connection
21
Server and clients
22
socket()
bind()
listen()
accept()
write()
read()
read()
TCP Server
close()
socket()
TCP Client
connect()
write()
read()
close()
connection establishment
data request
data reply
end-of-file notification
2017-03-27
12
Connection setup steps
Server end:
step 1: listen (for incoming requests)
step 3: accept (a request)
step 4: send/recv
The accepted connection is on a new socket
The old socket continues to listen for other active participants
Client end:
step 2: request &
establish connection
step 4: send/recv
23
Server
l-socka-sock-1 a-sock-2
Client1
socket
Client2
socket