I want to write (for the sake of learning) how to write a client-server app using C++. I have actually done it in the past and found many excellent tutorials on the web, including this good post on stack overflow:    What I would like to learn this time (and better understand) is how to actually write a protocol to communicate "meaningful" data between the client and server. Most examples just pass strings such as "Hello world". Great but not very practical.  What I would like to do is pass for example commands to the server that would modify some data (a document that the server would for example be responsible of keeping a copy of). I have been inspired after watching  in which they explain how the clients send commands like [insert, 10, "caterpillar"] to the server, which has for effect for example, to insert the chain of character "caterpillar" at index 10 in a string maintained by the server.  How would you send such commands typically in professional applications (how do you define them in the code, how do you pass them on, how to read them on the other end?)?   Bonus question;-): the video speaks a lot about data model. How does it relate to the protocol (the commands exchanged between the client and server and that allow them to understand each other)?  EDIT:  Actually found this question on Stackoverflow with some helpful answers:      The solution seems to be called serialisation.
120476
I'm looking into a mechanism for serialize data to be passed over a socket or shared-memory in a language-independent mechanism.  I'm reluctant to use XML since this data is going to be very structured, and encoding/decoding speed is vital.  Having a good C API that's liberally licensed is important, but ideally there should be support for a ton of other languages.  I've looked at  and .  Am I on the right track?  Is there something better?  Should I just implement my own packed structure and not look for some standard?
89543
I have a small server program that accepts connections on a TCP or local UNIX socket, reads a simple command and (depending on the command) sends a reply. The problem is that the client may have no interest in the answer and sometimes exits early.  So writing to that socket will cause a SIGPIPE and make my server crash. What's the best practice to prevent the crash here? Is there a way to check if the other side of the line is still reading? (select() doesn't seem to work here as it always says the socket is writable). Or should I just catch the SIGPIPE with a handler and ignore it?
78162
I collect a few corner cases and  and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible:  string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x, y));   I'd expect that to print False - after all, "new" (with a reference type) always creates a new object, doesn't it? The specs for both C# and the CLI indicate that it should.  Well, not in this particular case. It prints True, and has done on every version of the framework I've tested it with. (I haven't tried it on Mono, admittedly...)  Just to be clear, this is only an example of the kind of thing I'm looking for - I wasn't particularly looking for discussion/explanation of this oddity. (It's not the same as normal string interning; in particular, string interning doesn't normally happen when a constructor is called.) I was really asking for similar odd behaviour.  Any other gems lurking out there?
8707
The socket API is the de-facto standard for TCP/IP and UDP/IP communications (that is, networking code as we know it). However, one of its core functions, accept() is a bit magical.  To borrow a semi-formal definition:     accept() is used on the server side.   It accepts a received incoming attempt   to create a new TCP connection from   the remote client, and creates a new   socket associated with the socket   address pair of this connection.   In other words, accept returns a new socket through which the server can communicate with the newly connected client. The old socket (on which accept was called) stays open, on the same port, listening for new connections.  How does accept work? How is it implemented? There's a lot of confusion on this topic. Many people claim accept opens a new port and you communicate with the client through it. But this obviously isn't true, as no new port is opened. You actually can communicate through the same port with different clients, but how? When several threads call recv on the same port, how does the data know where to go?  I guess it's something along the lines of the client's address being associated with a socket descriptor, and whenever data comes through recv it's routed to the correct socket, but I'm not sure.  It'd be great to get a thorough explanation of the inner-workings of this mechanism.
25485
If I decide to write a simple game both text and graphical (2d) what libraries would I use? (Assume we are using a HTML5 compatible browser)  The main things I can think of   Rendering text on screen Animating sprites (using images/css) Input (capturing the arrow keys and getting relative mouse positions) Perhaps some preloading resource or dynamically loading resources and choosing order Sound (but I am unsure how important this will be to me at first). Perhaps with mixing and chaining sounds or looping forever until stop. Networking (low priority) to connect a user to another or to continuously GET data without multiple request (I know this exist but I don't know how easy it is to setup or use. But this isn't important to me. Its for the question).
7075
I have a function which parses one string into two strings.  In C# I would declare it like this:  void ParseQuery(string toParse, out string search, out string sort) {     ... }   and I'd call it like this:  string searchOutput, sortOutput; ParseQuery(userInput, out searchOutput, out sortOutput);   The current project has to be done in C++/CLI.  I've tried  using System::Runtime::InteropServices;  ...  void ParseQuery(String ^ toParse, [Out] String^ search, [Out] String^ sort) {     ... }   but if I call it like this:  String ^ searchOutput, ^ sortOutput; ParseQuery(userInput, [Out] searchOutput, [Out] sortOutput);   I get a compiler error, and if I call it like this:  String ^ searchOutput, ^ sortOutput; ParseQuery(userInput, searchOutput, sortOutput);   then I get an error at runtime.  How should I declare and call my function?
86120
As per the title, curious about any implementation details that can be revealed.  I don't see any details in the  that indicate how they might be stored.  Particularly, I'm curious about whether or not it's stored on the client side, the server side, a combination of the two (e.g. a cookie that the server uses) and any underlying details that can be exposed.
36534
Having recently introduced an overload of a method the application started to fail. Finally tracking it down, the new method is being called where I did not expect it to be.  We had  setValue( const std::wstring&amp; name, const std::wstring&amp; value );  std::wstring avalue( func() ); setValue( L"string", avalue ); std::wstring bvalue( func2() ? L"true", L"false" ); setValue( L"bool", bvalue ); setValue( L"empty", L"" );   It was changed so that when a bool value is stored we use the same strings (internal data storage of strings)  setValue( const std::wstring&amp; name, const std::wstring&amp; value ); setValue( const std::wstring&amp; name, const bool&amp; value );  std::wstring avalue( func() ); setValue( L"string", avalue ); setValue( L"bool", func2() ); setValue( L"empty", L"" ); &lt;&lt; --- this FAILS!?!   The problem with L"" is that it is implicitly casting and previously it was happy to be a std::wstring, but not it prefers to be a bool. The MSVC compiler does not complain, or raise warning, so I'm worried that even if I "fix" the setValue( L"empty", L"" ); to be  setValue( L"empty", std::wstring() );   somebody else may come later and simply use setValue( L"empty", L"" ); and have to track down this issue again.  We thought to use explicit on the method but it is not a valid keyword for this usage. Is there some way to get the compiler to complain about this, or otherwise prevent the issue?  Otherwise I'm thinking to change the name of the method which takes a bool to ensure it can't make an incorrect guess.
97173
I am working on a Web application, and I was looking for a reasonable implementation for one of its backend services. I wanted to ask the community if they have done something similar and if so how does their implementation work?  This is a subjective and possibly open-ended question, hence Stack&nbsp;Overflow is not the right place to ask this question.  Is there a Stack&nbsp;Exchange community in which one can ask suchlike questions?
46146
I often use the execv() function in C++, but if some of the arguments are in C++ strings, it annoys me that I cannot do this: const char *args[4];  args[0] = &quot;/usr/bin/whatever&quot;; args[1] = filename.c_str(); args[2] = someparameter.c_str(); args[3] = 0;  execv(args[0], args);  This doesn't compile because execv() takes char *const argv[] which is not compatible with const char *, so I have to copy my std::strings to character arrays using strdup(), which is a pain. Does anyone know the reason for this?
8492
I have seen links in servers that can send you to a page in a browser, is there any way to replicate this with command blocks?  I don't mean command links, I mean web pages.
122116
We have a codebase that is several years old, and all the original developers are long gone.  It uses many, many threads, but with no apparent design or common architectural principles.  Every developer had his own style of multithreaded programming, so some threads communicate with one another using queues, some lock data with mutexes, some lock with semaphores, some use operating-system IPC mechanisms for intra-process communications.  There is no design documentation, and comments are sparse.  It's a mess, and it seems that whenever we try to refactor the code or add new functionality, we introduce deadlocks or other problems.  So, does anyone know of any tools or techniques that would help to analyze and document all the interactions between threads?  FWIW, the codebase is C++ on Linux, but I'd be interested to hear about tools for other environments.    Update  I appreciate the responses received so far, but I was hoping for something more sophisticated or systematic than advice that is essentially "add log messages, figure out what's going on, and fix it."  There are lots of tools out there for analyzing and documenting control-flow in single-threaded programs; is there nothing available for multi-threaded programs?    See also
19774
We need to interface to 3rd party app, but company behind the app doesn't disclose message protocol and provides only Windows DLL to interface to. Our application is Linux-based so I cannot directly communicate with DLL. I couldn't find any existing solution so I'm considering writing socket-based bridge between Linux and Windows, however I'm sure it is not such a unique problem and somebody should have done it before. Are you aware of any solution that allows to call Windows DDL functions from C app on Linux? It can use Wine or separate Windows PC - doesn't matter. Many thanks in advance.
43864
The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc &amp; g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care):  char **a; const char** b = a;   I can understand and accept this. The C++ solution to this problem is to change b to be a const char * const *, which disallows reassignment of the pointers and prevents you from circumventing const-correctness ().  char **a; const char* const* b = a;   However, in pure C, the corrected version (using const char * const *) still gives a warning, and I don't understand why. Is there a way to get around this without using a cast?  To clarify: 1) Why does this generate a warning in C?  It should be entirely const-safe, and the C++ compiler seems to recognize it as such. 2) What is the correct way to go about accepting this char** as a parameter while saying (and having the compiler enforce) that I will not be modifying the characters it points to? For example, if I wanted to write a function:  void f(const char* const* in) {   // Only reads the data from in, does not write to it }   And I wanted to invoke it on a char**, what would be the correct type for the parameter?
32250
I've looked at , specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing something obvious (wood for the trees) - could someone please explain it using a definitive real-world example?    What I can make from the answers so far, it seems to me to be just a more complex way of doing this:  have an abstract class: MoveAlong with a virtual method: DoIt() have class Car inherit from MoveAlong,       implementing DoIt() { ..start-car-and-drive..} have class HorseCart inherit from MoveAlong,       implementing DoIt() { ..hit-horse..} have class Bicycle inherit from MoveAlong,       implementing DoIt() { ..pedal..} now I can call any function taking MoveAlong as parm  passing any of the three classes and call DoIt Isn't this what Strategy intents? (just simpler?)   [Edit-update] The function I refer to above is replaced with another class in which MoveAlong would be attribute which is set according to need based on the algorithm implemented in this new class. (Similar to what is demonstrated in the accepted answer.)    [Edit-update] Conclusion  The Strategy Pattern has it's uses, but I am a strong believer in KISS, and would tend to more straightforward and less obfuscatory techniques. Mostly since I want to pass on easily maintainable code (and 'cos I'll most likely be the one who have to make the changes!).
91571
I have downloaded Privoxy few weeks ago and for the fun I was curious to know how a simple version of it can be done. I understand that I need to configure the browser (client) to send request to the proxy. The proxy send the request to the web (let say it's a http proxy). The proxy will receive the answer... but how can the proxy send back the request to the browser (client)? I have search on the web for C# and http proxy but haven't found something that let me understand how it works behind the scene correctly. (I believe I do not want a reverse proxy but I am not sure). Does any of you have some explication or some information that will let me continue this small project? Update This is what I understand (see graphic below). Step 1 I configure the client (browser) for all request to be send to 127.0.0.1 at the port the Proxy listen. This way, request will be not sent to the Internet directly but will be processed by the proxy. Step2 The proxy see a new connection, read the HTTP header and see the request he must executes. He executes the request. Step3 The proxy receive an answer from the request. Now he must send the answer from the web to the client but how???  Useful link  : I have found this project that is a proxy (but more that I would like). I might check the source but I really wanted something basic to understand more the concept.  : I might be able to get some information over here too.  : This is a simple example. Here is a .
77872
Say I have the following C++:  char *p = new char[cb]; SOME_STRUCT *pSS = (SOME_STRUCT *) p; delete pSS;   Is this safe according to the C++ standard? Do I need to cast back to a char* and then use delete[]? I know it'll work in most C++ compilers, because it's plain-ordinary-data, with no destructors. Is it guaranteed to be safe?