serversocket3bobcat
Server socket accepting internet connection requests
Usage
#include <bobcat/serversocket>
Linking option: -lbobcat
Overview
An FBB::ServerSocket may be constructed to listen for connection requests from the Internet or from the local host. Connection requests may be accepted in either blocking or non-blocking modes. When a connection is accepted a socket is returned which may be used to read information from or write information to the client that requested the connection. The socket that is made available is a file descriptor which may be used to initialize a std::istream and/or std::ostream. The std::istream is used to read information from the client process; the std::ostream is used to send information to the client process. Since a socket may be considered a file descriptor the available FBB::IFdStream, FBB::IFdStreamBuf, FBB::OFdStream, and FBB::OFdStreamBuf classes may be used profitably here. Note that having available a socket does not mean that this defines the communication protocol. It is (still) the responsibility of the programmer to comply with an existing protocol or to implement a tailor-made protocol. The latter situation implies that the sequence of input- and output operations is defined by the programmer.
A Unix Domain server socket can be defined using FBB::LocalServerSocket.
Namespace
FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.
Inherits from
FBB::SocketBase
Constructor
- o
-
ServerSocket(size_t port):
This constructor initializes anFBB::ServerSocketobject, which will listen for connections at the specified port. The construction of the socket does not mean that theFBB::ServerSocketobject is actually listening for connections. To start listening, the memberlisten()should be used.
Copy and move constructors (and assignment operators) are not available.
Member functions
All members of FBB::SocketBase (and thus of FBB::InetAddress) are available, as FBB::ServerSocket inherits from FBB::SocketBase.
- o
-
void listen(size_t backlog = 5, bool blocking = true):
Thelisten()member defines the way theFBB::ServerSocketwill listen for clients requesting a connection. It can be used only once with aFBB::ServerSocket. AnFBB::Exceptionobject is thrown if listening fails, if the constructor could not create a socket, or if theSocketBasebase class could not properly be constructed.
-
The
listen()member’s backlog parameter defines the size of theFBB::ServerSocket’s internal queue in which connection requests may be stored waiting for their turn to be serviced. When backlog requests are waiting and another request arrives, then that request is lost.
-
The member’s second parameter, blocking, is used to control the blocking mode. By default, blocking is used, and listen() will wait until a connection is established. This is ok in situations where clients connect infrquently and for relatively short time intervals. Otherwise, in more complex programs, an
FBB::Selectorobject can be used to sense input on the server socket and/or on various client sockets.
- o
-
SocketBase accept():
Theaccept()member returns anFBB::SocketBaseobject containing information about the client whose connection request was accepted. TheFBB::SocketBaseobject’s socket value may be used to initialize streams that can be used to communicate with the client. In more complex programs theFBB::SocketBasecould be passed to a class derived fromFBB::Fork, handling the communication with the child as a separate (child) process.
Examples
See also the clientsocket(3bobcat) example.
#include <iostream>
#include <string>
#include <bobcat/serversocket>
#include <bobcat/ifdstream>
#include <bobcat/ofdstream>
using namespace std;
using namespace FBB;
int main(int argc, char **argv)
try
{
if (argc == 1)
{
cerr << "Provide server port number\n";
return 1;
}
size_t portnr = stoul(argv[1]);
ServerSocket server(portnr);
cerr << "server listens on port " << argv[1] << endl;
cerr << "serversocket returns:\n" <<
"address = " << server.dottedDecimalAddress() << "\n"
"port = " << server.port() << endl;
int fd = server.socket(); // open the socket’s descriptor
cout << "File descriptor of the socket is " << fd << "\n"
"The server terminates when it receives a "
"single `q’ on a line\n"
"A connection is terminated when no input "
"is received anymore.\n"
"Then another connection is possible" << endl;
server.listen(); // listen in blocking mode
while (true)
{
SocketBase fdb = server.accept();
int fd = fdb.socket();
cerr << "Client FD = " << fd << ", " << endl <<
"address = " << fdb.dottedDecimalAddress() << ", " <<
endl <<
"communication through port " << fdb.port() << endl;
IFdStream in(fd); // stream to read from client
OFdStream out(fd); // stream to write to client
string cmd;
while (getline(in, cmd))
{
cout << "Got: " << cmd << endl;
out << "Got: " << cmd << "\r" << endl;
if (cmd[0] == ’q’)
return 0;
}
cout << "Ready for another connection\n";
}
}
catch (Exception const &err)
{
cerr <<
err.what() << endl <<
"Server socket on port " << argv[1] <<
" can’t be opened" << endl;
return -1;
}
Files
bobcat/serversocket - defines the class interface
Bobcat project files
- o
-
https://fbb-git.gitlab.io/bobcat/: gitlab project page;
Debian Bobcat project files:
- o
-
libbobcat6: debian package containing the shared library, changelog and copyright note;
- o
-
libbobcat-dev: debian package containing the static library, headers, manual pages, and developer info;
Bobcat
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.
Releases
10 releases since Debian 4.0 (Etch); the text changed 9 times.