PacketWriter.cpp
author Markus Bröker <mbroeker@largo.homelinux.org>
Tue, 02 Feb 2010 13:26:30 +0100
changeset 8 6097dc1fe2cf
parent 4 fdf04a93faeb
permissions -rw-r--r--
perror is declared in cstdio
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     1
/*
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     2
 *  $Id: PacketWriter.cpp 54 2008-01-10 00:24:52Z mbroeker $
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     3
 * $URL: http://localhost/svn/cpp/qMonitor/trunk/PacketWriter.cpp $
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     4
 */
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     5
8
6097dc1fe2cf perror is declared in cstdio
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 4
diff changeset
     6
#include <cstdio>
6097dc1fe2cf perror is declared in cstdio
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 4
diff changeset
     7
#include <cstdlib>
0
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     8
#include <PacketWriter.h>
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     9
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
extern "C" {
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    11
#include <sys/socket.h>
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    12
#include <netinet/in.h>
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    13
#include <arpa/inet.h>
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    14
#include <netdb.h>
4
fdf04a93faeb Codebase indented by me and gnu-indent
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 3
diff changeset
    15
};
fdf04a93faeb Codebase indented by me and gnu-indent
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 3
diff changeset
    16
fdf04a93faeb Codebase indented by me and gnu-indent
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 3
diff changeset
    17
PacketWriter::PacketWriter (char *hostname, int port)
0
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    18
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    19
    unsigned int size;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    20
    unsigned int fd;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    21
    struct hostent *host = gethostbyname (hostname);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    22
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    23
    if (host == NULL) {
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    24
        perror ("GETHOSTBYNAME");
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    25
        exit (0);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    26
    }
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    27
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    28
    ca.sin_family = PF_INET;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    29
    ca.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    30
    ca.sin_port = htons (port);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    31
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    32
    size = sizeof (ca);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    33
    fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    34
    p_socket = connect (fd, (sockaddr *) & ca, size);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    35
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    36
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    37
PacketWriter::~PacketWriter ()
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    38
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    39
    close (p_socket);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    40
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    41
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    42
void PacketWriter::write (char s[], int len)
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    43
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    44
    int c;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    45
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    46
    c =::write (p_socket, s, len);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    47
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    48
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    49
bool PacketWriter::ready ()
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    50
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    51
    return true;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    52
}