Cristian Diaconescu
Cristian Diaconescu

Reputation: 35701

Write a client-server program in C ... on a sheet of paper

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++.

Upvotes: 3

Views: 1945

Answers (8)

John
John

Reputation: 299

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.

Upvotes: 1

Dr. Watson
Dr. Watson

Reputation: 3820

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.

Upvotes: 0

Greg Domjan
Greg Domjan

Reputation: 14115

DCOM and Named Pipes could also be options

Upvotes: 0

Peter G.
Peter G.

Reputation: 15144

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

Upvotes: 2

Martin Beckett
Martin Beckett

Reputation: 96167

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

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

   system("telnet 127.0.0.1 80") 

}

;-)

Upvotes: 8

unwind
unwind

Reputation: 400059

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).

Upvotes: 7

Cristian Diaconescu
Cristian Diaconescu

Reputation: 35701

Here's one possibility: CORBA

The specs

IDL -> C mapper

A tutorial for C++

Upvotes: 1

Related Questions