The following socket interface functions are used by the client in these program examples:
socket(): create socket
gethostbyname() / getipnodebyname(): get the host name entry
sendto(): send a message to a socket
soc_close(): close socket
Example: Connectionless client for AF_INET
#include <stdio.h> #include <sys.types.h> #include <sys.socket.h> #include <ioctl.h> #include <signal.h> #include <netinet.in.h> #include <netdb.h> #define DATA " The sea is calm, the tide is full ..." #define TESTPORT 2222 /* * This program sends a datagram to a receiver whose name is passed * as an argument in the command line. */ main(argc,argv) int argc; char *argv[]; { int sock; struct sockaddr_in to; struct hostent *hp, *gethostbyname(); /* Create the socket to be sent on. */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0 ) { perror("Socket datagram"); exit(1); } /* Construct the name of the socket to be sent on, without using * wildcards. gethostbyname returns a structure which contains the * network address of the specified host. * The port number is taken from the TESTPORT constant. */ hp =gethostbyname(argv[1]); if (hp == 0) { fprintf(stderr, "%s:unknown host\n", argv[1]); exit(1); } memcpy((char *)&to.sin_addr, (char *)hp->h_addr,hp->h_length); to.sin_family = AF_INET; to.sin_port = htons(TESTPORT); /* Send message. */ if (sendto(sock, DATA, sizeof DATA, 0, &to, sizeof to) < 0) { perror("Sending datagram message"); exit(1); } soc_close(sock); }
Example: Connectionless client for AF_INET6
#include <stdio.h> #include <sys.types.h> #include <sys.socket.h> #include <ioctl.h> #include <signal.h> #include <netinet.in.h> #include <netdb.h> #define DATA " The sea is calm, the tide is full ..." #define TESTPORT 2222 /* * This program sends a datagram to a receiver whose name is passed * as an argument in the command line. */ main(argc,argv) int argc; char *argv[]; { int sock; int error_num; struct sockaddr_in6 to; struct hostent *hp; /* Create the socket to be sent on. */ sock = socket(AF_INET6, SOCK_DGRAM, 0); if (sock < 0 ) { perror("Socket datagram"); exit(1); } /* Construct the name of the socket to be sent on, without using * wildcards. gethostbyname returns a structure which contains the * network address of the specified host. * The port number is taken from the TESTPORT constant. */ hp =getipnodebyname(argv[1], AF_INET6, 0, &error_num); if ((hp = = 0) || (error_num != NETDB_SUCCESS)) { fprintf(stderr, "%s:unknown host\n", argv[1]); exit(1); } memcpy((char *)&to.sin6_addr, (char *)hp->h_addr,hp->h_length); to.sin6_family = AF_INET6; to.sin6_port = htons(TESTPORT); /* Release the dynamic memory of hostent */ freehostent(hp); /* Send message. */ if (sendto(sock, DATA, sizeof DATA, 0, &to, sizeof to) < 0) { perror("Sending datagram message"); exit(1); } soc_close(sock); }
The client creates a communications endpoint (socket) and corresponding descriptor with socket().The following steps are executed in the program examples for AF_INET and AF_INET6:
The client queries the address of the host with gethostbyname() (for AF_INET); the host name is passed as a parameter.
The client queries the IPv6 address of the host name passed as a parameter with getipnodebyname(). This new function could also be used for the AF_INET example. The address structure is then initialized.The client sends a datagram with sendto(), which returns the number of transferred characters.
The client closes the socket with soc_close().