distSOCKET/udpserver.c

///////////////////////////////////////////////////////////////////////////////
// Filename: udpserver.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: show how to use UDP (this file: udp server) (socket version)
//          uses udpclient(.c) as UDP client
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/20 08:54:53 muellerg: created
// 96/02/21 05:34:44 muellerg: documentation cleanup
// 96/03/19 04:42:44 muellerg: updated, functionality added
///////////////////////////////////////////////////////////////////////////////


// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */



// System headers /////////////////////////////////////////// System headers //

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#ifndef sun
#include <strings.h>
#endif

// Local headers ///////////////////////////////////////////// Local headers //

#include "../common.h"


// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //

/*
 * read and return all incoming data upto MAXLINE bytes (a echo server)
 *
 * in: sockfd   : fd to use
 *     pcli_addr: ptr to appropriate sockaddr_XX structure
 *     maxclilen: sizeof(*pcli_addr)
 */

void do_it(int sockfd, struct sockaddr *pcli_addr, int maxclilen)
{
    int             n, clilen;

    char *mesg;
    mesg = new char[MAXLINE];

    while(true)
    {
        clilen = maxclilen;
        n = recvfrom(sockfd, mesg, MAXLINE, 0, pcli_addr, &clilen);
        if (n < 0)
            error.system("recvfrom error");

        cout << "UDP server: got string \"" << mesg << "\", sending it back"
             << endl;

        // send received data back

        if (sendto(sockfd, mesg, n, 0, pcli_addr, clilen) != n)
            error.system("sendto error");
    }
}




// Main /////////////////////////////////////////////////////////////// Main //

/*
 * Example of server using the UDP protocol.
 *
 * paramteters:
 *
 *   argv[1]: port number to bind() & listen() to 
 */

int
main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    

    if(argc!=2)
    {
        cerr << "Usage: " << argv[0] << " port" << endl;
        exit(EXIT_FAILURE);
    }

    int sockfd;
    struct sockaddr_in  serv_addr, cli_addr;
    int portnumber=-1;


    // get port number

    portnumber = atoi(argv[1]);
    if(portnumber <1)
    {
        cerr << "illegal port number" << endl;
        exit(EXIT_FAILURE);
    }


    // create a UDP socket (an Internet datagram socket)

    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        error.system("server: can't create datagram socket");


    // bind local address so that the client can send to us.

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family      = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port        = htons(portnumber);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        error.system("server: can't bind local address");

    // do something...

    do_it(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr));

    // NOTREACHED 

    return(EXIT_SUCCESS);
}