/**
*
*  @File : exo_01c.cxx
*
*  @Authors : D. Mathieu
*             M. Laporte
*
*  @Date : 19/12/2000
*
*  @Version : V1.0
*
*  @Synopsis :
*
**/
#include <iostream>
#include <string>
#include <exception>
#include <csignal>        // SIGURG
#include <climits>        // MAX_INPUT
#include <cerrno>         // EINTR

#include <unistd.h>       // getpid(), atoi()
#include <fcntl.h>        // F_SETOWN
#include <netinet/in.h>   // htons()
#include <sys/socket.h>   // MSG_OOB
#include <sys/types.h>    // size_t

#include "CExc.h"
#include "CExcFctSyst.h"
#include "nsSysteme.h"
#include "nsNet.h"

using namespace nsSysteme;
using namespace nsNet;

namespace
{
    int sd;

    void DeroutUrg  (int)
    {
        char Msg;
        Recv (sd, &Msg, 1, MSG_OOB);
        cout << "Msg urgent : " << Msg << endl;

    } // DeroutUrg()

} // namespace anonyme

int std::ppal (int argc, char * argv []) throw (exception)
{
    if (3 != argc)
        throw nsBAO::CExc (string ("Usage : ") + argv [0] +
                           " <Machine> <Port>");

    sd = ClientSockInStream (AddrIP (argv [1]), ::htons (::atoi (argv [2])));

    char Tampon [MAX_INPUT];

    Signal (SIGURG, DeroutUrg);

    Fcntl (sd, F_SETOWN, ::getpid());

    for (;;)
    {
        try
        {
            size_t Lg = Recv (sd, Tampon, MAX_INPUT - 1);
            if (! Lg) break;
            Tampon [Lg] = '\0';
            cout << "Msg normal : " << Tampon << endl;
         }
         catch (const CExcFctSystFile & E)
         {
             if (EINTR != E.GetErrno ()) throw;
         }
    }
    return 0;

} // ppal()