ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
StreamSocketServer.cpp
Go to the documentation of this file.
1 /*
2  * StreamSocketServer.cpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdexcept>
22 #include <unistd.h>
23 #include "StreamSocketServer.hpp"
24 #include "Logger.hpp"
25 
26 
27 namespace onposix {
28 
29 /**
30  * \brief Constructor for local connection-oriented sockets.
31  *
32  * This constructor creates a connection-oriented AF_UNIX socket.
33  * It calls socket()+bind()+listen().
34  * @param name Name of the local socket on the filesystem
35  * @exception runtime_error in case of error in socket(), bind() or listen()
36  */
38  int maxPendingConnections)
39 {
40  // socket()
41  fd_ = socket(AF_UNIX, SOCK_STREAM, 0);
42  if (fd_ < 0) {
43  ERROR("Creating client socket");
44  throw std::runtime_error ("Socket error");
45  }
46 
47  // bind()
48  struct sockaddr_un serv_addr;
49  bzero((char *) &serv_addr, sizeof(serv_addr));
50  serv_addr.sun_family = AF_UNIX;
51  strncpy(serv_addr.sun_path, name.c_str(),
52  sizeof(serv_addr.sun_path) - 1);
53  if (bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
54  ::close(fd_);
55  ERROR("bind()");
56  throw std::runtime_error ("Bind error");
57  }
58 
59  // listen()
60  if (listen(fd_, maxPendingConnections) < 0) {
61  ::close(fd_);
62  ERROR("listen()");
63  throw std::runtime_error ("Listen error");
64  }
65 }
66 
67 /**
68  * \brief Constructor for TCP connection-oriented sockets.
69  *
70  * This constructor creates a connection-oriented AF_INET socket.
71  * It calls socket()+bind()+listen().
72  * If the protocol is a stream, it also calls listen().
73  * @param port Port of the socket
74  * @exception runtime_error in case of error in socket(), bind() or listen()
75  *
76  */
77 StreamSocketServer::StreamSocketServer(const uint16_t port, int maxPendingConnections)
78 {
79  // socket()
80  fd_ = socket(AF_INET, SOCK_STREAM, 0);
81  if (fd_ < 0) {
82  ERROR("Creating client socket");
83  throw std::runtime_error ("Socket error");
84  }
85 
86  // bind()
87  struct sockaddr_in serv_addr;
88  bzero((char *) &serv_addr, sizeof(serv_addr));
89  serv_addr.sin_family = AF_INET;
90  serv_addr.sin_port = htons(port);
91  serv_addr.sin_addr.s_addr = INADDR_ANY;
92  if (bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
93  ::close(fd_);
94  ERROR("bind()");
95  throw std::runtime_error ("Bind error");
96  }
97 
98  // listen()
99  if (listen(fd_, maxPendingConnections) < 0) {
100  ::close(fd_);
101  ERROR("listen()");
102  throw std::runtime_error ("Listen error");
103  }
104 }
105 
106 } /* onposix */