FBB::ServerSocket(3bobcat)
π FBB::ServerSocket π
okay bestie, welcome to the bobcat section 3 glow-up π
this is FBB::ServerSocket and she is the server socket that accepts Internet connection requests. yes, THE one. let's get into it π¦
β¨ NAME β¨
FBB::ServerSocket β a server socket that accepts Internet connection requests. iconic behavior, no cap π
π SYNOPSIS π
#include <bobcat/serversocket>
Linking option: -lbobcat
πΈ DESCRIPTION πΈ
okay so listen up π«Ά an FBB::ServerSocket can be constructed to listen for connection requests coming from the Internet or from the local host. you get to accept those connection requests in either blocking mode or non-blocking mode, your choice bestie β¨
the moment a connection is accepted, you get a socket back, and that socket is what you use to read information from the client that requested the connection, or write information to them. the socket you get is literally a file descriptor β and a file descriptor can be used to initialize a std::istream and/or a std::ostream. the std::istream is the one you read stuff from the client process with, and the std::ostream is the one you send stuff to the client process with π
and since a socket may be considered a file descriptor, you can absolutely use the available FBB::IFdStream, FBB::IFdStreamBuf, FBB::OFdStream, and FBB::OFdStreamBuf classes here. profitably, even. slay π₯
not me reminding you: just having a socket does not mean the communication protocol is defined!! it is (still) the programmer's responsibility to comply with an existing protocol or to implement a tailor-made protocol. and that second situation implies the sequence of input- and output operations is defined by the programmer. so like. that's on you bestie π
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. periodt β¨
π INHERITS FROM π
FBB::SocketBase
π§ CONSTRUCTOR π§
ServerSocket(size_t port)β this constructor initializes an FBB::ServerSocket object that will listen for connections at the specified port. but lowkey important: constructing the socket does not mean the FBB::ServerSocket object is actually listening for connections yet. to start listening you gotta use thelisten()member. don't forget or you'll be staring at nothing wondering why nobody's connecting π
copy and move constructors (and assignment operators) are not available. nope. not a thing. π
π MEMBER FUNCTIONS π
all members of FBB::SocketBase (and thus of FBB::InetAddress) are available, because FBB::ServerSocket inherits from FBB::SocketBase. inherited, bestie. they're yours now π«Ά
void listen(size_t backlog = 5, bool blocking = true)β thelisten()member defines the way the FBB::ServerSocket will listen for clients requesting a connection. heads up: it can be used only once with an FBB::ServerSocket. an FBB::Exception object is thrown if listening fails, if the constructor could not create a socket, or if theSocketBasebase class could not properly be constructed. so many ways for things to go sideways, but we move π
the listen() member's backlog parameter defines the size of the FBB::ServerSocket's internal queue where connection requests may be stored waiting for their turn to be serviced. and here's the dramatic part: when backlog requests are waiting and another request arrives, that request is lost. gone. never to be seen again π
the member's second parameter, blocking, controls the blocking mode. by default, blocking is used, and listen() will wait until a connection is established. this is totes fine in situations where clients connect infrequently and for relatively short time intervals. otherwise, in more complex programs, an FBB::Selector object can be used to sense input on the server socket and/or on various client sockets. main character energy for FBB::Selector honestly β¨
SocketBase accept()β theaccept()member returns an FBB::SocketBase object containing information about the client whose connection request was accepted. the FBB::SocketBase object's socket value may be used to initialize streams that can communicate with the client π and in more complex programs, that FBB::SocketBase could be passed to a class derived from FBB::Fork, handling the communication with the child as a separate (child) process. very grown-up of it πΈ
β EXAMPLE β
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 π
π¦ SEE ALSO π¦
bobcat(7), clientsocket(3bobcat), fork(3bobcat), ifdstream(3bobcat), ifdbuf(3bobcat), inetaddress(3bobcat), localserversocket(3bobcat), ofdstream(3bobcat), ofdstream(3bobcat), select(2), selector(3bobcat), socketbase(3bobcat)
(yes ofdstream(3bobcat) is listed twice in the original, and honestly? we respect the commitment π«Ά)
π BUGS π
None Reported. literally flawless, we love to see it β¨
π BOBCAT PROJECT FILES π
https://fbb-git.gitlab.io/bobcat/β gitlab project page π
Debian Bobcat project files:
libbobcat6β debian package containing the shared library, changelog and copyright notelibbobcat-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. iykyk β¨
π COPYRIGHT π
This is free software, distributed under the terms of the GNU General Public License (GPL). free software bestie, we love that for us π
π AUTHOR π
Frank B. Brokken (f.b.brokken@rug.nl). thank you king π
okay that's the whole thing, bestie, you're so welcome π«Ά this was libbobcat-dev_6.15.01, dated 2005-2026, and it's giving FBB::ServerSocket(3bobcat) energy from start to finish. go accept some connections, mwah ππΈ
π¦ where this page comes from
- ubuntu stonking: libbobcat-dev 6.15.01-1 Β· GPL-3.0-or-later WITH OpenSSL exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms