Unix Network Programming: Herguan University 28th Sept., 2010
Unix Network Programming: Herguan University 28th Sept., 2010
(CS546)
Lectured By
Dr. Chethana Nagaraja
•Contact:
[email protected]
408-481-9988
•Assessment:
•Network Layer:
Address format
Address resolution
• Introduction to Sockets:
IP Datagram
Fragmentation by source
IP Packet IP Packet
Reassembling by destination
IP Datagram
IP Datagram
•4 formats
Assigned by Network Information Centre (NIC) at SRI Intl.
• Class A
• Class B
• Class C
•Class D
Multicast addresses
bind ( )
CLIENT
listen ( )
socket ( )
accept ( )
connect ( )
blocks until client connects connected
read ( ) write ( )
data (request)
process request
write ( ) read ( )
data (reply)
(a) (b)
Socket address format (a) IPv4 fixed length (16 bytes) and (b) IPv6 fixed length (24 bytes)
Parameters:
Type Description
Family Description
SOCK_STREAM Stream socket
AF_INET IPv4 Protocol
SOCK_DGRAM Datagram socket
AF_INET6 IPv6 Protocol
SOCK_RAW Raw socket
• bind ( ):
Assigns a local protocol address to the socket.
Format:
#include <sys/socket.h>
int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addelen);
Purpose of bind ( ):
Provides identity to server and client via an address which facilitates communication
between server and client.
• connect ( ):
Used by the client to connect with the server.
Format:
#include <sys/socket.h>
int connect (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
• listen ( ):
Used by the server to indicate that it is ready to connect.
Format:
#include <sys/socket.h>
int listen (int sockfd, int backlog);
backlog specifies the maximum number of connections that the kernel can
queue.
Incomplete connection queue: Sockets in SYN_RCVD state.
Complete connection queue: Sockets in ESTABLISHED state.
• accept ( ):
Executed by the server to return the completed connection from the front of the
queue.
Format:
#include <sys/socket.h>
int accept (int sockfd, struct sock *cliaddr, socklen_t *addrlen);
accept ( ) returns nonnegative integer called the connect socket on success, else
-1 on error.
• close ( ):
Terminates the connection by closing the socket.
Format:
#include <unistd.h>
int close (int sockfd);