Contents Socket Socket Structures Socket Functions TCP Example TCP Client TCP Server UDP Example UDP Client UDP Server Functions for Multicasting
Socket Socket Source ( IP , Port ) Destination ( IP , Port ) Protocol ( TCP , UDP , SCTP) Connection Oriented / Connection less
Socket Structures sockaddr sockaddr_in Connection information. Used by connect , send , recv etc in_addr IP address in long format hostent The IP addresses of a hostname. Used by gethostbyname ()
Socket Functions socket: creates a socket of a given domain, type, protocol (buy a phone) bind: assigns a name to the socket (get a telephone number) listen: specifies the number of pending connections that can be queued for a server socket. (call waiting allowed) accept: server accepts a connection request from a client (answer phone) connect: client requests a connection request to a server (call) send, sendto : write to connection (speak) recv , recvfrom : read from connection (listen) shutdown: end the call
Sockets Functions used for TCP
Sockets Functions used for UDP
Socket Functions Supporting Functions For Port Numbers htons / htonl Host to Network Byte Order (short-16/long-32) ntohs / ntohl Network to Host Byte Order (short-16/long-32) For IP Address inet_ntoa convert an IP address to dotted format inet_addr convert an IP address to a long format For Host gethostbyname
socket() int socket ( int domain , int type , int protocol) domain (Address Family) AF_INET (IP version 4) AF_INET6 (IP version 6) Type : SOCK_STREAM (connection oriented TCP protocol) SOCK_DGRAM (connectionless UDP protocol) Protocol : 0 , (zero) to detect protocol according to the type IPPROTO_TCP returns Socket Descriptor on success
socket()
bind() int bind( int sid , struct sockaddr * addrptr , int len ) sid : socket ID obtained through socket() * addrptr : (for local host) Family dependent address Used to bind IP and Port number to a socket len Length of the addrptr
bind()
listen() int listen( int sid , int size) sid : Socket descriptor obtained through socket() size: Number of connections that can be handled concurrently returns 0 on success or -1 in failure Example listen ( socket_desc , 5 );
connect() int connect( int sid , struct sockaddr * addrptr , int len ) sid : socket ID obtained through socket() * addrptr : (for remote host or Destination) Family dependent address Used to specify destination (IP and Port) len Length of the addrptr returns 0 on success and -1 on failure
connect()
accept() int accept( int sid , struct sockaddr * addrptr , int len ) sid : socket ID obtained through socket() * addrptr : ( remote host who requested to connect) Family dependent address Used to get remote client address (IP and Port) len Length of the addrptr returns new sock descriptor and address of a remote client who requested the connection by connect()
send() / recv () int send ( int sid , const char *buffer, int len , int flag) Int recv ( int sid , const char *buffer, int len , int flag) int sendto ( int sid , const char *buffer, int len , int flag struct sockaddr * addrptr , int addrptr_len ) int recvfrom ( int sid , const char *buffer, int len , int flag struct sockaddr * addrptr , int addrptr_len )
Getting IP of a host name/domain struct hostent * gethostbyname ()
Functions for UDP communication int sendto int sid const char *buffer, int len , int flag struct sockaddr * addrptr , int addrptr_len int recvfrom int sid , const char *buffer, int len , int flag struct sockaddr * addrptr , int addrptr_len
Functions for Multicasting int getsockopt ( int sid , int level, int optname , void* optval , int * optlen ); int setsockopt ( int sid , int level, int optname , const void* optval , int optlen ); int IN_MULTICAST ( ntohl ( Addr.sin_addr.s_addr ))
Multicasting – Function Arguments Socket ID AF_INET SOCK_DGRAM or SOCK_RAW Level (identifies the layer that is to handle the option, message or query) SOL_SOCKET (socket layer) IPPROTO_IP (IP layer)
Multicasting – Function Arguments optname (option for multicasting) setsockopt() getsockopt() IP_MULTICAST_LOOP yes yes IP_MULTICAST_TTL yes yes IP_MULTICAST_IF yes yes IP_ADD_MEMBERSHIP yes no IP_DROP_MEMBERSHIP yes no
Options Value for Multicasting loopback u_char loop ; // loop = 0 or 1 setsockopt (socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop , sizeof (loop)); TTL u_char ttl ; // default = 1 for own network only, 0 to 255 setsockopt (socket, IPPROTO_IP, IP_MULTICAST_TTL, & ttl , sizeof ( ttl )); Interface (multicast datagram sent from) struct in_addr interface_addr ; setsockopt (socket, IPPROTO_IP, IP_MULTICAST_IF, & interface_addr , sizeof ( interface_addr ));
Options Value for Multicasting Membership request structure struct ip_mreq { /* IP multicast address of group */ struct in_addr imr_multiaddr ; /* local IP address of interface */ struct in_addr imr_interface ; }; Add Members struct ip_mreq mreq ; setsockopt (socket, IPPROTO_IP , IP_ADD_MEMBERSHIP, & mreq , sizeof ( mreq )); Drop Members struct ip_mreq mreq ; setsockopt (socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, & mreq , sizeof ( mreq ));