2
* Copyright (c) 2012 Intel Corporation. All Rights Reserved.
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the
6
* "Software"), to deal in the Software without restriction, including
7
* without limitation the rights to use, copy, modify, merge, publish,
8
* distribute, sub license, and/or sell copies of the Software, and to
9
* permit persons to whom the Software is furnished to do so, subject to
10
* the following conditions:
12
* The above copyright notice and this permission notice (including the
13
* next paragraph) shall be included in all copies or substantial portions
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22
* USE OR OTHER DEALINGS IN THE SOFTWARE.
26
* C++ wrapper around an TCP socket
29
#include "TCPSocketClient.h"
31
#include <sys/types.h>
32
#include <sys/socket.h>
33
#include <netinet/in.h>
34
#include <arpa/inet.h>
42
TCPSocketClient::TCPSocketClient(const std::string &remoteAddr, const unsigned short &remotePort) throw(std::runtime_error) :
45
::memset(&sockAddr, 0, sizeof(sockAddr));
46
sockAddr.sin_family = AF_INET;
48
if ((hp = gethostbyname(remoteAddr.c_str())) == NULL) {
49
throw std::runtime_error("Unknown host " + remoteAddr);
51
bcopy(hp->h_addr, &sockAddr.sin_addr, hp->h_length);
52
sockAddr.sin_port = htons(remotePort);
53
if ((sockDesc = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
54
throw std::runtime_error("Socket creation failed (socket())");
57
if (connect(sockDesc, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) < 0) {
58
throw std::runtime_error("Error connecting to remote host (connect())");
63
TCPSocketClient::~TCPSocketClient()
70
/* Communication over socket */
72
ssize_t TCPSocketClient::recv(void *buffer, const size_t &bufferLen) throw (std::runtime_error)
75
throw std::runtime_error("socket is not connected (recv())");
77
int rval = ::read(sockDesc, buffer, bufferLen);
79
// EOF (connection closed by remote host) or error:
80
// reset state, so a new accept() call will succeed
82
::memset(&sockAddr, 0, sizeof(sockAddr));
85
throw std::runtime_error("Error reading from socket (read())");
91
ssize_t TCPSocketClient::send(const void *buffer, const int &bufferLen) throw (std::runtime_error)
94
throw std::runtime_error("socket is not connected (send())");
97
int rval = ::write(sockDesc, buffer, bufferLen);
100
// EOF (connection closed by remote host) or error:
101
// reset state, so a new accept() call will succeed
103
::memset(&sockAddr, 0, sizeof(sockAddr));
107
throw std::runtime_error("Error reading from socket (read())");
113
ssize_t TCPSocketClient::send(const string &message) throw (std::runtime_error)
115
return send(message.c_str(), message.length());
119
ssize_t TCPSocketClient::send(unsigned int val) throw (std::runtime_error)
121
return send(&val, 4);