SOCKETS
COMPUTER NETWORK
A
computer network is an interconnected collection of autonomous computers.
WHAT A NETWORK INCLUDES
Address:
byte-string that identifies a node
usually unique
Routing:
process of forwarding messages to the destination node based on its address. Protocol: set of rules used for communication.
BASIC PARADIGM FOR COMMUNICATION
Establish contact (connection). Exchange information (bi-directional). Terminate contact.
ELEMENTS OF C-S COMPUTING
a client, a server, and network
Re qu es t
Client
Server
Re su lt
Network
Client machine
Server machine
CLIENT-SERVER COMMUNICATION
Client sends a request to the server
Server accepts the request and establish a connection
SOCKETS
A socket is defined as an endpoint for communication. Concatenation of IP address and port A socket pair (local IP address, local port, foreign IP address, foreign port) uniquely identifies a communication. The socket [Link]:1625 refers to port 1625 on host [Link]
TWO ESSENTIAL TYPES OF SOCKETS
SOCK_STREAM TCP reliable delivery in-order guaranteed connection-oriented Bidirectional Stream flow of data
SOCK_DGRAM UDP unreliable delivery no order guarantees no notion of connection app indicates dest. for each packet can send or receive Datagram format
App D1
App
3 2 1
socket
Dest.
3 2 1
socket
D3
D2
SOCKET FUNCTIONAL CALLS
socket (): Create a socket bind(): bind a socket to a local IP address and port # listen(): passively waiting for connections connect(): initiating connection to another socket accept(): accept a new connection Write(): write data to a socket Read(): read data from a socket sendto(): send a datagram to another UDP socket recvfrom(): read a datagram from a UDP socket close(): close a socket (tear down the connection)
CONNECTION-ORIENTED EXAMPLE (TCP)
Server Socket() Client
10
Bind() Socket() Listen() Connection Establishmt. Block until connect Process request Send() Accept() Recv() Data (request) Bind() Connect() Send()
Data (reply) Recv()
CONNECTIONLESS EXAMPLE (UDP)
Server Socket() Bind() Recvfrom() Block until Data from client Process request Sendto() Data (reply) Recvfrom() Client Socket() Bind() Data (request) Sendto()
11
JAVA SOCKETS
ServerSocket(1254)
server
12
Output/write stream Input/read stream
Client
Socket([Link], 1254) It can be host_name like [Link]