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 the listen() 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) β€” the listen() 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 the SocketBase base 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() β€” the accept() 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 🌈

Debian Bobcat project files:

  • libbobcat6 β€” debian package containing the shared library, changelog and copyright note
  • 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. iykyk ✨


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