PacketWriter.cpp
changeset 0 826dd5531eb0
child 3 724e07a9f155
new file mode 100644
--- /dev/null
+++ b/PacketWriter.cpp
@@ -0,0 +1,48 @@
+/*
+ *  $Id: PacketWriter.cpp 54 2008-01-10 00:24:52Z mbroeker $
+ * $URL: http://localhost/svn/cpp/qMonitor/trunk/PacketWriter.cpp $
+ */
+
+#include <PacketWriter.h>
+
+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;
+}