Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Examples of connectionless communications

&pagelevel(5)&pagelevel

The following two program examples illustrate how datagrams are received and sent with connectionless communications:

Example 1: Receiving datagrams

#include <sys.types.h>
#include <sys.socket.h>
#include <netinet.in.h>
#include <stdio.h>
#define TESTPORT 2222
 
/*
 * The <netinet.in.h> header file declares sockaddr_in as follows:
 *
 * struct sockaddr_in {
 *    short   sin_family;
 *    u_short sin_port;
 *    struct in_addr sin_addr;
 *    char    sin_zero[8];
 * };
 *
 * This program creates a socket, assigns it a name and then reads from
 * the socket.
 */
 
main()
{
      int sock, length, peerlen;
      struct sockaddr_in name, peer;
      char buf[1024];
  
      /* Create the socket to be read from. */
      sock = socket(AF_INET, SOCK_DGRAM, 0);
      if (sock < 0) {
              perror("opening datagram socket");
              exit(1);
      }
 
      /* Assign the socket a name using wildcards */
      name.sin_family = AF_INET;
      name.sin_addr.s_addr = INADDR_ANY;
      name.sin_port = htons(TESTPORT);
 
      if (bind(sock, &name, sizeof name ) < 0) {
              perror("binding datagram socket");
              exit(1);
      }
 
       /* Read from socket. */
       peerlen=sizeof peer;
       if (recvfrom(sock, buf, 1024, &peer, &peerlen) < 0)
               perror("receiving datagram packet");
       else 
       printf("-->%s\n", buf);
       soc_close(sock);
       exit(0);
}

Example 2: Sending datagrams

#include <sys.types.h>
#include <sys.socket.h>
#include <netinet.in.h>
#include <netdb.h> 
#include <stdio.h>
#define DATA "The sea is calm, the tide is full . . ."
 
/*
 * This program sends a datagram to a receiver whose name is passed via 
 * the arguments in the command line.
 */
 
main(argc, argv)
      int argc;
      char *argv[];
{
      int sock;
      struct sockaddr_in name;
      struct hostent *hp; 
      /* Create socket on which data is to be sent */
      sock = socket(AF_INET, SOCK_DGRAM, 0);
      if (sock < 0) {
              perror("opening datagram socket");
              exit(1);
      }
 
      /*
       * Construct the name of the socket on which data is to be sent 
       * without using wildcards. gethostbyname()  returns a structure 
       * containing the Internet address of the specified host. The 
       * port number is taken over from the command line. 
       */
 
      hp = gethostbyname(argv[1]);
      if (hp == 0) {
              fprintf(stderr, "%s: unknown host\n", argv[1]);
              exit(2);
      }
      memcpy( (char *)&name.sin_addr, (char *)hp->h_addr,
        hp->h_length);
      name.sin_family = AF_INET;
      name.sin_port = htons(atoi(argv[2]));
 
      /* Send message. */
 
      if (sendto(sock, DATA, sizeof DATA , 0, &name, sizeof name) < 0)
              perror("sending datagram message");
      soc_close(sock);
      exit(0);
}