performance/udpsserver.c

///////////////////////////////////////////////////////////////////////////////
// Filename: udpsserver.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: measure performance of UDP (this file: UDP server) (socket version)
//          needs udpsclient(.c) as UDP client
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/03/01 17:57:19 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


// 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"
#include "udps.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 discard) all incoming data, send a byte as reply back
 * 
 * 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[MAX_BUFFER];

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

        // send a byte as acknowledgment back:

        if (sendto(sockfd, mesg, 1, 0, pcli_addr, clilen) != 1)
            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));

    return(EXIT_SUCCESS);
}