libsocket
1.5
|
This class represent a tcp connection (client and server) More...
#include <tcpsocket.hh>
Public Member Functions | |||
TcpSocket (SOCKET_VERSION version=V4) | |||
TcpSocket (PROTO_KIND pkind, SOCKET_VERSION version=V4) | |||
TcpSocket (int socket, SOCKET_VERSION version=V4) | |||
TcpSocket (int socket, PROTO_KIND pkind, SOCKET_VERSION version=V4) | |||
virtual | ~TcpSocket () | ||
void | connect (const std::string &hostname, int port) | ||
Connect as an TCP client. More... | |||
std::string | get_ip (TcpSocket *client) const | ||
return ip of client (after an accept) More... | |||
TcpSocket * | accept () const | ||
accept a new client (For server only) More... | |||
void | connect (int port) | ||
Connect as an TCP server (echo server) More... | |||
void | close () | ||
Close the connection. More... | |||
![]() | |||
NetSocket (SOCKET_KIND kind, SOCKET_VERSION version=V4) | |||
NetSocket (SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version=V4) | |||
virtual | ~NetSocket () | ||
virtual void | writeto (const std::string &str, const std::string &host, int port) | ||
function used to send a msg to a specific host (UDP) More... | |||
std::string | read () | ||
function used by >> operator (read a string on current socket) More... | |||
std::string | read (int timeout) | ||
read a string with a timeout More... | |||
std::string | read (int &port, std::string &host) | ||
Get a line from socket and store client hostname and port in port and host variable. More... | |||
std::string | read (int &port, std::string &host, int timeout) | ||
Get a line from socket and store client hostname and port in port and host variable (with a timeout on listen) More... | |||
std::string | readn (unsigned int size) | ||
read a string from socket More... | |||
std::string | readn (int timeout, unsigned int size) | ||
read a string with a timeout More... | |||
std::string | readn (int &port, std::string &host, unsigned int size) | ||
Get a line from socket and store client hostname and port in port and host variable. More... | |||
std::string | readn (int &port, std::string &host, int timeout, unsigned int size) | ||
Get a line from socket and store client hostname and port in port and host variable (with a timeout on listen) More... | |||
![]() | |||
Socket (SOCKET_KIND kind, SOCKET_VERSION version=V4) | |||
Socket (SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version=V4) | |||
virtual | ~Socket () | ||
void | write (const std::string &str) | ||
function used by << operator (write a string on current socket) More... | |||
bool | connected () const | ||
return true when socket is connected More... | |||
int | get_socket () | ||
get socket (fd) warning: be very carefull with this method More... | |||
void | add_delim (const std::string &delim) | ||
set the delimitor for the text mode More... | |||
void | del_delim (const std::string &delim) | ||
delete this delimitor for the socket More... | |||
void | allow_empty_lines () | ||
, if set, empty lines will be returned in text procols (if not, they are skipped) More... | |||
void | init_tls (GnuTLSKind kind, unsigned size=1024, const std::string &certfile="", const std::string &keyfile="", const std::string &trustfile="", const std::string &crlfile="") | ||
| |||
void | enable_tls () | ||
Enable TLS on socket. More... | |||
Protected Member Functions | |||||||||
std::string | _read_line_bin (int socket, int &port, std::string &host, unsigned int psize) | ||||||||
Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol)
| |||||||||
std::string | _read_line_bin (int socket, unsigned int psize) | ||||||||
Get a line from socket (when used with binary protocol)
| |||||||||
![]() | |||||||||
struct sockaddr_in | _get_addr (int port) const | ||||||||
internal function (construct a sockaddr) More... | |||||||||
struct sockaddr_in | _get_addr (const std::string &host, int port) const | ||||||||
internal function (construct a sockaddr)
| |||||||||
int | _bind (int port, const std::string &host) | ||||||||
Bind a UDP server
| |||||||||
int | _bind (int port) | ||||||||
Bind a TCP server
| |||||||||
int | _accept (int port, int server_socket) const | ||||||||
Wait for a client
| |||||||||
std::string | _get_ip (int port, int socket) const | ||||||||
Get Client Ip. More... | |||||||||
void | _connect (int socket, int port, const std::string &host) const | ||||||||
Connect to a hostname
| |||||||||
std::string | _read_line (int socket) | ||||||||
Get a line from socket (when used with textual protocol)
| |||||||||
std::string | _read_line (int socket, int &port, std::string &host) | ||||||||
Get a line from socket and store client hostname and port in port and host variable (when used with textual protocol)
| |||||||||
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)
| |||||||||
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)
| |||||||||
![]() | |||||||||
void | _close (int socket) const | ||||||||
Close a connnection
| |||||||||
void | _listen (int socket) const | ||||||||
Listen on port
| |||||||||
void | _write_str (int socket, const std::string &str) const | ||||||||
Write a string to a socket (when used with textual protocol)
| |||||||||
void | _write_str_bin (int socket, const std::string &str) const | ||||||||
Write a string to a socket (when used with binary protocol)
| |||||||||
void | _set_timeout (bool enable, int socket, int timeout) | ||||||||
set a timeout on a socket More... | |||||||||
std::pair< int, int > | _find_delim (const std::string &str, int start) const | ||||||||
bool | _update_buffer (std::pair< int, int > &delim, int &i, std::string &str) | ||||||||
look delimiter and remove delimiter at begining of buffer if needed More... | |||||||||
bool | _check_answer (int res, std::string &str) | ||||||||
return the content of the buffer is there is More... | |||||||||
Additional Inherited Members | |
![]() | |
int | _port |
This class represent a tcp connection (client and server)
Definition at line 32 of file tcpsocket.hh.
|
inline |
Definition at line 35 of file tcpsocket.hh.
Referenced by accept().
|
inline |
Definition at line 38 of file tcpsocket.hh.
|
inline |
Definition at line 41 of file tcpsocket.hh.
References Network::Socket::_socket.
|
inline |
Definition at line 46 of file tcpsocket.hh.
References Network::Socket::_socket.
|
inlinevirtual |
|
protectedvirtual |
Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol)
NoConnection | when there is no open socket |
ConnectionClosed | when there is no more connection |
GetpeernameError | when getpeername libc function return a negative value. |
Implements Network::NetSocket.
Definition at line 152 of file tcpsocket.cc.
References Network::Socket::_buffer, Network::Socket::_tls, Network::Socket::_version, HERE, Network::NetSocket::read(), and Network::V4.
|
protectedvirtual |
Get a line from socket (when used with binary protocol)
NoConnection | when there is no open socket |
ConnectionClosed | when there is no more connection. |
Implements Network::NetSocket.
Definition at line 81 of file tcpsocket.cc.
References Network::Socket::_buffer, Network::Socket::_tls, HERE, and Network::NetSocket::read().
TcpSocket * Network::TcpSocket::accept | ( | ) | const |
accept a new client (For server only)
Definition at line 42 of file tcpsocket.cc.
References Network::NetSocket::_accept(), Network::NetSocket::_port, Network::Socket::_proto_kind, Network::Socket::_socket, Network::Socket::_tls, Network::Socket::_version, Network::Socket::enable_tls(), TcpSocket(), Network::V4, and Network::V6.
void Network::TcpSocket::close | ( | ) |
Close the connection.
Definition at line 74 of file tcpsocket.cc.
References Network::Socket::_close(), and Network::Socket::_socket.
Referenced by ~TcpSocket().
void Network::TcpSocket::connect | ( | const std::string & | hostname, |
int | port | ||
) |
Connect as an TCP client.
Here is an example of tcp client using libsocket :
#include <stdlib.h> #include <iostream> #include <string> #include "socket/tcpsocket.hh"
int main(int argc, char **argv) { Network::TcpSocket client; std::string str;
if (argc < 3) { std::cout << "Use: " << argv[0] << " port hostname" << std::endl; exit(0); } try { client.connect(std::string(argv[2]), strtol(argv[1], NULL, 10)); client >> str; std::cout << str << std::endl;
while (str != "quit") { std::cin >> str; client << str; client >> str; } client.close(); exit (0); } catch (Network::Exception e) // catch all libsocket errors { std::cerr << e; exit(1); }
}
Definition at line 28 of file tcpsocket.cc.
References Network::NetSocket::_bind(), Network::NetSocket::_connect(), Network::NetSocket::_port, and Network::Socket::_socket.
void Network::TcpSocket::connect | ( | int | port) |
Connect as an TCP server (echo server)
Here is an example of tcp server using libsocket :
#include <stdlib.h> #include <iostream> #include <string> #include "socket/tcpsocket.hh"
int main(int argc, char **argv) { Network::TcpSocket server; Network::TcpSocket *client; std::string str;
if (argc < 2) { std::cout << "Use: " << argv[0] << " port" << std::endl; exit(0); } try { std::cout << "--- echo server ---" << std::endl; server.connect(strtol(argv[1], NULL, 10)); client = server.accept(); (*client) << "Welcome on test server"; while (str != "quit") {
//(*client) >> str; // read with a timeout of 30 second and get port and host str = client->read(port, host, 30); std::cout << "[" << str << "] from [" << host << ":" << port << "]" << std::endl; (*client) << str; } server.close(); exit (0); } catch (Network::Exception e) // catch all libsocket errors { std::cerr << e; exit(1); } }
Definition at line 35 of file tcpsocket.cc.
References Network::NetSocket::_bind(), Network::Socket::_listen(), Network::NetSocket::_port, and Network::Socket::_socket.
std::string Network::TcpSocket::get_ip | ( | TcpSocket * | client) | const |
return ip of client (after an accept)
Definition at line 69 of file tcpsocket.cc.
References Network::NetSocket::_get_ip(), Network::NetSocket::_port, and Network::Socket::_socket.