Jump to content
  • Advertisement
Sign in to follow this  
Drezard

[C++] Simple Problem

This topic is 3705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Now im getting this error:
'accept' : cannot convert parameter 3 from 'sockaddr_in' to 'int *' [/QUOTE] and heres the two main lines of code in question: struct sockaddr_in sin; accept(server, (sockaddr*)&sin, (sin)); Whats wrong? cheers, Daniel

Share this post


Link to post
Share on other sites
Advertisement
MSDN

The 3rd parameter is a pointer to an int containing the length of the struct you're passing in the second parameter. When accept() returns it stores the length of the returned address in that variable.

struct sockaddr_in sin;
int size = sizeof(struct sockaddr_in);
accept(server, (sockaddr *)&sin, &size);

Share this post


Link to post
Share on other sites
Quote:
Original post by Drezard
still dont understand how I would do it?


By passing something of the correct type, with a proper value (just as with any other function call).

Let's look at the error message you got:

Quote:

'accept' : cannot convert parameter 3 from 'sockaddr_in' to 'int *'


Parameter 3 is the last one. You have passed 'sin', which is a 'struct sockaddr_in' (as per the previous line of your code). The function expects pointer-to-int. These are unrelated things, so the compiler "cannot convert" one to the other.

Now, let's look at what the page you were linked to says:

Quote:

SYNOPSIS
...
int accept(int s, struct sockaddr *addr, socklen_t *addrlen)


The "synopsis" section of a "man page" shows you what libraries need to be included, and what the function's prototype is. Here, we see that the last parameter is expected to be a "socklen_t *". This agrees with the compiler, because a socklen_t is simply an int (the typedef socklen_t is used to represent a type which can hold the length of a socket structure).

Quote:

PARAMETERS
...
addrlen

This is a value-result parameter. Initially, addrlen contains the amount of space that addr points to. When returned, it contains the actual length (in bytes) of the address returned. If addrlen is NULL, no information about the remote address of the accepted socket is returned.


The "parameters" section of the man page gives information about what each parameter is supposed to represent, how it will be used, and what should be passed.

This is a "value-result" parameter, meaning that we pass a pointer to some value, and the function will use that pointer to both read that value and overwrite it. The idea is that we pass the address of some local variable, and the local variable will possibly change as a result of calling the function.

In C documentation, it's common to say that "addrlen contains blah blah blah", when actually addrlen is a pointer to a variable which contains that value. What we want to do is make our local variable hold "the amount of space that addr points to" (remember, 'addr' in our case is '(sockaddr*)&sin'), and then pass the address of that variable.

Well, how much space *does* addr point at? We pointed it at a 'struct sockaddr_in' (notice that this is different from 'struct sockaddr'; what's going on is that we are asking the function to treat the pointed-at sockaddr_in as a special kind of sockaddr - if the function expected that we always passed a pointer to a sockaddr and nothing else, then it could figure out the size for itself!), so the amount of space is the size of that struct. In C, we get the size (which must be known at compile-time for code to compile) of a struct with the sizeof() operator.

Thus, we write:


struct sockaddr_in sin;
int size = sizeof(struct sockaddr_in);

accept(server, (sockaddr*)&sin, &size);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!