Socket Programming in C/C++
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()
sockaddr , sockaddr_in & in_addr
hostent
Socket Functions TCP UDP socket bind listen accept connect write /send read / recv socket bind sendto recvfrom
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 Functions • Supporting functions Example – htons & inet_addr • struct sockaddr_in server; • server.sin_port = htons( 80 ); • server.sin_addr.s_addr = inet_addr("74.125.235.20");
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
UDP Receiver sd=socket(AF_INET, SOCK_DGRAM, 0); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(SERVER_PORT); rc = bind (sd,(struct sockaddr *)&servAddr, sizeof(servAddr)); n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr , sizeof(cliAddr)
UDP sender sd = socket(AF_INET,SOCK_DGRAM,0); remoteServAddr.sin_family = AF_INET; remoteServAddr.sin_addr.s_addr = htonl(SERVER_IP); remoteServAddr.sin_port = htons(SERVER_PORT); rc = sendto(sd, msg, strlen(msg)+1, 0, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr));
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));
Multicast Receiver sd = socket(AF_INET,SOCK_DGRAM,0); servAddr.sin_family=AF_INET; servAddr.sin_addr.s_addr=htonl(INADDR_ANY); servAddr.sin_port=htons(SERVER_PORT); rc = bind(sd,(struct sockaddr *) &servAddr, sizeof(servAddr)) /* join multicast group */ mreq.imr_multiaddr.s_addr=mcastAddr.s_addr; mreq.imr_interface.s_addr=htonl(INADDR_ANY); rc = setsockopt(sd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(void *) &mreq, sizeof(mreq)); n = recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr *) &cliAddr,&cliLen);
Multicast Sender //simple UDP sender sd = socket(AF_INET,SOCK_DGRAM,0); setsockopt(sd,IPPROTO_IP,IP_MULTICAST_TTL, &ttl,sizeof(ttl) servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(SERVER_IP); servAddr.sin_port = htons(SERVER_PORT); rc = sendto(sd,argv[i],strlen(argv[i])+1,0, (struct sockaddr *) &servAddr, sizeof(servAddr));

Socket programming

  • 1.
  • 2.
    Contents • Socket • SocketStructures • Socket Functions • TCP Example – TCP Client – TCP Server • UDP Example – UDP Client – UDP Server • Functions for Multicasting
  • 3.
    Socket • Socket – Source( IP , Port ) – Destination ( IP , Port ) – Protocol ( TCP , UDP , SCTP) • Connection Oriented / Connection less
  • 4.
    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()
  • 5.
  • 6.
  • 7.
    Socket Functions TCP UDP socket bind listen acceptconnect write /send read / recv socket bind sendto recvfrom
  • 8.
    Socket Functions socket: createsa 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
  • 9.
  • 10.
  • 11.
    Socket Functions • SupportingFunctions – 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
  • 12.
    Socket Functions • Supportingfunctions Example – htons & inet_addr • struct sockaddr_in server; • server.sin_port = htons( 80 ); • server.sin_addr.s_addr = inet_addr("74.125.235.20");
  • 13.
    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
  • 14.
  • 15.
    bind() • int bind(intsid , 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
  • 16.
  • 17.
    listen() • int listen(intsid , 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 );
  • 18.
    connect() • int connect(intsid , 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
  • 19.
  • 20.
    accept() • int accept(intsid , 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()
  • 22.
    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)
  • 23.
    Getting IP ofa host name/domain • struct hostent * gethostbyname()
  • 25.
    Functions for UDPcommunication • 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
  • 26.
    UDP Receiver sd=socket(AF_INET, SOCK_DGRAM,0); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(SERVER_PORT); rc = bind (sd,(struct sockaddr *)&servAddr, sizeof(servAddr)); n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr , sizeof(cliAddr)
  • 27.
    UDP sender sd =socket(AF_INET,SOCK_DGRAM,0); remoteServAddr.sin_family = AF_INET; remoteServAddr.sin_addr.s_addr = htonl(SERVER_IP); remoteServAddr.sin_port = htons(SERVER_PORT); rc = sendto(sd, msg, strlen(msg)+1, 0, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr));
  • 28.
    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))
  • 29.
    Multicasting – FunctionArguments • 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)
  • 30.
    Multicasting – FunctionArguments • 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
  • 31.
    Options Value forMulticasting • 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));
  • 32.
    Options Value forMulticasting • 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));
  • 33.
    Multicast Receiver sd =socket(AF_INET,SOCK_DGRAM,0); servAddr.sin_family=AF_INET; servAddr.sin_addr.s_addr=htonl(INADDR_ANY); servAddr.sin_port=htons(SERVER_PORT); rc = bind(sd,(struct sockaddr *) &servAddr, sizeof(servAddr)) /* join multicast group */ mreq.imr_multiaddr.s_addr=mcastAddr.s_addr; mreq.imr_interface.s_addr=htonl(INADDR_ANY); rc = setsockopt(sd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(void *) &mreq, sizeof(mreq)); n = recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr *) &cliAddr,&cliLen);
  • 34.
    Multicast Sender //simple UDPsender sd = socket(AF_INET,SOCK_DGRAM,0); setsockopt(sd,IPPROTO_IP,IP_MULTICAST_TTL, &ttl,sizeof(ttl) servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(SERVER_IP); servAddr.sin_port = htons(SERVER_PORT); rc = sendto(sd,argv[i],strlen(argv[i])+1,0, (struct sockaddr *) &servAddr, sizeof(servAddr));