Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Sockets

Similar presentations


Presentation on theme: "Introduction to Sockets"— Presentation transcript:

1 Introduction to Sockets
shri Introduction to Sockets K.C. Rao CDAC-Mumbai

2 Socket Interface What is the Socket interface? History
It is a protocol independent interface to multiple transport layer primitives It is an interface (a “door”) into which an application process can both send and receive messages to/from another (remote or local) application process History The first implementation was released along with BSD 4.2. Since then, it has undergone several improvements CDAC-Mumbai

3 Typical Client-Server interaction using TCP
socket() well-known port bind() socket() listen() connect() accept() Connection establishment TCP three-way handshake Block until connection from client write() Data (request) read() Process requests write() Data (reply) read() close() read() End-of-file notification CDAC-Mumbai close()

4 Creating a Socket #include <sys/socket.h> int socket(int family, int type, int protocol); Returns: non-negative socket descriptor if OK, –1 on error family specifies address or protocol family. Can be AF_INET, AF_INET6, AF_LOCAL, etc type specifies socket type. Can be SOCK_DGRAM, SOCK_STREAM, SOCK_RAW protocol argument is normally left to zero except for raw sockets- need not bother about it in this elementary discussion CDAC-Mumbai

5 Closing a socket #inlcude <unistd.h> int close(int sockfd); Returns: 0 if OK, –1 on error The transport layer generally tries to flush out, i.e. send, any remaining data, when a close is done. CDAC-Mumbai

6 Establishing a connection
#include <sys/socket.h> int connect(int sockfd, const struct sockaddr* servaddr, socklen_t servaddrlen); Returns: 0 if OK, -1 on error This function is used by a client to establish a connection with a server The socket address structure, sockaddr, contains the address information of the server to connect to In general, the port number to be used on the client’s side is chosen by the kernel (ephemeral port) CDAC-Mumbai

7 Background Information: Socket Address Structure
This contains the protocol specific addressing information The general structure is named sockaddr Each of the protocols supported by a socket implementation have their own socket address structure sockaddr_suffix -Where ‘suffix’ represents the protocol family Eg: sockaddr_in – Internet/IPv4 socket address structure sockaddr_ipx – IPX socket address structure CDAC-Mumbai

8 The Socket Address Structure(POSIX)
struct sockaddr { sa_family_t sa_family; /*Address Family, AF_XXX*/ char sa_data[14]; /*14 bytes of protocol address*/ }; struct sockaddr_in { sa_family_t sin_family; /* must be AF_INET */ in_port_t sin_port; /*16-bit TCP or UDP port number*/ ___ /*network byte ordered*/ struct in_addr sin_addr; /*32-bit IPv4 address*/ /*network byte ordered*/ char sin_zero[8]; /* Not used, must be zero */ }; struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address*/ /*network byte ordered */ }; CDAC-Mumbai

9 Associating a local address with a socket
#include <sys/socket.h> int bind(int sockfd, struct sockaddr* myaddr, socklen_t addrlen); Returns: 0 if OK, -1 if error It is used for explicitly associating a protocol specific local address to a socket For Internet Protocols, bind() associates with the socket a port number and/or one of the IP addresses of the host on which the program is running (note that the host may be multi-homed) CDAC-Mumbai

10 Associating a local address with a socket
It is necessary for a server wanting to listen for connections on some port, as also for connectionless applications (eg UDP based) On unbounded sockets an implicit bind is done with IN_ADDRANY and a random port as the address and port parameters respectively CDAC-Mumbai

11 Listening for a connection
#include <sys/socket.h> int listen(int sockfd, int backlog); Returns: 0 if OK, -1 if error listen() performs two duties: It specifies to the kernel that it should passively listen for connections to the address associated to sockfd It specifies the maximum number of pending connections the kernel should queue for this socket CDAC-Mumbai

12 Accepting a connection
#include <sys/socket.h> int accept(int sockfd, struct sockaddr* cliaddr, socklen_t* addrlen); Returns: non-negative descriptor if OK, -1 on error This function is called by the server to get the next connection from the queue of completed connections If no connection has been completed, the function call blocks A new socket descriptor is returned for the connection The structure pointed to by cliaddr is filled in with information about the protocol address of the connected peer process(client), while *addrlen gives the size of this structure CDAC-Mumbai

13 Functions for sending/receiving data
#include <sys/types.h> #include <sys/socket.h> int write(int sockfd, char* buf, int nbytes); /*Return value: No. of bytes sent if OK, -1 on error*/ int read(int sockfd, char* buf, int nbytes); /*Return value: No. of bytes read if OK, 0 on connection closure, -1 on error*/ CDAC-Mumbai

14 Some additional functions
In network programming, we often need the help of some additional functions for tasks like: Byte ordering Byte operations Address Conversions CDAC-Mumbai

15 Byte Ordering High-order byte Low-order byte MSB 16-bit value LSB
Increasing memory address Address A+1 Address A Little-endian byte order High-order byte Low-order byte MSB bit value LSB Consider a 16-bit integer that is made up of 2 bytes: there are two ways to store the 2 bytes in memory, with low order byte at the starting address, known as little endian byte order, or with the high order byte at the starting address, known as big endian byte order Big-endian byte order High-order byte Low-order byte Address A Address A+1 Increasing memory address CDAC-Mumbai

16 Implications of Byte Order
Unfortunately there is no standard byte order- some systems are big endian, the others little endian We refer to the byte ordering used by a given system as host byte order The sender and the receiver must agree on the order in which the bytes of multi-byte field transmitted. Hence we specify the network byte order, which is big-endian byte ordering CDAC-Mumbai

17 Byte Order Functions #include <netinet/in.h>
#include <sys/types.h> uint16_t htons(uint16_t host16bitvalue) uint32_t htonl(uint32_t host32bitvalue) Return: value in network byte order uint16_t ntohs(uint16_t net16bitvalue) uint32_t ntohl(uint32_t net32bitvalue) Return: value in host byte order CDAC-Mumbai

18 Byte Manipulation Functions
#include <string.h> void *memset(void *s, int c, size_t n); /*Return: Pointer to the memory area s */ void *memcpy(void *dest, const void *src, size_t n); /*Return: Pointer to dest */ int memcmp(const void *s1, const void *s2, size_t n); /* Return: <0, =0, >0, if first n bytes of s1 are respectively less than, equal to, or greater than the first n bytes of s2 */ CDAC-Mumbai

19 Address Conversion Functions
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int inet_aton(const char* strptr, struct in_addr* addrptr); /* return non-zero if string was valid,0 if error */ char* inet_ntoa(struct in_addr inaddr); /* returns: pointer to dotted-decimal string */ CDAC-Mumbai

20 Address Conversion Functions
#include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> int inet_pton(int family, const char *strpr, void *addrptr); /* returns 1 if OK, 0 if input not a valid presentation format, -1 on other errors*/ const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len); /*returns pointer to result if OK, NULL on error*/ CDAC-Mumbai

21 References Unix Network Programming :Vol 1- Stevens
Network Programming for Microsoft Windows –Jones, Ohlund Java Network Programming – Rusty, Harold TCP/IP Illustrated - Stevens CDAC-Mumbai


Download ppt "Introduction to Sockets"

Similar presentations


Ads by Google