The Application Layer: Socket Programming I: UNIX Socket API Overview Socket Programming Discuss TCP vs. UDP
The Application Layer: Socket Programming I: UNIX Socket API Overview Socket Programming Discuss TCP vs. UDP
v Example
CSC 249
Sept 28, 2012
v TCP:
Socket Programming
socket
application
process
transport
transport
network
network
link
physical
Internet
process
unreliable datagram
reliable, byte stream-oriented
application
calls
link
controlled by
app developer
controlled
by OS
physical
SERVER
socket()
bind()
listen()
SERVER
CLIENT
socket()
bind()
accept()
connect
()
recv()
send()
send()
recv()
socket()
TCP
Flow
Chart
UDP
Flow
Chart
CLIENT
socket()
bind()
bind()
recvfrom()
sendto()
sendto()
recvfrom()
Procedures: Socket()
application
process
transport
network
link
physical
socket
application
process
TCP: define/reserve
variables and buffers
transport
Internet
link
network
controlled by
app developer
controlled
by OS
physical
Client
Process
process
input
stream
running
q server must have created
socket (door) that accepts
clients contact
output
stream
inFromServer
monitor
inFromUser
keyboard
client
TCP
clientSocket
socket
to network
input
stream
establish connection to
server TCP layer (via TCP
handshaking, chap 3)
TCP
socket
from network
Application viewpoint:
TCP provides reliable, in-order
byte-stream transfer (pipe)
between client and server
(running on hostid)
10
Python TCPClient
Client
create socket,
port=x, for
incoming request:
serverSocket = socket()
wait for incoming
connection request
TCP
connection setup
connectionSocket =
serverSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
create socket,
connect to hostid, port=x
clientSocket = socket()
Application Layer
2-12
Python TCPServer
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()
Application Layer
application viewpoint
14
client
create socket:
clientSocket =
socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
clientSocket
input
stream
Client
Process
monitor
Input: receives
process
packet (recall
that TCP sent
byte stream)
UDP
packet
client
UDP
clientSocket
socket
to network
close
clientSocket
Application 2-15
Output: sends
receivePacket
loop forever
server waits on accept()
for incoming requests, new
socket created on return
inFromUser
sendPacket
UDP
packet
UDP
socket
from network
16
Python UDPClient
Python UDPServer
Application Layer
2-17
Application Layer
2-18
Socket Summary
qSockets defined
v Through analogy to a door
An interface between the application and
the Internet
v Through
19