networking chapter iii
DESCRIPTION
Networking chapter - III is about doing network programming using sockets.TRANSCRIPT
Compiled by: Jayakumar BalasubramanianWeb: http://www.jwritings.com
Email: [email protected]
Introduction Introduction to sockets The “Endian” concept Trivial functions Socket APIs Client server programming using sockets
Introduction
The “Endian” concept
Big-Endian
Little-Endian
Bit order transformation
Trivial functions
How does one talk to TCP/IP ?
Addressing in IP and TCP
Socket address
Socket APIs
API : Socket ()Use to create a socket.
fd = socket (family, type, protocol)
Family : AF_INET, AF_UNIX, AF_NS Type : SOCK_STREAM, SOCK_DGRAMProtocol : Protocol for a given socket
API: bind() [The anchor]
API: bind() Attach a transport address to a socket. status = bind (fd, addressp, addrlen); fd : Socket descriptor addressp : Pointer to address structure addrlen : Size of address structure
API: connect()
API : connect()The connect function is used by a TCP client to establish a connection with a TCP server
int fd, status, addrlen; struct sockaddr *addressp; status = connect (fd, addresssp, addrlen);
API: listen()
API: listen()listen() Prepares a socket to accept connections Must be used only on the server side of the
application before any connection request is accepted.
int fd, qlen, status; status = listen (fd, qlen);
fd : Socket descriptor qlen : Length of the queue
API : accept()
API : accept() Used in the server side of the connection to
accept incoming new connections When requests arrives it creates new socket,
accepts the connection on the new socket
int newfd, addrlen; struct sockaddr * addressp; newfd = accept (fd, addressp, & addrlen);
Connection establishment
API : send() Sends data over a communication channel Can be used in server and client side
int fd, len, flags; char *buff; sent = send (fd, buff, len, flags);
sent can be <= len
API : recv()Receives data over a communication channelCan be used in server and client side
int fd, len, flags, received; char *buff; received = recv (fd, buff, len, flags);
API: close() Close terminates both directions of data
transfer, reading and writing.
close( sockfd)
Web : http://www.jwritings.comEmail: [email protected]