Socket Programming What Is A Socket Goal
Socket Programming What Is A Socket Goal
Socket programming
Socket
programming
What is a socket?
goal: learn how to build client/server applications that communicate using
sockets
application application
socket controlled by
process process app developer
transport transport
network network controlled
link by OS
link Internet
physical physical
2-2
1
2017-03-27
What is a socket?
2
2017-03-27
Ports
• Each host machine has an IP
address (or more!) Port 0
Port 1
3
2017-03-27
Socket programming
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.
2-7
Ports
• Each host machine has an IP
address (or more!) Port 0
Port 1
4
2017-03-27
2-
10
5
2017-03-27
write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
Application 2-11
2-
12
6
2017-03-27
2-
13
2-
14
7
2017-03-27
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
2-
15
2-
16
8
2017-03-27
2-
17
18
9
2017-03-27
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)
19
20
10
2017-03-27
Connection Setup
A connection occurs between two ends
Server: waits for an active participant to request connection
Client: initiates connection request to passive side
21
bind()
socket() accept()
connection establishment
connect()
data request read()
write()
11
2017-03-27
12