views:

253

answers:

8

This was an actual interview question. o_O

Let's leave aside the issues of asking such a question in an interview.

I'm wondering what alternatives to the ol' TCP sockets approach are readily available (as libraries, for instance), for use in C or C++.

I'm making no assumptions on platform, compiler etc - take your pick.

I'm not asking you guys to actually write the program, but merely to point at technologies that may be usable for the purpose, and possibly at an example or tutorial using that technology in C/C++.

+1  A: 

Here's one possibility: CORBA

The specs

IDL -> C mapper

A tutorial for C++

Cristi Diaconescu
+7  A: 

How about Boost? http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/tutorial.html

Radu C
To whoever downvoted this, I'm curious, what's wrong with this answer?
fingerprint211b
@fingerprint I second that. I think the answer is nice. +1
Cristi Diaconescu
Some people just don't like things they can't understand. I'm giving it a +1.
Amardeep
+7  A: 

I would totally go for the bare-minimum straight Berkeley sockets approach. I would be quite chanceless to get all the arguments right for the various functions, but I think I would be pretty close in the actual sequence of calls needed (create socket, bind, accept, read/write etc).

unwind
+1. And for a tutorial, look no farther than [Beej's Guide to Network Programming](http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html).
Matt B.
Beej's guide is great! If you know how to use the Berkeley sockets, the rest of the libraries and APIs make a lot more sense. I also suspect that most employers are looking to see that you understand these core concepts, especially for C/C++ programmers. But maybe not...
Dr. Watson
+7  A: 

I'm making no assumptions on platform, compiler etc - take your pick.

main() {
   system("apache -start")

   system("telnet 127.0.0.1 80") 

}

;-)

Martin Beckett
made me chuckle
Cristi Diaconescu
LOL, what about `main() { int server; int client = 1; server = client; }` actually the client residing at some memory address is communicating with server residing at different address and sending a value of 1 to it ;)
doc
+2  A: 

Use tcpd so the you can base the server on stdin/out instead of sockets. Knowledge of tcpd should impress the interviewer IMO. :-)

Peter G.
A: 

DCOM and Named Pipes could also be options

Greg Domjan
DCOM on a piece of paper? I hope this was a joke...
Amardeep
A: 

If you're not afraid to get your hands dirty in the documentation, then Boost.ASIO is a great library. You can also look at ACE, another popular library that encapsulates the Berkeley sockets in an easy-to-use interface.

Dr. Watson
+1  A: 

I don't think you can totally disregard the "interview" part of this question because the question is far too vague to be useful outside of the context of an interview. It might as well be asking us to write a "multi user" program. The interviewer probably expected you to ask more questions. Most notably, to find out what IPC mechanism he requires, and what the gist of the protocol is (i.e., How are they communicating and what are they communicating?).

Without any of that information, you just have to assume the most common: TCP/IP sockets, where the server listens, the client initiates a connection, and the communication is simply a client request followed by server response. In which case, on the paper you might write this,

// server
s = socket();
listen (s);
bind (s, addr_port);
while ((c = accept (s)) != -1)
    spawn_thread_or_proc (handle_connection, c);


// client
s = socket();
connect (s, addr_port); 
...

to prove you know the basic calls. If more detail is required, then you can flesh out the parameters, return values, error handling, the read/write calls, the thread/proc mechanism, the select/poll mechanism, the dns lookup mechanism.

John