PacketWriter.cpp
author Markus Bröker <mbroeker@largo.homelinux.org>
Fri, 10 Apr 2009 21:21:11 +0200
changeset 5 70980306fd61
parent 4 fdf04a93faeb
child 8 6097dc1fe2cf
permissions -rw-r--r--
lintian checks minimized the lintian warnings, QoS

/*
 *  $Id: PacketWriter.cpp 54 2008-01-10 00:24:52Z mbroeker $
 * $URL: http://localhost/svn/cpp/qMonitor/trunk/PacketWriter.cpp $
 */

#include <PacketWriter.h>
#include <cstdlib>

extern "C" {
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
};

PacketWriter::PacketWriter (char *hostname, int port)
{
    unsigned int size;
    unsigned int fd;
    struct hostent *host = gethostbyname (hostname);

    if (host == NULL) {
        perror ("GETHOSTBYNAME");
        exit (0);
    }

    ca.sin_family = PF_INET;
    ca.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
    ca.sin_port = htons (port);

    size = sizeof (ca);
    fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
    p_socket = connect (fd, (sockaddr *) & ca, size);
}

PacketWriter::~PacketWriter ()
{
    close (p_socket);
}

void PacketWriter::write (char s[], int len)
{
    int c;

    c =::write (p_socket, s, len);
}

bool PacketWriter::ready ()
{
    return true;
}