impossible error in "getsockopt"

Hi all!
I'm starting to teach myself C++ to make a simple software because can't find programmers (at least here in Italy).
Since I'm a kinesthetic person, to learn I have to do things, so I started directly by developing my software (without having ANY knowledge in programming), counting on the help offered from a friend of mine, wich have a degree in software engineering.

I've tried to search here and there on internet to make a routine who recursively checks for internet connection without needing external libraries, and I found the solution in the "getsockopt" function.

And suddenly, a wild problem appears.

My function, althought seems to be written correctly according to the Windows Dev Center and the IBM Knowledge Center, is missing something. Searching on internet didn't really help, even my friend don't know why there is such problem, so I turn the question to anyone who is willing to help me to understand why this is happening.

This is the error I get on the beginning of the word SOL_SOCKET:
main.cpp(row,col): error: expected ‘,’ or ‘...’ before numeric constant

this is the full code:

#ifdef _WIN32
#include <io.h>
#define access _access_s
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif

bool is_connected (int sock)
{
int error_code;
int error_code_size = sizeof (error_code);
int getsockopt (int sock, int SOL_SOCKET, int SO_ERROR, void *restrict &error_code, socklen_t *restrict &error_code_size);
}

is still in developing, but the function should be complete.

can anyone pointing me at the source of the problem? I belive is something stupid I'm not noticing.
Last edited on
This is clearly not the full code, you have no main function. Can you post more code?

I'm starting to teach myself C++ to make a simple software because can't find programmers (at least here in Italy).
Since I'm a kinesthetic person, to learn I have to do things, so I started directly by developing my software (without having ANY knowledge in programming), counting on the help offered from a friend of mine, wich have a degree in software engineering.

If you're completely new to C++ why not start by learning the C++ language and how to code with it. For your info, you are using external libraries, and it seems it's making things more complicated than you need.
Last edited on
int getsockopt (int sock, int SOL_SOCKET, int SO_ERROR, void *restrict &error_code, socklen_t *restrict &error_code_size);

You shouldn't write the types when you call a function.
This site has a page on functions: http://www.cplusplus.com/doc/tutorial/functions/

And that's not a C++ program like H00G0 said. I presume you copy pasted stuff from the internet hoping it would work.

You said that you don't know anything about programing. That's fine. But I would recommend you to learn the basics of C++ before trying to make your own software with C++. I understand that you learn by writing and not reading. I am like that too (but dumber).

But you see, writing programs without having knowledge of what you're writing isn't the right way. You can't copy-paste functions to make your software. That won't take you far. You need to know the basics.

Even if you want to copy functions from the internet you need to know little knowledge about the language.
This site has a tutorial: http://www.cplusplus.com/doc/tutorial/

By the way, don't expect to make commercial software right off the bat. If you have a day job and you need commercial software as part of your day job then you're better off getting help from somebody who knows what they're doing.

But if you want to learn programming then learn it as a hobby, don't sacrifice your day job.
Can you expand your answer?

why you say that is not the full code?
This is everything I've written, surely miss something to be a full program, but keep in mind that my question is about the correct formation of the function.

and why you say that I'm using external libraries?
I'm sure that with this method I don't need to install/add anything that is not already on any given system (maybe I've used the wrong words to describe my purpose?)

The main objective of my project is to learn to code with c++; starting using stuff by-the-book with no real purpose, like "hello world" or such, don't works with me and I lose interest quickly.

Anyway I'm accepting suggestion, if this is, in your opinion, more complex that what is actually needed, can you suggest me another function more efficient?
why you say that is not the full code?
This is everything I've written, surely miss something to be a full program, but keep in mind that my question is about the correct formation of the function.

Well that's what Grime and I are talking about, you need the basics. In a C++ program you should know that there is always a main() function that runs your program. You can't just make a program with one function.

and why you say that I'm using external libraries?
I'm sure that with this method I don't need to install/add anything that is not already on any given system (maybe I've used the wrong words to describe my purpose?)

Basically this function is windows programming not C++, hence why I said you were using external libraries.
For more info see here: https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-getsockopt

The main objective of my project is to learn to code with c++; starting using stuff by-the-book with no real purpose, like "hello world" or such, don't works with me and I lose interest quickly.

Well sadly It's the way you should go. Start with the boring and easy stuff. Do it over and over until you master it, then go up a step, rince and repeat. Learning programming sure is fun when you've got quite a base to build upon, The beginning is a lot of grinding though, but mileage is important, maybe try to pick up a book or there are also many video tutorials on youtube which can help you
Okay maybe this post will help you understand better.

bob404 wrote:
why you say that is not the full code?

Because it doesn't have the structure that a C++ program must have. You don't have a main() function.
See this: http://www.cplusplus.com/doc/tutorial/program_structure/

bob404 wrote:
This is everything I've written, surely miss something to be a full program, but keep in mind that my question is about the correct formation of the function.

I told you why the function call is incorrect. How to use the function I can't tell because I don't know myself. But you can't test your 'function' without having a main() function. That's my point.

bob404 wrote:
and why you say that I'm using external libraries?
I'm sure that with this method I don't need to install/add anything that is not already on any given system (maybe I've used the wrong words to describe my purpose?)

I said that you copy-pasted code, didn't say that you were using an external library. My point being that you still aren't familiar with how functions and C++ work. Then how will you use a function if you don't know how it works?

bob404 wrote:
The main objective of my project is to learn to code with c++; starting using stuff by-the-book with no real purpose, like "hello world" or such, don't works with me and I lose interest quickly.

Doesn't it annoy you that you are able to check internet connections but aren't able to print a simple "hello world"?

What if one day you are supposed to display something on the console? You will eventually end up writing that hello world program.

bob404 wrote:
Anyway I'm accepting suggestion, if this is, in your opinion, more complex that what is actually needed, can you suggest me another function more efficient?

I would suggest learning to use functions before using functions ;p


For using a function you would need to learn how to use a function, and for that you would need to learn how to pass arguments, and for that you would need to learn how to assign values to variables, and for that you would need to learn how to declare variables, and for all of this to work you would need to know how to write the structure of a C++ program, and then maybe in the future you may not remember everything so then you need to learn how to document you program.

What I'm saying is, whether you want to or not, you will eventually end up learning the basics. But if you start with the basics, it will help you better.

Do you eat dessert before main course or main course before dessert?
I get your point, and of course I thanks you both for the advices. I'm not gonna quit my job for became a programmer, don't worry. :)

The main() is missing because is not written yet, I've decided to start to understand how it works function by function. I know that main() is needed, but even without main, using a compiler, if everything is correct you should get no syntax error at least!

The first part, actually, is more like a reminder or a note and can be ignored: it loads only the correct headers for the operative system in which is running, but I don't know if I'll keep it, because I'm planning to develope this thing only for linux. I know from different examples and educational websites (why you say is not c++ by the way?), that the function is written exactly the same in both operative system, but needs different headers. (correct me if this is wrong)

for the function, I've used this as a reference:
https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/getopt.htm

trying to adapt it to my case, I've also read the tutorials that you posted, but I can't figure out by myself where is the problem, the structure of the function should be:

int getsockopt(int socket[the descriptor of the socket], int level[wich is SOL_SOCKET for me], int option_name[wich is SO_ERROR, because I want to know if there is a connection error]
, void *__restrict__ option_value[is where the option data will point]
, socklen_t *__restrict__ option_len[is the lenght of my option data]);

I know I need to initialize the parameters and to specify the data type for the options, but anyway I flip it, even written as a main function, I'm always getting the error "expected ‘,’ or ‘...’ before numeric constant"

I don't get why, since SOL_SOCKET is a parameter of the function: of course is a costant, but as far as I know, it only give instructions to getsockopt on what is the level of interaction (socket, IPV4, IPV6, etc etc)!
...I believe that the information on IBM's website are correct, but maybe there is a typo?
Take a look here:

https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-getsockopt#example-code

It is a complete example for windows (with main()). Unfortunately for linux it would look differently.
can you post what you tried?

If it's the same program then It's because you're not supposed to write the type in the function arguments man.. I told you twice.. - _-
(why you say is not c++ by the way?)

Well as I mentioned before it's a windows function, which you need to #include <windows.h> for. So of course you use C++ to pass arguments to that function, but you're not learning anything about C++ doing So, in fact you must do what Grime said above to be able to do so :

For using a function you would need to learn how to use a function, and for that you would need to learn how to pass arguments, and for that you would need to learn how to assign values to variables, and for that you would need to learn how to declare variables, and for all of this to work you would need to know how to write the structure of a C++ program, and then maybe in the future you may not remember everything so then you need to learn how to document you program.
That's a very narrow definition of "C++", H00G0. So if I write a function and then call it, am I doing C++ at the call site? If I am, then what's the difference between calling my own functions and calling functions someone else (e.g. Microsoft) wrote? If I'm not then you're basically saying only code that never calls any functions is "C++".

Besides, the WinSock library is pretty much just an implementation of BSD sockets. Calling it "Windows programming" is a bit silly.
@helios
I get what you're saying, I was merely trying to insist on the fact that the basics, syntax of the language should be the first thing you focus on when you want to learn a programming language and have no previous experience in programming.
Fair enough.
I think you're going about this the hard way.

When you call socket() to establish a connection, socket() returns -1 if the socket could not be established. socket() also sets the external variable errno to a meaningful value to indicate why the socket could not be established.

The conventional idiom for establishing a socket and checking for an error is as follows.
1
2
3
4
5
6
7
8
9
int create_socket ()
  int rslt;
  rslt = socket ( <socket-params> );
  if (rslt == -1) 
  {  cout << "socket error: " << errno << endl;
      return -1;  // indicate failure to caller
  }
  return rslt;  // return socket descriptor to caller
}

There is no need to call getsockopt() to determine if there was an error.

If your socket() call fails, the <sock-descriptor> will be -1 which will be meaningless to getsockopt().
Last edited on
Topic archived. No new replies allowed.