PacketWriter.cpp
author Markus Bröker <mbroeker@largo.homelinux.org>
Tue, 02 Feb 2010 13:20:45 +0100
changeset 7 f23b595e590e
parent 4 fdf04a93faeb
child 8 6097dc1fe2cf
permissions -rw-r--r--
Resize changes on newer qt versions

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