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(4)&pagelevel

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

The example programs are only valid for the communications domain AF_INET. If they are modified according to the information in the sections “Socket addressing” and “Creating a socket”, they are also valid for the AF_INET6 domain.

Example 1: receiving datagrams

This program creates a socket, assigns it a name and then reads from the socket.

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#define TESTPORT 2222
int main(int argc, char **argv)
{
    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, (struct sockaddr *)&name, sizeof name ) < 0) {
        perror("binding datagram socket");
        exit(1);
    }
    /* Find and output the corresponding port number. */
    length = sizeof(name);
    if (getsockname(sock, (struct sockaddr *)&name, &length) < 0) {
        perror("getting socket name");
        exit(1);
    }
    printf("Socket port #%d\n", ntohs(name.sin_port));
    /* Read from the socket. */
    peerlen=sizeof peer;
    if (recvfrom(sock, buf, 1024, (struct sockaddr *)&peer, &peerlen) < 0) {
              perror("receiving datagram packet");
    } else {
        printf("-->%s\n", buf);
    }
    close(sock);
    exit(0);
}

Example 2: sending datagrams

This program sends a datagram to a receiver whose name is passed via the arguments in the command line.

The program call is: programname hostname portnumber

#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 . . ."
int main(int argc, char **argv)
{
    int sock;
    struct sockaddr_in name;
    struct hostent *hp;
    /* Create socket over which data is to be sent */
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
        perror("opening datagram socket");
        exit(1);
    }
    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, (struct sockaddr *)&name, sizeof name) < 0) {
        perror("sending datagram message");
    }
    close(sock);
    exit(0);
}