Arm Mbed OS support forum

TCPSocket & TCPServer in Mbed OS 6

It finally happened. The class TCPServer, that has been long deprecated, was totally eliminated in Mbed OS 6. This would be OK if TCPSocket class supplied the same functionality, but alas there is a small difference which ruins my code. The problem is in the format of accept(…) method:

nsapi_error_t  TCPServer::accept( TCPSocket * connection, SocketAddress * address = NULL )
TCPSocket *  TCPSocket::accept ( nsapi_error_t * error = NULL )

While TCPServer::accept(…) takes a pointer to an existing instance of TCPSocket as a parameter, TCPSocket::accept(…) creates a new instance of the same class all by itself and returns a pointer to it. This is all very well until one decides to build their own class inheriting from TCPSocket and extending its functionality. Obviously the new class should override TCPSocket::accept(…) method, so that a new instance of this class could be constructed and not one of the class TCPSocket, but I don’t see any reasonable way to do it.
With TCPServer::accept(…) this was not a problem, since it didn’t construct any objects by itself and I could supply it with a pointer of an instance of my extended class.

So the questions to the Mbed developers is: with all regards, can we have an overloaded accept method in TCPSocket similar to the one in TCPServer, if not return TCPServer class into the library altogether?

Or maybe I’m missing something and there IS a way to solve this little problem?

George.

What functionalities do you want to add?

You may want to try composition instead of inheritance. You can have an instance of TCPSocket in your class instead of extending it. This SO question may be helpful to see the difference between inheritance and composition.

Oh, thank you, Kentaro Okuda, for an insight. I understand the difference between inheritance and composition. Moreover, I have already considered this somewhat elaborate way to solve the problem, but it calls for restructurization of the code, which is not an option at the moment. Besides, I’d like to stick to object-oriented programming, and it’s no secret that inheritance is one of the main features oop presents. So why there should be a class, namely TCPSocket, which prevents using this feature? Why not adjust the class a little bit? :pinching_hand:

As for additional functionality, you know, there are much more to the socket implementations than HTTP server. Our sockets exchange data of certain format and their behavior is encapsulated in a class, which inherits directly from TCPSocket – except it doesn’t anymore. Or rather it does, but it can’t implement accept, that being the reason of using TCPServer class for a listener formerly.

I am also not too happy having 2 classes (TCPSocket and TCPServer) where one would be enough, that’s why I’m asking Mbed developers to add an overloaded method accept conforming to that of TCPServer class. And then I’d gladly say goodbye to TCPServer. :wave:

It is probably better open an issue and discuss with the team on GitHub.