0

Is there a dead simple socket interface in C++ that I could use with MS Visual Studio Express Edition? I know there is the WinSock library, I guess I am looking for a wrapper around that which is simpler to use.

I would consider the java.net library simple enough, however, MS VC++ Express is what I have to use. Is there anything equivalent?

Mark M
  • 1,807
  • 2
  • 21
  • 40
  • Most programmers that used to write simple socket code are dead. – Hans Passant Nov 10 '10 at 18:59
  • bsd/win socket code is actually dead simple. the complexity in it comes in dealing with the nature of streams, and every 'simple' wrapper ive seen just re-exposes the same problems. – Chris Becke Nov 10 '10 at 19:02
  • What kind of app is this? A command-line tool that uses the network (like wget or ftp)? A GUI application that uses the network (like a web browser)? A background service that uses the network (like Windows Update)? A background network server (like a web server or ftp server)? Wrappers that work well in one environment are horrible for other applications. – Ben Voigt Nov 10 '10 at 22:08

2 Answers2

3

Check out Boost Asio.

yasouser
  • 5,113
  • 2
  • 27
  • 41
3

I used to work a lot at the socket level and if that's what you need then yes Boost::Asio is great, if a little confusing.

However, if you just need to deliver data between processes (on the same or different machines) then I would go a bit further up the stack and look at something like ØMQ; take a look how easy it is to receive a "message" from another process:

zmq::context_t ctx(1);

zmq::socket_t sock(ctx, ZMQ_REQ);
sock.connect("tcp://localhost:5555");

zmq::message_t msg;
while(sock.recv(&msg)) {
    std::string s(static_cast<char*>(msg.data()), msg.size());
    std::cout << s;
}

Sending is just as simple.

zmq::context_t ctx(1);

zmq::socket_t sock(ctx, ZMQ_REP);
sock.bind("tcp://*:5555");

std::string s = "Hello you!";
zmq::message_t msg(s.size());
memcpy(msg.data(), s.data(), s.size());

while(true) {
    sock.send(msg);
    sleep(1);
}

ZeroMQ is very lightweight and takes care of connection, reconnection, transmission, framing, etc... All you have to have is your "message" payload that you want to show up on the other side of the pipe (in this case we just used simple strings).

It also takes care of a number of more advanced messaging techniques such as pub-sub (multiple receivers of the same messages).

joshperry
  • 41,167
  • 16
  • 88
  • 103
  • http://api.zeromq.org/zmq_cpp.html is the C++ bindings; starting at the home page there's a bit of 'why has he recommended a Python library for C++?' – Pete Kirkham Nov 10 '10 at 20:41
  • Yeah, sorry. One nice think about ZeroMQ is that it has bindings for a very very large range of languages so inter-operation is very simple. http://www.zeromq.org/bindings:_start – joshperry Nov 10 '10 at 21:14
  • Though, it is natively implemented in C++ so the C++ "binding" is just the native interface of the library. – joshperry Nov 10 '10 at 21:38