Important: Please read the Qt Code of Conduct -

BSD Socket wrapper with QT

  • Lets say that I have a project, that:

    • must be multiplataform (Win & Linux)
    • the source code must be based in BSD sockets syntax like.

    Is there any suggestion how can I wrapper QT socket functions to work like BSD sockets?
    Probably someone already implement a BSD socket library based on QT.

    The main propose is that I would like to add a new external BSD library (compatible with Win & linux .. .a .lib..obj..dll.. :S ) and must keep my source with the BSD socket include functions "language".


  • Qt sockets have a method to access their underlying raw socket,
    e.g. qudpsocket.socketDescriptor(), you might want to use that one to write your wrapper.
    doc here:

    here's an example to set the time to live (TTL) value:
    @ QUdpSocket socket;
    int port=10000;
    int msg_ttl=20;
    qintptr rawSocket=socket.socketDescriptor();
    int sockErr=setsockopt(rawSocket, IPPROTO_IP, IP_TTL, (const char*)&msg_ttl, sizeof(msg_ttl));
    // std::cout<<"Setsockopt error code: "<<WSAGetLastError()<<std::endl;

    1. BSD sockets is a "standard" communication C-library for Unix and Linux. I think Windows has a compatibility layer for BSD sockets.
    2. Qt is C++ library and it uses BSD sockets on Unix and Linux and OS X
    • Example 1

    A simplified example of BSD sockets initialization for server and client. I skipped all address resolving part.
    server = socket(...); /* create socket /
    bind(server, ...); /
    bind address to socket /
    listen(server, ...); /
    listen for client */

    client = socket(...); /* create socket /
    connect(client, ...); /
    connect to server */

    • Example 2

    A simplified Qt tcp server and client initialization example
    tcpServer = new QTcpServer;

    QTcpSocket socket;
    socket.connectToHost(serverName, serverPort);

    Do you need C++ or C library ?
    Is your idea to replace code in example 2 with code as in example 1 ?

  • I must use the BSD sockets header functions "syntax" like your Example1. So this is a C library.
    "In background", that BSD sockets header functions, should be implemented using QT sockets (like you describe in Example 2).

    If that doesn't exist (some one implemented it before opensource), I will need to implemented it.. or.. use some other additional external library that implements the BSD sockets (for windows in that case.. because for linux it is "built in..")

  • I don't think that BSD sockets on top of Qt exists.
    Windows implements the BSD sockets with some addition functionality.
    For example "socket()":

    If you have to use BSD sockets then it is easier to implement a layer on top of Windows Sockets Api to hide its "WSAStartup() and its relatives": and use native BSD sockets on Linux, etc.

Log in to reply