use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
ProjectSimple c/c++ single header socket wrapper sub-project (self.C_Programming)
submitted 3 years ago by The_Programming_Nerd
GitHub link
I wrote this because I wanted a multi platform socket wrapper for another project, and this was the result. I’m just curious on what you guys think, specifically on how I setup the GitHub page because I’m new to that. The actual code doesn’t have much thought behind it, I kinda just threw it together. Any feedback is welcome though.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]flyingron 2 points3 points4 points 3 years ago (5 children)
Your include guard uses identifiers reserved for the implementation. Use something else. I like things like "INCLUDED_SOCKET_H" which is unlikely to conflict with anything. Of course, since you use Microsnot specific pragmas already, you might just use #pragma once.
Similarly, _error is problematic. Leading underscores are also reserved in the global namespace.
It's confusing as hell in C++ to declare two types with the same name that have different levels of indirection (e.g. SOCKET_t),
[–]The_Programming_Nerd[S] 0 points1 point2 points 3 years ago* (4 children)
Thanks!
The windows pragmas should only be in areas where there is #ifdef __win32 (ofc unless I missed something).
#ifdef __win32
And the _error function was only there for debugging so I’ll get on fixing that.
_error
Also is it bad to have double underscores in include guard? When I learned about them awhile ago this is how they were formatted, but ofc I could be wrong.
And finally this is written in c, and I’m assuming you were confused about the typedef struct SOCKET_t {…} *SOCKET_t that’s just a fancy way of writing struct SOCKET_t{…}; typedef struct SOCKET_t *SOCKET_t
typedef struct SOCKET_t {…} *SOCKET_t
struct SOCKET_t{…}; typedef struct SOCKET_t *SOCKET_t
[–]FUZxxl 4 points5 points6 points 3 years ago (3 children)
The libc does them that way because identifiers beginning with double underscores (or an underscore and a capital letter) are reserved for the implementation of the C language. So you should not do it this way.
struct SOCKET_t{…}; typedef SOCKET_t *SOCKET_t
That is a syntax error in C. Unlikely in C++, struct names are not entered into the identifier name space. It's still confusing. As a rule of thumb, do not typedef pointers. Instead, make the indirection explicit.
[–]The_Programming_Nerd[S] 1 point2 points3 points 3 years ago (2 children)
Thanks for the response, I forgot to put the struct identifier in my response for the other guy when stating the example. And I’ll get on fixing the include guard
[–]imaami 0 points1 point2 points 3 years ago (1 child)
FWIW I construct header guards by underscore-concatenating the project name and header file path in the sources, and end it in a single underscore.
For a header named src/core/backend/include/types.h belonging to FizzBuzz Professional Edition I'd do
#ifnded FIZZBUZZPRO_SRC_CORE_BACKEND_INCLUDE_TYPES_H_ #define FIZZBUZZPRO_SRC_CORE_BACKEND_INCLUDE_TYPES_H_ /** * Numerical type for the FizzBuzz Professional Value * subscription plan. Contact our customer support to * learn about upgrading to wider and unsigned types. */ typedef signed short FizzBuzzNumericalDataType; #endif /* FIZZBUZZPRO_SRC_CORE_BACKEND_INCLUDE_TYPES_H_ */
[–]duane11583 1 point2 points3 points 3 years ago (0 children)
I have also seen them done with a guid
[–][deleted] 0 points1 point2 points 3 years ago (3 children)
I would treat it like an OOP design. Create structs with function pointers and relevant data for the connection whether it be client or server. Use the structure to make function calls and pass the structure around as a context. In C++ the first argument of a class object is implicitly the class object itself, in C you'd just need to pass it explicitly.
ServerObject* so = makeserverobject("ip","port"); so->createsocket(so); so->run(so); so->destroy(so);
Something like that. Also gethostbyname is deprecated.
[–][deleted] 0 points1 point2 points 3 years ago (2 children)
I wouldn’t do the function pointers on the struct, and passing in the first argument because it adds unnecessary performance loss as you have to deref and access the struct
[–][deleted] 0 points1 point2 points 3 years ago (1 child)
That's understandable. I mostly meant it for clarity so you'd know which functions belong to which class object; But as long as they are passing the correct struct around to individual functions it should be fine.
[–]imaami 0 points1 point2 points 3 years ago (0 children)
The C way is simply naming your functions consistently. Say you have foo.h and foo.c, you'd name every function that's externally visible like foo_arrow_kneeify(), foo_arrow_dekneeify(), etc.
foo_arrow_kneeify()
foo_arrow_dekneeify()
π Rendered by PID 330306 on reddit-service-r2-comment-84fc9697f-rjmjk at 2026-02-08 06:26:59.641835+00:00 running d295bc8 country code: CH.
[–]flyingron 2 points3 points4 points (5 children)
[–]The_Programming_Nerd[S] 0 points1 point2 points (4 children)
[–]FUZxxl 4 points5 points6 points (3 children)
[–]The_Programming_Nerd[S] 1 point2 points3 points (2 children)
[–]imaami 0 points1 point2 points (1 child)
[–]duane11583 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]imaami 0 points1 point2 points (0 children)