Unit 3
Unit 3
GET /index.html
• Network
• Deliver data packet to the destination host
• Based on the destination IP address
• Operating system
• Deliver data to the destination socket
• Based on the destination port number (e.g., 80)
• Application
• Read data from and write data to the socket
• Interpret the data (e.g., render a Web page)
SOCKET: END POINT OF
COMMUNICATION
• Sending message from one process to another
• Message must traverse the underlying network
• Process sends and receives through a “socket”
• In essence, the doorway leading in/out of the house
• Socket as an Application Programming Interface
• Supports the creation of network applications
socket socket
Operating Operating
System System
IDENTIFYING THE RECEIVING
PROCESS
• Sending process must identify the receiver
• The receiving end host machine
• The specific socket in a process on that machine
• Receiving host
• Destination address that uniquely identifies the host
• An IP address is a 32-bit quantity
• Receiving socket
• Host may be running many different processes
• Destination port that uniquely identifies the socket
• A port number is a 16-bit quantity
USING PORTS TO IDENTIFY
SERVICES Server host 128.2.194.242
• Socket interface
• Originally provided in Berkeley UNIX
• Later adopted by all popular operating systems
• Simplifies porting applications to different OSes
• In UNIX, everything is like a file
• All input is like reading a file
• All output is like writing a file
• File is represented by an integer file descriptor
• API implemented as system calls
• E.g., connect, read, write, close, …
PUTTING IT ALL TOGETHER
Server
socket()
bind()
Client
listen()
socket()
establish
accept()
connection
connect()
block
send request
write()
read()
process
request
send response
write()
read()
CLIENT CREATING A SOCKET:
SOCKET()
• Creating a socket
• int socket(int domain, int type, int protocol)
• Returns a file descriptor (or handle) for the socket
• Originally designed to support any protocol suite
• Sending data
• ssize_t write
(int sockfd, void *buf, size_t len)
• Arguments: socket descriptor, pointer to buffer of data to send, and length of the buffer
• Returns the number of bytes written, and -1 on error
CLIENT: RECEIVING DATA
• Receiving data
• ssize_t read
(int sockfd, void *buf, size_t len)
• Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer
• Returns the number of characters read (where 0 implies “end of file”), and -1 on error
• Why do you need len?
• What happens if buf’s size < len?
• Create a socket
• int socket (int domain,int type, int protocol)
• Bind socket to the local address and port number
• int bind (int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
• Arguments: sockfd, server address, address length
• Returns 0 on success, and -1 if an error occurs
SERVER: ALLOWING CLIENTS TO
WAIT
• Many client requests may arrive
• Server cannot handle them all at the same time
• Server could reject the requests, or let them wait
• Define how many connections can be pending
• int listen(int sockfd, int backlog)
• Arguments: socket descriptor and acceptable backlog
• Returns a 0 on success, and -1 on error
• What if too many clients arrive?
• Some requests don’t get through
• The Internet makes no promises…
• And the client can always try again
SERVER: ACCEPTING
CLIENT CONNECTION
• Now all the server can do is wait…
• Waits for connection request to arrive
• Blocking until the request arrives
• And then accepting the new request
Kernel Support
Hardware
SOCKET API
• Socket API originated with the 4.2 BSD system released in 1983
• Sockets – A way to speak to other programs using UNIX file descriptors.
• A file descriptor is an integer associated with an open file.This can be a network
connection
• Kinds of Sockets-DARPA Internet addresses(Internet Sockets) , Unix Sockets,
X.25 Sockets etc
• Types of Internet Sockets
1. SOCK_STREAM uses TCP (Transmission Control Protocol) Connection oriented and Reliable
2. SOCK_DGRAM uses UDP (User Datagram Protocol)
Connectionless and Unreliable
STRUCTS AND DATA HANDLING
Byte ordering
Most significant byte first – Network byte order (Big Endian)
Least significant byte first – Host Byte order ( Little ..)
struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14
bytes of protocol address };
• socket01.utdallas.edu 129.110.43.11
inet_ntoa(ina.sin_addr) ;
SYSTEM CALLS
• getpeername() – Obtain the peer name given the socket file descriptor
• fcntl ()- affects an open file and controls it with multiple operations.
• Nonblocking I/O— We can set the O_NONBLOCK file status flag using the F_SETFL
command to set a socket as nonblocking.
• Signal-driven I/O— We can set the O_ASYNC file status flag using the F_SETFL
command, which causes the SIGIO signal to be generated when the status of a socket
changes.
• The F_SETOWN command lets us set the socket owner (the process ID or process group
ID) to receive the SIGIO and SIGURG signals. The former signal is generated when
signal-driven I/O is enabled for a socket and the latter signal is generated when new out-
of-band data arrives for a socket
• The F_GETOWN command returns the current owner of the socket.
• The term "socket owner" is defined by POSIX. Historically, Berkeley-derived
implementations have called this "the process group ID of the socket" because the
variable that stores this ID is the so_pgid member of the socket structure
SUMMARY OF fcntl, ioctl, AND
ROUTING SOCKET OPERATIONS
SUMMARY OF NETWORKING ioctl
REQUESTS.
USEFUL UNIX COMMANDS
• loop back interface is called lo and the ethernet interface is called eth0 or le0 depending on the machine
• ifconfig eth0 – Given the interface name ifconfig gives the details for each interface --- Ethernet Addr ,
inet_addr , Bcast , Mask , MTU
• ping IP_addr -- Sends a packet to the host specified by IP_addr and prints out the roundtrip time ( Uses ICMP
messages)
• traceroute IP_addr -- Shows the path from this host to the destination printing out the roundtrip time for a
packet to each hop in between
• Tcpdump communicates directly with Data Link layer UDP Packet fail
CLIENT-SERVER PROGRAMMING
• TCP Client Server Example
• Status of client-server before call to accept daytimetcpsrv1.c
• Server Client
listenfd ● ● connect()
listenfd ● ● connect()
connfd ●
Status of Client-Server after fork returns
Server ( Child)
listenfd●
connfd ●
Status of Client Server after parent and Child close appropriate sockets
Server(Client)
connfd ●
Socket functions of UDP Client Server
Socket()
Bind()
Socket() Recvfrom()
Sendto()
Blocks until datagram
received from a client
recvfrom
Sendto()
Close()