Very Simple C++ TCP Echo Server [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am new to C++ network programming but have experience with Java sockets etc.
I have been trying to write a simple TCP echo server in C++ but cannot really make any progress. I've tried looking at some code like at http://cs.baylor.edu/~donahoo/practical/CSockets/practical/ but cannot get anything to work.
Can anyone give me some simple C++ code to get started with for something like a TCP echo server? I do not really understand how to even get started.
Thanks in advance.

The terms "simple" and "C++ TCP Echo Server" don't belong together in the same sentence. There is no such thing.
The sample that you are looking at is probably as close to "simple" as you're going to get (if you want to get into the nitty-gritty). Using a library that handles all the heavy lifting for you would make things easier (but far less educational). I would probably check out Boost.Asio (and the example Blocking TCP Echo Server example).
If things aren't making sense, you should probably go back and brush up on your C++ network programming until you get to the point that things start clicking.

To learn network programming, I highly recommend you see if you can buy, beg, borrow, or steal (stay away from my copy) a copy of Richard W Stevens book Unix Network Programming (note that after the first edition, the subsequent editions are split into volumes, so make sure you get the right volume for TCP/IP).
I found it to be a detailed resource for learning TCP programming, primarily on Unix/POSIX systems. If memory servers, it has some code for a TCP echo client and server written in C that it uses for some of its examples. You can find the source code for the book here - dig around the Makefile and source code in the tcpcliserv dir:
http://www.kohala.com/start/unpv12e.html
Edit: I realize its not a C++ version you asked for, but if I'm right that your end goal is learning network programming, learning it in C should be a fine stepping stone to C++ ....
B

I would recommend that you look into the boost framework -- it provides the same kind of "indispensable utility classes" that the JDK provides to Java programmers.
There are many tutorials available for various aspects of boost. Here's one on getting started with the asynchronous i/o components.
If you want to jump straight into the (very simple) server socket example, here it is.

Related

Writing drivers for Windows [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I recently asked the question if I could limit bandwidth via a C# WinForms applications much like NetLimiter and NetBalancer. I was told that there's two ways to do this. Either via proper QoS or writing something along the lines of an "NDIS Network Filter Driver". Using QoS isn't the way I want to take. So I've looked up some of the stuff required to write drivers and found some interesting points. Points such as a good understanding of C/C++, because the executed code being very prone to BSODs since it could be run in something called "Kernel mode". I also found a GitHub "dump" which looks interesting and tempts me to investigate and look around in.
As you can see I'm no where near advanced enough to delve into this on a professional level. Ignoring that, what would be a good start to start my adventures into writing drivers to monitor - and further down the line manipulate the network to introduce throttling.
Any help, guides or information that might be of help is always appreciated.
PS: I am unsure as to whether this is (as afore mentioned in a comment to my previous question) too broad a question to be answered on Stack Overflow. If so, where would I go to ask this?
Indeed, this would be too broad. Driver writing is a complicated thing which requires a good understanding of how a computer and the OS works. Also, C# (and .NET itself) indeed isn't available in Kernel Mode, so C/C++ is the preferred way. Although theoretically any unmanaged language (like Pascal) could do, I haven't heard of anyone writing drivers in them. Microsoft's own developer resources are also written with C/C++ in mind.
Which brings us to the question of why you want to do it.
If you need it for work and there's a deadline - forget it. Get someone else who already knows this stuff. Or there might be a library out there that fills the need. Any of these options will be cheaper than your time spent learning all this stuff.
If it's for your own curiosity however - go for it! I'd advise by starting to learn C first. Not C++, that's more complicated and for drivers it will be easier with C anyway. But you can pick up C++ later too, it's good stuff. C++ is mostly compatible with C, so you can start with C and then continue with C++.
In parallel, get a good book about OS design. Not because you want to design an OS, but to understand the basic concepts that it is built upon. You should get a good understanding of things such as Kernel Mode/User Mode, virtual memory, interrupts, process scheduling, etc.
Learning a bit of assembly might be useful too (albeit not required).
Finally, when you feel like you've got a good grasp of the above, head over to MSDN and start reading about driver development. There will be long articles and example programs to get you started. Tweak them and play around in a virtual machine until you get what you need.
And also... read this.

Getting started with network programming in Qt [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I just started a summer job at my university doing work in a lab, and I've been given a rather large, vague problem to tackle without much guidance so I was hoping someone could help point me in the right direction.
Essentially it's a 3-d world built in Qt using VTK (Visualization ToolKit) for use in therapy and rehab, and my task is to find a way to network two or more instances of the program so that users can share the same 3-d environment (essentially a networked video game).
The professor wants it to be secure, for latency to be as low as possible, and for the program to record data after a session is complete.
So far I was thinking (without much experience) of doing a client/server model built in Qt, but I'm not sure where to start.
Q1:
Should I use Boost.asio, or Qt library for networking?
Q2:
Are there any concepts I should be mindful of from the get-go for security, and network programming in general? (I've heard good things about Beej's Guide, and books by W. Richard Stevens)
Trying to answer your first question, it depends on which platforms are you targeting (Windows, Linux, OSX...). You could use native OS socket api (bsd sockets or winsock) but Qt provides very good abstractions for these so for the sake of simplicity I would stick with it. I'm unfamiliar with boost.asio but I'm pretty sure that Qt could provide you with all you need disregarding the platform you intent to target.
As for the second question, you have to carefully analyze what kind of information you want to transmit and what will characterize the exchange.
If you intend to make it like a video game where players interact in real time, you are bound to use UDP sockets (although some data can go missing, it allows for "real time" communication). Control messages can circulate through TCP as the impact of latency won't be so critical and you want them to be reliably sent, so consider having two sockets (TCP and UDP) if that's the case, and use them for their purposes.
Those resources that you link are quite informative but assuming you have knowledge of TCP and UDP and their features, I would suggest you to brush up your multithreading skills. Qt offers you good infrastructure for this, but topics like Asynchronous I/O (how to implement selectable sockets) may help you to create a better design that will remove a lot of overhead from additional threads (specially for reading).
These are my 2 cents.
Good luck for your project, I'm sure it will be a good chance for you to learn and put some theory into practice.

How do I get started writing my own IP-Like Protocol? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So my question is, would it be possible to write a protocol which does the same as the Internet Protocol, and if so, how do I get started? And don't say, "isn't the Internet Protocol good enough?", yes it is, it is just to see if it is possible:P
I would like to know a bit more about how,
just for learning how protocols are done. I have some experience in programming, but not anything like networking protocols.
The short answer is yes -- it has been done, and could be done again.
Examples of what have been done include DECnet, NetBIOS, Appletalk, and ATM. Although I'm not sure it was ever fully implemented as intended (though DECNet came pretty close), the standard OSI 7-layer model for networking was originally intended as a model of actual implementation (i.e., the intent was that people would implement those layers, and you'd be able to build a fully network stack by plugging together the implementation of your choice of each layer).
Of course, what most of us think of as IP today is really IPv4 -- as you can probably guess from the version number, it had predecessors (and a successor, IPv6).
Edit: as to how you do it: pretty much like anything else. I'd start by writing up something about your motivation -- what you want to accomplish, what sorts of operations it should support, etc. Then start working on the simplest design you can figure out that can possibly do what you want. Then, as you use it and encounter problems, figure out whether they stem from poor implementation, or shortcomings in the design itself. Modify one or both as needed, trying to keep its evolution as coherent and understandable as possible.
In short: Yes, it would be possible. On a higher level (i.e. OSI layer 7) it is done daily. If you want to implement the next IP, all you need are:
Special hardware (for the actual physical implementation, assuming that your protocol greatly differs from IP)
Device drivers for your favourite operating system that support your protocol
Maybe a high-level API to facilitate implementation
Edit: Saw that two others beat me to it ;)
would it be possible to write a protocol which does the same as the
Internet Protocol?
Yes it is possible to write your own IP stack, but it is extremely difficult to actually go ahead and do it (and actually do it right) unless you are an expert level both in programming and in networking

Writing database software in C/C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have been a self taught web developer for a few years now, and usually use C#/ASP.NET, Python, and PHP. But I want to try to advance my skills by doing something more advanced. I am looking to try to build a database program like SQLite, or a mini MySQL sort of thing in C/C++ just to practice and learn new things.
EDIT:
My project doesn't have to be a RDBMS. It can be something like a simple web server or something. Just something to see if I like doing that kind of thing more than web development.
However, I can't seem to find any kind of book or tutorials online that teaches this sort of thing. Does anyone know where I can find resources regarding this? I have a C book that teaches the language itself, but I learn how to think through things a lot better when I try to build something specific like a database engine etc. Thanks for any input.
For literature I could recommend something like Accelerated C++ or Thinking in C++. I also recently got my hands on Code Complete, found it in a shelf at work, and it is as good as people say. Solid language agnostic advice.
Also you should separate C and C++, they may seem similar and people clump them together but it's really two different ways of thinking. Now the new C++11 makes the differences even more important to understand, C++ is just not C with classes.
Why don't you try something a bit smaller like a ray tracer? Its very attainable to write a simple one that can produce some nice images, and its something you can come back to again and again to add features.
You can read the book Learn C the Hard Way by Zed Shaw. He teaches you how to write C using Make and Valgrind. Later exercises have you write your own software package installer and a tiny web server. Best of all, it's free.
Updated broke link
Bookmark this resource, http://nptel.iitm.ac.in/courses.php?disciplineId=106
This is often a great/massive start point for in depth knowledge about everything from algorithms, dbms, graphics to real time systems. Complete video courses/lectures or written course materials. A place to expand knowledge or get ideas.
For example you could check the video lectures about dbms development
-> http://nptel.iitm.ac.in/video.php?subjectId=106106093
...Or why not this one about artificial intelligence -> http://nptel.iitm.ac.in/video.php?subjectId=106105077
No c/c++ examples but they drill down each part, patterns, strategy and algorithms.
...The only sad thing is that most professors speak in really bad English.
Well after #MitchWheat said that even writing something like SQLite was pretty ambitious, I chose to try an do a little web server instead and found this post which included a few links for doing that. Thanks for the input.

c++ - relearning [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I haven't done C++ for about three years and looking to get back and ready. What is the best way? any open source projects that I might want to look at to recall all the details and be ready for interviews?
I started reading (again) C++ Primer 5th edition but was wondering whether there's more efficient way since I did program in C++ for few years before.
Just wanted to add: Does anyone know about open source projects related to finance? (e.g. servers, fix, etc)
I was facing a similiar situation a while back, and my conclusion was - no matter how many smart books you read nothing will suit you better then practicing.
Find some tutorials or set yourself some simple goals and learn by doing.
Hope that helps
PS. A friend of mine asked me once "How do you eat an elefant? - in small pieces, one at a time"
I'd start in on a real project.
If nothing else, download an open source C++ project that's in the same realm as the jobs you want to target, and start modifying. Practice helps more than anything for being comfortable.
If you're going to focus on reading, or in addition to practice, I'd actually focus on reading books that work more on using C++ well, not necessarily learning C++. Effective C++, More Effective C++, and Effective STL are great for this - you'll learn new things while refreshing your old knowledge. You can always use the primer book as a reference to study things you've forgotten as you read about them elsewhere.
Pick something that you know very little about and attempt to make a program that works with that subject area. For example if you've never done GUI work fire up C++ and try to create a simple paint program in the GUI framework of your choice (Qt, WTL, whatever). Or if you've never worked with a database grab SQLite and create a little app that manages your time. Or better yet, combine all these areas into a larger program.
The key is to force yourself to learn how to do real, practical things and solve problems using the languages paradigms. Books are great to reinforce certain practices but they'll never replace hours of frustration trying to figure out why your pointers aren't pointing where they're supposed to.
If you are good at maths (or statistics, probability, finance, geometry) and want to recall how to build re-useable functions, classes, and templates, you are welcome to help us at mathlibcpp. I recommend it, its good learning. Building a complete library on a large subject is very educatively hard ;) I found a chance to use all OO methods I know in C++, even forced to learn more.
Regarding Fix, there is QuickFAST, a very efficient implementation of the 'Fix for streaming' protocol.
Regarding C++, I would look for something that uses Boost, as it seems to be a premiere library for C++ (QuickFAST uses it to some degree).
Check out http://www.topcoder.com. This is an amazing tool for practicing programming in many different languages (C++, Java, C, C#). You can even win money if you get good at it:)
As with anything, write many programs, (re)read the best books (such as Effective C++, already mentioned). :-)
And ledger is an open source finance program written in C++. (GnuCash is another but written in C.)
Most of KDE is written in C++, albeit with Qt's signal extensions. Probably lots of examples of good code there.