![]() ![]() I disable Nagle's algorithm to ensure the TCP packets are sent as soon as the message is ready.I am currently postponing this issue unless there is a straightforward way to take care of it. This leads to a big problem with portability: my code will probably not work if the two communicating systems use different byte order or different struct alignment. Aside from these structs only having fixed-size primitive-type fields and no further structure, I would like my network code to work with any such user-defined struct-type. The contents of a message is an array of custom-defined C-structs. My messages can be variable size and have custom-defined headers specifying the length.In my real code I encapsulate the TCP logic in classes, which internally call the functions shown here. The other two code files are an example application (main methods to run server and client). tcp_helpers.h declares (AND defines for brevity of this post) functions containing all the TCP logic.I tried to shorten it as much as possible but it is still quite long. My problem seems very standard and most of the code is puzzled together from some tutorials on sockets but I was struggling to find detailed sources on best practices.īelow is a simplified demo code that illustrates the way I handle the TCP logic. This was my first time using sockets and I surely made many mistakes, both specific to sockets as well as pure C++ (I'm using C++17 although the code also compiles with C++11). I decided to use TCP rather than UDP because all data must be received in the correct order and the packet header size is not a relevant overhead, especially since latency is the issue. For lack of better alternatives I'm using (POSIX-API) TCP sockets here. If so, please point this out to me, preferably together with a demo or link to sources on how to use it. Probably what I want to do can be achieved by using some library. The messages are not very large, hence bandwidth does not seem to matter much. Speed is quite critical and deteriorates very quickly when the network has moderate latency (due to the many messages and unavoidable waiting for the response). This means that both parties have to wait idly for the response to arrive before they can carry on with computations. ![]() Each message sent is a response to the last received message and depends on it. Two parties communicate over a network sending back and forth up to 100 messages. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |