Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

COMPUTER NETWORKS- LECTURE 34

DR. RASHMI SAHAY


BERKELEY SOCKET
BERKELEY SERVICE PRIMITIVES
 Used in Berkeley UNIX for TCP
 Addressing primitives: socket
bind

listen
 Server primitives:
accept
send + receive
close

connect
 Client primitives:
send + receive
close
BERKELEY SERVICE PRIMITIVES

socket create new communication end point

bind attach a local address to a socket

listen announce willingness to accept connections; give queue size

accept block caller until a connection request arrives

connect actively attempt to establish a connection

send send some data over the connection

receive receive some data from the connection

close release the connection


CREATING A SOCKET
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);

• domain is one of the Protocol Families (PF_INET,


PF_UNIX, etc.)

• type defines the communication protocol semantics,


usually defines either:
– SOCK_STREAM: connection-oriented stream (TCP)
– SOCK_DGRAM: connectionless, unreliable (UDP)

• protocol specifies a particular protocol, just set this to 0 to


accept the default
SERVER

Create socket

TCP CLIENT/SERVER
bind a port to the
socket

CLIENT
listen for incoming
Create socket
connections

accept an
connect to server's
incoming
port
connection

read from the write to the


connection connection

loop loop

write to the read from the


connection connection

close connection
TCP SERVER
 Sequence of calls

sock_init() Create the socket

bind() Register port with the system

listen() Establish client connection

accept() Accept client connection

read/write read/write data

close() shutdown
TCP CLIENT

 Sequence of calls

sock_init () Create socket

connect() Set up connection

write/read write/read data

close() Shutdown
SERVER SIDE SOCKET DETAILS
SERVER

Create socket
int socket(int domain, int type, int protocol)
sockfd = socket(PF_INET, SOCK_STREAM, 0);

bind a port to the int bind(int sockfd, struct sockaddr *server_addr, socklen_t length)
socket
bind(sockfd, &server, sizeof(server));

listen for incoming int listen( int sockfd, int num_queued_requests)


connections
listen( sockfd, 5);

accept an
incoming
int accept(int sockfd, struct sockaddr *incoming_address, socklen_t length)
connection newfd = accept(sockfd, &client, sizeof(client)); /* BLOCKS */

read from the int read(int sockfd, void * buffer, size_t buffer_size)
connection
read(newfd, buffer, sizeof(buffer));

write to the int write(int sockfd, void * buffer, size_t buffer_size)


connection
write(newfd, buffer, sizeof(buffer));
CLIENT SIDE SOCKET DETAILS

CLIENT

Create socket
int socket(int domain, int type, int protocol)
sockfd = socket(PF_INET, SOCK_STREAM, 0);

connect to Server int connect(int sockfd, struct sockaddr *server_address, socklen_t length)
socket
connect(sockfd, &server, sizeof(server));

write to the int write(int sockfd, void * buffer, size_t buffer_size)


connection
write(sockfd, buffer, sizeof(buffer));

read from the int read(int sockfd, void * buffer, size_t buffer_size)
connection
read(sockfd, buffer, sizeof(buffer));
TRANSPORT LAYER

 Service Primitives

 Elements of transport protocol

 Simple transport protocol

 TCP

 UD
ELEMENTS OF TRANSPORT PROTOCOLS (ETP)
 Transport <> Data Link
 Addressing
 Establishing a connection
 Releasing a connection
 Flow control and buffering
 Multiplexing
 Crash recovery
ETP: TRANSPORT <> DATA LINK

 Physical channel <> subnet

Explicit addressing
Connection establishment
Potential existence of storage capacity in subnet
Dynamically varying number of connections
ETP: ADDRESSING

 TSAP = transport service access point


 Internet: IP address + local port
 ATM: AAL-SAPs
 Connection scenario
 Getting TSAP addresses?
 From TSAP address to NSAP address?
ETP: ADDRESSING

 Connection scenario
ETP: ADDRESSING

 Connection scenario
 Host 2 (server)
 Time-of-day server attaches itself to TSAP 1522
 Host 1 (client)
 Connect from TSAP 1208 to TSAP 1522
 Setup network connection to host 2
 Send transport connection request
 Host 2
 Accept connection request
ETP: ADDRESSING
 Getting TSAP addresses?
 Stable TSAP addresses
 For key services
 Not for user processes
 active for a short time
 number of addresses limited
 Name servers
 to find existing servers
 map service name into TSAP address
 Initial connection protocol
ETP: ADDRESSING

 Getting TSAP addresses?


 Initial connection protocol
 to avoid many waiting
servers ➔ one process server
 waits on many TSAPs
 creates requested server
ETP: ADDRESSING
 From TSAP address to NSAP address?
 hierarchical addresses
 address = <country> <network> <host> <port>
 Examples: IP address + port
Telephone numbers (<> number portability?)
 Disadvantages:
 TSAP bound to host!
 flat address space
 Advantages:
 Independent of underlying network addresses
 TSAP address not bound to host
 Mapping to network addresses:
 Name server
 broadcast

You might also like