libsocket
inetclientstream.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <iostream>
3 #include <string>
4 
5 #include <stdio.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 /*
10  The committers of the libsocket project, all rights reserved
11  (c) 2012, dermesser <lbo@spheniscida.de>
12 
13  Redistribution and use in source and binary forms, with or without
14  modification, are permitted provided that the following conditions are met:
15 
16  1. Redistributions of source code must retain the above copyright notice,
17  this list of conditions and the following disclaimer.
18  2. Redistributions in binary form must reproduce the above copyright notice,
19  this list of conditions and the following disclaimer in the documentation
20  and/or other materials provided with the distribution.
21 
22  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
23  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
26  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 */
34 
47 #include <libinetsocket.h>
48 #include <exception.hpp>
49 #include <inetclientstream.hpp>
50 
51 namespace libsocket {
52 using std::string;
53 
56 
67 inet_stream::inet_stream(const char* dsthost, const char* dstport,
68  int proto_osi3, int flags) {
69  connect(dsthost, dstport, proto_osi3, flags);
70 }
71 
82 inet_stream::inet_stream(const string& dsthost, const string& dstport,
83  int proto_osi3, int flags) {
84  connect(dsthost.c_str(), dstport.c_str(), proto_osi3, flags);
85 }
86 
98 void inet_stream::connect(const char* dsthost, const char* dstport,
99  int proto_osi3, int flags) {
100  if (sfd != -1)
101  throw socket_exception(__FILE__, __LINE__,
102  "inet_stream::connect() - Already connected!",
103  false);
104 
105  sfd = create_inet_stream_socket(dsthost, dstport, proto_osi3, flags);
106 
107  if (sfd < 0)
108  throw socket_exception(
109  __FILE__, __LINE__,
110  "inet_stream::connect() - Could not create socket");
111 
112  host = dsthost;
113  port = dstport;
114  proto = proto_osi3;
115 
116  // New file descriptor, therefore reset shutdown flags
117  shut_rd = false;
118  shut_wr = false;
119 }
120 
132 void inet_stream::connect(const string& dsthost, const string& dstport,
133  int proto_osi3, int flags) {
134  connect(dsthost.c_str(), dstport.c_str(), proto_osi3, flags);
135 }
136 } // namespace libsocket
Contains libsocket elements.
Definition: dgramclient.hpp:41
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
void connect(const char *dsthost, const char *dstport, int proto_osi3, int flags=0)
Set up socket if not already done.
int create_inet_stream_socket(const char *host, const char *service, char proto_osi3, int flags)
Create and connect a new TCP/IP socket.
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
int proto
Which internet protocol version we're using.
Definition: inetbase.hpp:59
inet_stream(void)
Void constructor; call connect() before using the socket!
string host
The address we're bound or connected to.
Definition: inetbase.hpp:55
Contains all functions available in the C libsocket.