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".
Suggestions?
Thanks! -
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: https://qt-project.org/doc/qt-5/qabstractsocket.htmlhere's an example to set the time to live (TTL) value:
@ QUdpSocket socket;
int port=10000;
int msg_ttl=20;
socket.bind(port);
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;
assert(!sockErr);@ -
- BSD sockets is a "standard" communication C-library for Unix and Linux. I think Windows has a compatibility layer for BSD sockets.
- 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()":http://msdn.microsoft.com/en-us/library/ms740506(v=vs.85).aspxIf 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":http://msdn.microsoft.com/en-us/library/ms742213(v=vs.85).aspx and use native BSD sockets on Linux, etc.