PacketWriter.cpp
author Markus Bröker <mbroeker@largo.homelinux.org>
Sat, 03 Jan 2009 01:28:28 +0100
changeset 4 fdf04a93faeb
parent 3 724e07a9f155
child 8 6097dc1fe2cf
permissions -rw-r--r--
Codebase indented by me and gnu-indent

/*
 *  $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;
}