FBB::Lo...rSocket(3bobcat)

πŸ’– NAME πŸ’–

FBB::LocalServerSocket β€” bestie, it's the Unix Domain Server socket that accepts connection requests ✨ (and yes, this page is straight out of the bobcat manual, section 3bobcat, so you know it's real 🫢)


πŸŽ€ SYNOPSIS πŸŽ€

#include <bobcat/localserversocket>
Linking option: -lbobcat

🌸 DESCRIPTION 🌸

An FBB::LocalServerSocket is a Unix Domain server socket, and its whole job is listening for connection requests coming from the local host over a Unix Domain socket. No network, no drama, just localhost vibes πŸ’…

Connection requests can get accepted in either blocking or non-blocking mode β€” you pick. When a connection finally gets accepted, out pops a socket, and that socket is what you use to read info from the client or write info back to it.

Now here's the cute part: the socket you get is a file descriptor, which means you can absolutely use it to initialize a std::istream and/or a std::ostream 😌

  • the std::istream is for reading info from the client process
  • the std::ostream is for sending info to the client process

And because a socket is basically just a file descriptor, the existing FBB::IFdStream, FBB::IFdStreamBuf, FBB::OFdStream, and FBB::OFdStreamBuf classes are so usable here β€” they were literally made for this, iykyk.

⚠️ But bestie, listen β€” having a socket does not mean you have a communication protocol. That's still 100% on the programmer: either comply with an existing protocol, or implement a tailor-made one. And in that second case, the sequence of input and output operations is defined by you, the programmer. No cap.

πŸ¦‹ NAMESPACE πŸ¦‹

FBB

All constructors, members, operators and manipulators mentioned in this man-page are defined in the namespace FBB.


πŸ‘‘ INHERITS FROM πŸ‘‘

FBB::LocalSocketBase


🌈 ENUMERATION 🌈

The following enumeration is defined in the class FBB::LocalServerSocket:

enum Socket β€” this enumeration holds two values:

  • KEEP β€” when this value is specified at construction time, the file representing the Unix Domain Socket is not removed when the LocalServerSocket is destroyed.
  • UNLINK β€” when this value is specified at construction time, the file representing the Unix Domain Socket is removed when the LocalServerSocket is destroyed. And since a socket cannot be reused, this is the default value used with the LocalServerSocket constructor. periodt.

πŸ’… CONSTRUCTOR πŸ’…

  • LocalServerSocket() β€” this constructor creates an empty (non-functioning) FBB::LocalServerSocket object. Before you can do literally anything with it, you have to call the open() member (peep below πŸ‘‡).
  • LocalServerSocket(string const &name, Socket action = UNLINK) throw (Exception) β€” this constructor initializes an FBB::LocalServerSocket object, and it will listen for connection requests using the named Unix Domain socket. An FBB::Exception is thrown if the socket could not be constructed. If the constructor is given a second argument FBB::LocalServerSocket::KEEP, the constructed socket is not unlinked when the FBB::LocalServerSocket object is destroyed. And ngl, constructing the socket does not mean the object is actually listening for connections yet β€” to start listening, the member listen() should be called.
  • Copy and move constructors (and assignment operators) are not available. Not invited to the party, sorry. πŸ™ˆ

✨ MEMBER FUNCTIONS ✨

  • size_t accept() β€” the accept() member returns an size_t which is a file descriptor (socket) that may be used to communicate with the client requesting the connection. In more complex programs, that returned file descriptor (socket) could be passed to a class derived from FBB::Fork, handling the communication with the child as a separate (child) process. main character energy fr fr.
  • void listen(size_t backlog = 5, bool blocking = true) β€” the listen() member defines the way the FBB::LocalServerSocket will listen for clients requesting a connection. Heads up: it can be used only once with a FBB::LocalServerSocket. An FBB::Exception object is thrown if listening fails.
    • The listen() member's backlog parameter defines the size of the FBB::LocalServerSocket's internal queue, where 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 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.
  • void open(string const &name, Socket action = UNLINK) β€” this member prepares a FBB::LocalServerSocket object for use. It should only be used in combination with the default constructor. Following open() the FBB:::LocalServerSocket object will be able to listen for connection requests using the named Unix Domain socket. An FBB::Exception is thrown if the socket could not be constructed. If the a second argument FBB::LocalServerSocket::KEEP, is provided the constructed socket is not unlinked when the FBB::LocalServerSocket object is destroyed. The construction of the socket does not mean that the FBB::LocalServerSocket object is actually listening for connections. To start listening, the member listen() should be called next.

πŸ“ EXAMPLE πŸ“

See also the localclientsocket(3bobcat) example. πŸ‘€

#include <iostream>
#include <bobcat/localserversocket>
#include <bobcat/ifdstream>
#include <bobcat/ofdstream>

using namespace std;
using namespace FBB;

int main(int argc, char **argv)
try
{
    if (argc == 1)
    {
        cerr << "Provide local filename, e.g., /tmp/uds\n";
        return 1;
    }

    LocalServerSocket server(argv[1]);

    cerr << "server using `" << argv[1] << "’" << endl;

    cout <<
        "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)
    {
        int fd = server.accept();

        cerr << "Client FD = " << fd << ", " << 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), localclientsocket(3bobcat), fork(3bobcat), ifdstream(3bobcat), ifdbuf(3bobcat), localsocketbase(3bobcat), ofdstream(3bobcat), ofdstream(3bobcat), select(2), selector(3bobcat), serversocket(3bobcat)


πŸ’€ BUGS πŸ’€

None Reported. (clean sheet, 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’.


This is free software, distributed under the terms of the GNU General Public License (GPL).


🌸 AUTHOR 🌸

Frank B. Brokken (f.b.brokken@rug.nl).


okay bestie that's the whole page, you're fully caught up πŸ’– this was the FBB::Lo...rSocket(3bobcat) page, served fresh from libbobcat-dev_6.15.01, dated 2005-2026 β€” slay, go build something cute with it πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from