0

I'm using the standard C/C++ socket function, but I'd like to encapsulate them into a C++ class. The problem is that the functions for sending and receive returns (or require) pointers to void. Is there any way to use an object that encapsulates those values?

For example, in Java the Socket class uses both ObjectOutputStream and ObjectInputStream in order to work with Object type so every object can be sent via Sockets. I know that in Java the approach is quite different because the pointers are hidden to the programmer, but is there any similar solution in C++?

user4581301
  • 33,082
  • 7
  • 33
  • 54
VeryMina
  • 13
  • 3
  • 1
    See if you can use [Boost's Socket Iostreams](https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/overview/networking/iostreams.html) – user4581301 Jan 07 '20 at 18:43
  • 1
    You need to use some ready-made library or framework for this. See https://stackoverflow.com/questions/4199185/socket-api-or-library-for-c – Vladimir Bershov Jan 07 '20 at 18:45
  • I'd try to get a bit of distance between your code and sockets. What I mean is, that if you e.g. want to implement HTTP, use an HTTP library. For other protocols, the same applies. Don't implement protocols yourself unless there's a good reason. If you just want to exchange messages between programs, consider using e.g. ZeroMQ instead. – Ulrich Eckhardt Jan 10 '20 at 23:49

2 Answers2

0

socket isn't a c++ function. It's a system level function and it doesn't know anything about objects (or indeed anything in c++), so you have to arrange to provide it with a pointer to the data you want transferred.

gct
  • 14,100
  • 15
  • 68
  • 107
0

As @GCT says, socket isn't a function but is a system level function which is used to handle network connections. In C/C++ each socket is identified with an Integer value, so it's not easy, as you want, to handle it as an object.

I recommend you to read this tutorial to know more about socket.

Maybe it can help you: I have a project that show how to use sockets in C++. Server and client are contained in their own class. You can get it by this link.

Jorge Omar Medra
  • 978
  • 1
  • 9
  • 19