Add Book to My BookshelfPurchase This Book Online

Chapter 14 - Networking with Sockets

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Putting It All Together
Examples 14-1 and 14-2 are small server and client programs that transfer data between themselves using a virtual circuit. These two programs are identical in operation to the programs in Examples 13-6 and 13-7, except they are implemented using Internet-domain sockets.
Example 14-1:  server
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define PORTNUMBER  12345
int
main(void)
{
    char buf[1024];
    int n, s, ns, len;
    struct sockaddr_in name;
    /*
     * Create the socket.
     */
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }
    /*
     * Create the address of the server.
     */
    memset(&name, 0, sizeof(struct sockaddr_in));
    name.sin_family = AF_INET;
    name.sin_port = htons(PORTNUMBER);
    len = sizeof(struct sockaddr_in);
    /*
     * Use the wildcard address.
     */
    n = INADDR_ANY;
    memcpy(&name.sin_addr, &n, sizeof(long));
    /*
     * Bind the socket to the address.
     */
    if (bind(s, (struct sockaddr *) &name, len) < 0) {
        perror("bind");
        exit(1);
    }
    /*
     * Listen for connections.
     */
    if (listen(s, 5) < 0) {
        perror("listen");
        exit(1);
    }
    /*
     * Accept a connection.
     */
    if ((ns = accept(s, (struct sockaddr *) &name, &len)) < 0) {
        perror("accept");
        exit(1);
    }
    /*
     * Read from the socket until end-of-file and
     * print what we get on the standard output.
     */
    while ((n = recv(ns, buf, sizeof(buf), 0)) > 0)
        write(1, buf, n);
    close(ns);
    close(s);
    exit(0);
}
Example 14-2:  client
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#define PORTNUMBER  12345
int
main(void)
{
    int n, s, len;
    char buf[1024];
    char hostname[64];
    struct hostent *hp;
    struct sockaddr_in name;
    /*
     * Get our local host name.
     */
    if (gethostname(hostname, sizeof(hostname)) < 0) {
        perror("gethostname");
        exit(1);
    }
    /*
     * Look up our host's network address.
     */
    if ((hp = gethostbyname(hostname)) == NULL) {
        fprintf(stderr, "unknown host: %s.\n", hostname);
        exit(1);
    }
    /*
     * Create a socket in the INET
     * domain.
     */
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }
    /*
     * Create the address of the server.
     */
    memset(&name, 0, sizeof(struct sockaddr_in));
    name.sin_family = AF_INET;
    name.sin_port = htons(PORTNUMBER);
    memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length);
    len = sizeof(struct sockaddr_in);
    /*
     * Connect to the server.
     */
    if (connect(s, (struct sockaddr *) &name, len) < 0) {
        perror("connect");
        exit(1);
    }
    /*
     * Read from standard input, and copy the
     * data to the socket.
     */
    while ((n = read(0, buf, sizeof(buf))) > 0) {
        if (send(s, buf, n, 0) < 0) {
            perror("send");
            exit(1);
        }
    }
    close(s);
    exit(0);
}
    % server &
    % client < /etc/motd
    Sun Microsystems Inc.   SunOS 5.3       Generic September 1993
Example 14-3 shows a sample datagram client program that contacts the “daytime” service on every host named on the command line. The “daytime” service is an Internet standard service that returns the local time (to the server) in an ASCII string. It is defined for both TCP and UDP; try modifying the program to use TCP instead.
Example 14-3:  daytime
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#define SERVICENAME "daytime"
int
main(int argc, char **argv)
{
    int n, s, len;
    char buf[1024];
    char *hostname;
    struct hostent *hp;
    struct servent *sp;
    struct sockaddr_in name, from;
    if (argc < 2) {
        fprintf(stderr, "Usage: %s hostname [hostname...]\n", *argv);
        exit(1);
    }
    /*
     * Look up our service.  We want the UDP version.
     */
    if ((sp = getservbyname(SERVICENAME, "udp")) == NULL) {
        fprintf(stderr, "%s/udp: unknown service.\n", SERVICENAME);
        exit(1);
    }
    while (--argc) {
        hostname = *++argv;
        /*
         * Look up the host's network address.
         */
        if ((hp = gethostbyname(hostname)) == NULL) {
            fprintf(stderr, "%s: unknown host.\n", hostname);
            continue;
        }
        /*
         * Create a socket in the INET
         * domain.
         */
        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
            perror("socket");
            exit(1);
        }
        /*
         * Create the address of the server.
         */
        memset(&name, 0, sizeof(struct sockaddr_in));
        name.sin_family = AF_INET;
        name.sin_port = sp->s_port;
        memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length);
        len = sizeof(struct sockaddr_in);
        /*
         * Send a packet to the server.
         */
        memset(buf, 0, sizeof(buf));
        n = sendto(s, buf, sizeof(buf), 0, (struct sockaddr *) &name,
                   sizeof(struct sockaddr_in));
        if (n < 0) {
            perror("sendto");
            exit(1);
        }
        /*
         * Receive a packet back.
         */
        len = sizeof(struct sockaddr_in);
        n = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from, &len);
        if (n < 0) {
            perror("recvfrom");
            exit(1);
        }
        /*
         * Print the packet.
         */
        buf[n] = '\0';
        printf("%s: %s", hostname, buf);
        /*
         * Close the socket.
         */
        close(s);
    }
    exit(0);
}
    % daytime localhost
    localhost: Mon Mar 20 15:50:54 1995

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback