Developing applications over TCP/IP using Internet sockets

Datagrams in the Internet Domain

Unlike the previous examples, which deal with streams sockets, the following two sample programs send and receive data on datagram sockets. These examples are for the Internet domain; for the UNIX domain equivalents, see ``Datagrams in the UNIX domain''.

First, create a server that can accept Internet domain datagrams:

Reading Internet domain datagrams

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

/* * In the included file <netinet/in.h> a sockaddr_in is defined 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 datagram socket, binds a name to it, then reads * from the socket. */ main() { int sock, length; struct sockaddr_in name; char buf[1024];

/* Create socket from which to read. */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); } /* Create name with wildcards. */ name.sin_family = AF_INET; name.sin_addr.s_addr = INADDR_ANY; name.sin_port = 0; if (bind(sock, (struct sockaddr *) &name, sizeof(name))) { perror("binding datagram socket"); exit(1); } /* Find assigned port value and print it out. */ length = sizeof(name); if (getsockname(sock, &name, &length)) { perror("getting socket name"); exit(1); } printf("Socket has port #%d\n", ntohs(name.sin_port)); /* Read from the socket */ if (read(sock, buf, 1024) < 0) perror("receiving datagram packet"); printf("-->%s\n", buf); close(sock); }

Then, create a client that can send datagrams. The following sample code creates a client and sends datagrams to a server like the one created in the previous example. For the UNIX domain equivalent example, see ``Sending UNIX domain datagrams''.

Sending an Internet domain datagram

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#define DATA "The sea is calm tonight, the tide is full . . ."

/* * Here I send a datagram to a receiver whose name I get from the * command line arguments. The form of the command line is: * <programname> <hostname> <portnumber> */

main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_in name; struct hostent *hp, *gethostbyname();

/* Create socket on which to send. */

sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); }

/* * Construct name, with no wildcards, of the socket to send to. * Gethostbyname() returns a structure including the network address * of the specified host. The port number is taken from the command * line. */

hp = gethostbyname(argv[1]); if (hp == 0) { fprintf(stderr, "%s: unknown host\n", argv[1]); exit(2); }

bcopy(hp->h_addr, &name.sin_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"); close(sock); }

Previous topic: Sample client (part 2 of 2)

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003