OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 Intel Corporation. All Rights Reserved. |
| 3 * |
| 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: |
| 11 * |
| 12 * The above copyright notice and this permission notice (including the |
| 13 * next paragraph) shall be included in all copies or substantial portions |
| 14 * of the Software. |
| 15 * |
| 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. |
| 23 */ |
| 24 |
| 25 /* |
| 26 * C++ wrapper around an TCP socket |
| 27 */ |
| 28 |
| 29 #ifndef __TCP_SOCKET_H__ |
| 30 #define __TCP_SOCKET_H__ |
| 31 |
| 32 #include <stdexcept> |
| 33 #include <netinet/in.h> // for IPPROTO_TCP, sockadd_in |
| 34 |
| 35 |
| 36 class TCPSocketClient |
| 37 { |
| 38 public: |
| 39 /* Constructors */ |
| 40 /** |
| 41 * Construct generic TCPSocket |
| 42 * XXX: Don't use, use the constructor with explicit port specification inste
ad |
| 43 **/ |
| 44 //TCPSocketClient() throw(std::runtime_error); |
| 45 |
| 46 /** |
| 47 * Construct TCPSocket that connects to the given remote server. |
| 48 * parameters: |
| 49 * - remoteAddr: address of server to connect to |
| 50 * - remotePort: port of server to connect to |
| 51 **/ |
| 52 TCPSocketClient(const std::string &remoteAddr, const unsigned short &remoteP
ort) throw(std::runtime_error); |
| 53 |
| 54 |
| 55 /* Destructor */ |
| 56 ~TCPSocketClient(); |
| 57 |
| 58 |
| 59 /* Communication over socket */ |
| 60 /** |
| 61 * Receive data from remote peer. |
| 62 * parameters: |
| 63 * - buffer: buffer to receive data |
| 64 * - bufferLen: maximum number of bytes to receive |
| 65 * return value: |
| 66 * number of bytes received, 0 means connection closed by peer |
| 67 **/ |
| 68 ssize_t recv(void *buffer, const size_t &bufferLen) throw (std::runtime_erro
r); |
| 69 |
| 70 /** |
| 71 * Send data to remote peer. |
| 72 * parameters: |
| 73 * - buffer: buffer to send |
| 74 * - bufferLen: number of bytes in buffer |
| 75 * return value: |
| 76 * number of bytes actually written |
| 77 **/ |
| 78 ssize_t send(const void *buffer, const int &bufferLen) throw (std::runtime_e
rror); |
| 79 |
| 80 /** |
| 81 * Sends the given string over the TCP connection. |
| 82 * This is a convenience method which calls the previous method with the corr
ect |
| 83 * length parameter. |
| 84 * parameters: |
| 85 * - message: message to send |
| 86 * return value: |
| 87 * number of bytes actually written |
| 88 **/ |
| 89 ssize_t send(const std::string &message) throw (std::runtime_error); |
| 90 |
| 91 ssize_t send(unsigned int val) throw (std::runtime_error); |
| 92 |
| 93 private: |
| 94 // don't allow value semantics on this object |
| 95 TCPSocketClient(const TCPSocketClient &sock); |
| 96 void operator=(const TCPSocketClient &sock); |
| 97 |
| 98 int sockDesc; // socket descriptor |
| 99 sockaddr_in sockAddr; // structure keeping IP and port of peer |
| 100 }; |
| 101 |
| 102 #endif // __TCP_SOCKET_H__ |
OLD | NEW |