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
#ifndef __TCP_SOCKET_H__
30
#define __TCP_SOCKET_H__
34
#include <netinet/in.h> // for IPPROTO_TCP, sockadd_in
37
#define SERVER_ADDR "localhost"
40
#define SERVER_PORT 8888
49
* Construct generic TCPSocket
50
* XXX: Don't use, use the constructor with explicit port specification instead
52
//TCPSocketServer() throw(std::runtime_error);
55
* Construct TCPSocket that binds to the given local port
57
* - localPort: port to bind to
59
TCPSocketServer(unsigned short localPort) throw(std::runtime_error);
66
/* Handle incoming connections */
68
* Listen for an incoming connection.
69
* This call blocks until a connection with a remote peer has been established.
71
* - remoteAddr: (OUT) contains address of peer
72
* - remotePort: (OUT) contains port of peer
76
void accept(std::string &remoteAddr, unsigned short &remotePort) throw(std::runtime_error);
79
/* Communication over socket */
81
* Receive data from remote peer.
83
* - buffer: buffer to receive data
84
* - bufferLen: maximum number of bytes to receive
86
* number of bytes received, 0 means connection closed by peer
88
ssize_t recv(void *buffer, const size_t &bufferLen) throw (std::runtime_error);
91
* Send data to remote peer.
93
* - buffer: buffer to send
94
* - bufferLen: number of bytes in buffer
96
* number of bytes actually written
98
ssize_t send(const void *buffer, const int &bufferLen) throw (std::runtime_error);
101
* Sends the given string over the TCP connection.
102
* This is a convenience method which calls the previous method with the correct
105
* - message: message to send
107
* number of bytes actually written
109
ssize_t send(const std::string &message) throw (std::runtime_error);
112
int recv_data(unsigned char *data, int size);
113
unsigned int recv_uint32();
117
// don't allow value semantics on this object
118
TCPSocketServer(const TCPSocketServer &sock);
119
void operator=(const TCPSocketServer &sock);
121
int sockDesc; // listening socket descriptor
122
sockaddr_in sockAddr; // structure keeping IP and port of peer
124
int connSockDesc; // connected socket descriptor
125
sockaddr_in connSockAddr;
128
#endif // __TCP_SOCKET_H__