TCPSocket: replaced read/write with recv/send to ignore SIGPIPE during sending; ebusctl: Dealing when receiving data corrected to detect the end of the message with double newline sign.

This commit is contained in:
Roland Jax
2015-01-02 18:57:30 +01:00
parent a167c4565e
commit c490dcd3da
2 changed files with 17 additions and 9 deletions
+3 -2
View File
@@ -21,6 +21,7 @@
#define LIBUTILS_TCPSOCKET_H_
#include <unistd.h>
#include <sys/socket.h>
#include <string>
/** \file tcpsocket.h */
@@ -51,7 +52,7 @@ public:
* @param len number of bytes to send.
* @return number of written bytes or -1 if an error has occured.
*/
ssize_t send(const char* buffer, size_t len) { return write(m_sfd, buffer, len); }
ssize_t send(const char* buffer, size_t len) { return ::send(m_sfd, buffer, len, MSG_NOSIGNAL); }
/**
* @brief read bytes from opened file descriptor.
@@ -59,7 +60,7 @@ public:
* @param len size of the receive buffer.
* @return number of read bytes or -1 if an error has occured.
*/
ssize_t recv(char* buffer, size_t len) { return read(m_sfd, buffer, len); }
ssize_t recv(char* buffer, size_t len) { return ::recv(m_sfd, buffer, len, 0); }
/**
* @brief returns the tcp port.
+14 -7
View File
@@ -23,6 +23,7 @@
#include "appl.h"
#include "tcpsocket.h"
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <sstream>
@@ -74,18 +75,24 @@ bool connect(const char* host, int port, bool once)
if (strncasecmp(message.c_str(), "QUIT", 4) != 0 && strncasecmp(message.c_str(), "STOP", 4) != 0) {
char data[1024];
size_t datalen;
ssize_t datalen = 0;
do {
memset(data, 0, sizeof(data));
datalen = socket->recv(data, sizeof(data)-1);
if (data[datalen-1] != '\n')
if (datalen > 0) {
data[datalen] = '\0';
cout << data;
}
} while (data[datalen-1] != '\n');
memset(data, 0, sizeof(data));
datalen = socket->recv(data, sizeof(data));
if (datalen < 0) {
perror("send");
break;
}
} while (data[datalen-2] != '\n' || data[datalen-1] != '\n');
data[datalen] = '\0';
cout << data;
}
else