libsocket 1.5
netsocket.hh
Go to the documentation of this file.
1/*
2** netsocket.hh
3** Login : Julien Lemoine <speedblue@happycoders.org>
4** Started on Mon May 12 22:23:32 2003 Julien Lemoine
5** $Id: netsocket.hh,v 1.2 2004/06/01 21:30:53 speedblue Exp $
6**
7** Copyright (C) 2003,2004 Julien Lemoine
8** This program is free software; you can redistribute it and/or modify
9** it under the terms of the GNU Lesser General Public License as published by
10** the Free Software Foundation; either version 2 of the License, or
11** (at your option) any later version.
12**
13** This program is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** GNU Lesser General Public License for more details.
17**
18** You should have received a copy of the GNU Lesser General Public License
19** along with this program; if not, write to the Free Software
20** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#ifndef NETSOCKET_HH_
24# define NETSOCKET_HH_
25
26#include "socket.hh"
27
28namespace Network
29{
33 class NetSocket : public Socket
34 {
35 public:
37 Socket(kind, version), _port(0)
38 {
39 }
41 Socket(kind, pkind, version), _port(0)
42 {
43 }
44
45 virtual ~NetSocket() {}
46
47 public:
49 virtual void writeto(const std::string& str,
50 const std::string& host, int port);
51
54 std::string read();
56 std::string read(int timeout);
59 std::string read(int& port, std::string& host);
62 std::string read(int& port, std::string& host, int timeout);
65 std::string readn(unsigned int size);
68 std::string readn(int timeout, unsigned int size);
72 std::string readn(int& port, std::string& host, unsigned int size);
76 std::string readn(int& port, std::string& host, int timeout,
77 unsigned int size);
78
79 protected:
81 struct sockaddr_in _get_addr(int port) const;
82#ifdef IPV6_ENABLED
83 struct sockaddr_in6 _get_addr6(int port) const;
84#endif
85
88 struct sockaddr_in _get_addr(const std::string& host,
89 int port) const;
90#ifdef IPV6_ENABLED
91 struct sockaddr_in6 _get_addr6(const std::string& host,
92 int port) const;
93#endif
99 int _bind(int port, const std::string& host);
109 int _bind(int port);
113 int _accept(int port, int server_socket) const;
115 std::string _get_ip(int port, int socket) const;
119 void _connect(int socket, int port,
120 const std::string& host) const;
124 std::string _read_line(int socket);
131 std::string _read_line(int socket, int& port,
132 std::string& host);
139 virtual std::string _read_line_bin(int socket, int& port,
140 std::string& host,
141 unsigned int pkg_size) = 0;
145 virtual std::string _read_line_bin(int socket, unsigned int size) = 0;
146
151 void _write_str(int socket, const std::string& str,
152 const std::string& host, int port) const;
157 void _write_str_bin(int socket, const std::string& str,
158 const std::string& host, int port) const;
159
160 protected:
161 int _port;
162 };
163}
164
165
166#endif /* !NETSOCKET_HH_ */
This class represent an abstract socket connection (udp | tcp server | tcp client)
Definition: netsocket.hh:34
int _bind(int port, const std::string &host)
Bind a UDP server.
Definition: netsocket.cc:86
virtual void writeto(const std::string &str, const std::string &host, int port)
function used to send a msg to a specific host (UDP)
Definition: netsocket.cc:502
std::string _read_line(int socket)
Get a line from socket (when used with textual protocol)
Definition: netsocket.cc:261
std::string _get_ip(int port, int socket) const
Get Client Ip.
Definition: netsocket.cc:243
void _write_str_bin(int socket, const std::string &str, const std::string &host, int port) const
Write a string to a socket to a particular host (UDP) (when used with binary protocol)
Definition: netsocket.cc:446
std::string readn(unsigned int size)
read a string from socket
Definition: netsocket.cc:575
void _connect(int socket, int port, const std::string &host) const
Connect to a hostname.
Definition: netsocket.cc:186
NetSocket(SOCKET_KIND kind, SOCKET_VERSION version=V4)
Definition: netsocket.hh:36
struct sockaddr_in _get_addr(int port) const
internal function (construct a sockaddr)
Definition: netsocket.cc:61
NetSocket(SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version=V4)
Definition: netsocket.hh:40
void _write_str(int socket, const std::string &str, const std::string &host, int port) const
Write a string to a socket to a particular host (UDP) (when used with textual protocol)
Definition: netsocket.cc:400
virtual ~NetSocket()
Definition: netsocket.hh:45
int _accept(int port, int server_socket) const
Wait for a client.
Definition: netsocket.cc:211
virtual std::string _read_line_bin(int socket, int &port, std::string &host, unsigned int pkg_size)=0
Get a line from socket and store client hostname and port in port and host variable (when used with b...
std::string read()
function used by >> operator (read a string on current socket)
Definition: netsocket.cc:533
This class represent an abstract socket connection (udp | tcp server | tcp client)
Definition: socket.hh:101
Network namespace represent all networks connection.
Definition: localsocket.cc:33
@ V4
Definition: socket.hh:87
enum Network::e_version SOCKET_VERSION
enum Network::e_kind SOCKET_KIND
enum Network::e_pkind PROTO_KIND