r/codereview • u/Right-Beginning2651 • 23d ago
C/C++ I’ve been actively programming in C++ for over a year now.
Recently, I wrote a project that I’m proud of and would like to share, and perhaps also to hear some criticism regarding the implementation of my project.The project includes a simple client and server for exchanging files and folders using its own data transfer protocol.If it’s not too much trouble, please take a look and rate this project — it will motivate me to create new, larger‑scale projects.
Currently, the project is under development, so you can actively help me with the development by submitting a Pool request or suggestions for improvement in this post.Or you can simply mark the project or my other completed projects with a star(If, of course, they deserve it) — this motivates me to create and improve current projects.
The GitHub link to my project is below.
2
u/Rougeon-Goree 17d ago
make sure your struct serialization for the protocol headers handles endianness and compiler padding explicitly, otherwise itll break the moment you run the client and server on different architectures
1
u/Jazzlike_Place_1974 22d ago
how does it handle large files right now? if youre buffering the whole file in memory before sending, chunking the file stream into fixed-size buffers is a good next step so it doesnt blow up RAM on multi-gigabyte transfers
1
u/kingguru 22d ago
Nice to see some actual code here.
This subreddit is 99% "AI" slop and spam so you probably won't get much out of posting here.
I can see you've already got some very useful comments from posting in /r/Cplusplus. I agree with those comments so I cannot really add more but suggest you address these issues first (especially
using namespaceand#definewill scare away experienced C++ developers) and then maybe try posting in /r/cpp_questions where you'll actually feedback from experienced C++ developers.But again, start by addressing the issues that's already been pointed out to you.
1
u/voquanghoa 22d ago
"Submitting a Pool request", did you mean "Pull Request"? 🥸
I used C++ when I was a student and later when I joined a game studio. We developed video games, so I had some hands-on experience with it. Writing code entirely by hand is definitely an interesting experience. People who still have the opportunity to do that today should be proud of it!
1
u/Plus-Dust 20d ago
I'm not AI, so I looked at your code for a minute. It looks pretty decent...I personally don't care for the way it's spaced and the spacing is a little inconsistent in a few places as well (
buffer+= to_string(it->ifa_name) + ": ";) and I'm personally not a fan of Boost but like that's just me I'm not trying to nitpick. Another thing I noticed is #defines/constexpr's and class definitions in the middle of a file which looks a little surprising to me.1
u/nickdaniels92 6d ago edited 6d ago
Well done for working on something. There are some major issues with the code however, and this is more of a C application with a few classes thrown in rather than a true C++ codebase.
In no particular order and this is far from an exhaustive list. I have missed off some of the more serious issues and bugs, focussing on the basics.
* No semicolon is needed after the closing brace of a class.
* You tend to have uninitialised variables, such as with the Request constructor, so the following would have undefined behaviour.
Request r;string data = r.convert();* in times gone past an initialisation list would have been used, though there are other ways to handle initialisation now.
* convert is a terrible method name. Convert what? Convert why?
* put at least one space before //, but if it applies to a block of code, put it above the code on a separate line. e.g. the append bytes for reading comment
* The following could lead to out of bounds memory access.
* readU32 is faulty and badly implemented. For example, returning 1 for an error condition when 1 is a valid value. return is not a function so does not need parens. Consider using exceptions or some other error mechanism.
* this does not guarantee that there is sufficient data to read with the memcpy, merely that there is at least one byte in read_data. Memory beyond read_data may therefore be accessed.
if(read_pos>=read_data.size()){//check sizereturn 1;}memcpy(&buffer,read_data.data()+read_pos,sizeof(uint32_t));//copy bytes in buffer* Having functions that are not inside a class. In general you should expect for all code to be methods that belong to a class that models some concept or "thing". There may be cases where this doesn't hold, but in general it will. In the case of your read or append functions for example, consider what they are doing and why they are used. They are used to read and write to a buffer. Buffer is a thing, a concept, so should exist as a class and not be implemented simply as an stl string. Think about what a buffer is. What does it mean to be a buffer. What questions can a buffer be asked? What can it do? In this case a buffer needs some storage, so you might have an stl string as a property of the buffer. This property should be private, not public that you seem to use. To be useful you would want to read and write to a buffer, so that's where you might expect the read and write methods to be. You have read and app(end), which is odd. Append is not the natural opposite to read.
* make use of private as mentioned above.
* your member variables should be named differently to others. A helpful convention is to prefix m_ for member vars and s_ for statics. Some use just an underscore prefix, though m_ is clearer and makes s_ consistent with the convention.
* there is a lot of full buffer copying in the implementation though likely a performance issue that is of no consequence.
Keep at it though and well done for not simply going the easy route of using an LLM.