libsocket
inetserverdgram.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <string>
3 
4 /*
5  The committers of the libsocket project, all rights reserved
6  (c) 2012, dermesser <lbo@spheniscida.de>
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  1. Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
18  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
40 #include <libinetsocket.h>
41 #include <exception.hpp>
42 #include <inetserverdgram.hpp>
43 
44 #include <fcntl.h>
45 #ifndef SOCK_NONBLOCK
46 #define SOCK_NONBLOCK O_NONBLOCK
47 #endif
48 
49 namespace libsocket {
50 using std::string;
51 
60 inet_dgram_server::inet_dgram_server(const char* host, const char* port,
61  int proto_osi3, int flags) {
62  setup(host, port, proto_osi3, flags);
63 }
64 
73 inet_dgram_server::inet_dgram_server(const string& host, const string& port,
74  int proto_osi3, int flags) {
75  setup(host, port, proto_osi3, flags);
76 }
77 
86 void inet_dgram_server::setup(const char* bhost, const char* bport,
87  int proto_osi3, int flags) {
88  // No separate call to get_address_family()
89 
90  if (-1 == (sfd = create_inet_server_socket(bhost, bport, LIBSOCKET_UDP,
91  proto_osi3, flags)))
92  throw socket_exception(__FILE__, __LINE__,
93  "inet_dgram_server::inet_dgram_server() - could "
94  "not create server socket!");
95 
96  host = string(bhost);
97  port = string(bport);
98  is_nonblocking = flags & SOCK_NONBLOCK;
99 }
100 
109 void inet_dgram_server::setup(const string& bhost, const string& bport,
110  int proto_osi3, int flags) {
111  setup(bhost.c_str(), bport.c_str(), proto_osi3, flags);
112 }
113 } // namespace libsocket
Contains libsocket elements.
Definition: dgramclient.hpp:41
inet_dgram_server(const char *host, const char *port, int proto_osi3, int flags=0)
Create datagram socket and bind it.
int create_inet_server_socket(const char *bind_addr, const char *bind_port, char proto_osi4, char proto_osi3, int flags)
Create a TCP or UDP server socket.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
string port
The port we're bound or connected to.
Definition: inetbase.hpp:57
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74
void setup(const char *host, const char *port, int proto_osi3, int flags=0)
Set up socket. NOT FOR EXTERNAL USE
string host
The address we're bound or connected to.
Definition: inetbase.hpp:55
Contains all functions available in the C libsocket.