9 static const char rcsid[] __attribute__((used)) =
"$Id: TCPSocket.cpp 1653 2016-02-28 19:54:59Z sella $";
11 #include "TCPSocket.h"
13 #include <sys/types.h>
14 #include <sys/socket.h>
17 using namespace sella::net;
19 TCPSocket::TCPSocket(
int family)
throw (SocketException) :
Socket(family, SOCK_STREAM, IPPROTO_TCP) {
22 TCPSocket::TCPSocket(
int family,
int type,
int protocol)
throw (SocketException) :
Socket(family, type, protocol) {
25 TCPSocket::TCPSocket(
int family,
const IPAddressPort &addressport)
throw (SocketException) :
Socket(family, SOCK_STREAM, IPPROTO_TCP) {
29 TCPSocket::TCPSocket(
int family,
const IPAddress &address,
const Port &port)
throw (SocketException) :
Socket(family, SOCK_STREAM, IPPROTO_TCP) {
30 connect(address, port);
33 TCPSocket::TCPSocket(
int family,
const std::string &address,
unsigned short port)
throw (SocketException) :
Socket(family, SOCK_STREAM, IPPROTO_TCP) {
34 connect(address, port);
37 TCPSocket::TCPSocket(
const IPAddressPort &addressport)
throw (SocketException) :
Socket(addressport.getFamily(), SOCK_STREAM, IPPROTO_TCP) {
41 TCPSocket::TCPSocket(
const IPAddress &address,
const Port &port)
throw (SocketException) :
Socket(address.getFamily(), SOCK_STREAM, IPPROTO_TCP) {
42 connect(address, port);
45 TCPSocket::TCPSocket(
int s,
bool unused)
throw (SocketException) :
Socket(s) {
49 TCPSocket::~TCPSocket() {
52 void TCPSocket::listen(
int backlog)
throw (SocketException) {
53 if (::listen(s, backlog) < 0) {
54 THROW2(SocketException,
"listen()");
58 TCPSocket::shared TCPSocket::accept(
void) throw (SocketException) {
61 if ((fd = ::accept(s, NULL, NULL)) < 0) {
62 THROW2(SocketException,
"accept()");
65 const TCPSocket::shared socket(
new TCPSocket(fd,
true));
70 const TCPSocket::shared TCPSocket::shared_ptr(
int family)
throw (SocketException) {
71 return std::make_shared<TCPSocket>(family);
74 const TCPSocket::shared TCPSocket::shared_ptr(
int family,
const IPAddressPort &addressport)
throw (SocketException) {
75 return std::make_shared<TCPSocket>(family, addressport);
78 const TCPSocket::shared TCPSocket::shared_ptr(
int family,
const IPAddress &address,
const Port &port)
throw (SocketException) {
79 return std::make_shared<TCPSocket>(family, address, port);
82 const TCPSocket::shared TCPSocket::shared_ptr(
int family,
const std::string &address,
unsigned short port)
throw (SocketException) {
83 return std::make_shared<TCPSocket>(family, address, port);
86 const TCPSocket::shared TCPSocket::shared_ptr(
const IPAddressPort &addressport)
throw (SocketException) {
87 return std::make_shared<TCPSocket>(addressport);
90 const TCPSocket::shared TCPSocket::shared_ptr(
const IPAddress &address,
const Port &port)
throw (SocketException) {
91 return std::make_shared<TCPSocket>(address, port);