invalid conversion from `int*' to `socklen_t*'

A

Abhijit Bhadra

Hi ,
I am using gcc version 3.3.2. While compiling this code
struct sockaddr_in *CBaseSocket::GetSocketName()
{
socklen_t iLen;
cCriticalSocket.Lock();
// Do we have a socket?
if (sSocket == INVALID_SOCKET)
{
cCriticalSocket.Unlock();
return(NULL);
};
cCriticalSocket.Unlock();

iLen = sizeof(struct sockaddr_in);
if (!::getsockname(sSocket, (struct sockaddr *) &sTAddr, &iLen))
return(&sTAddr);

return(NULL);
};

I am getting this error while compiling :

"invalid conversion from `int*' to `socklen_t*'"

Can anyone help me to get rid of this error message ?

Thanks,
Abhijit
 
S

Surendra Singhi

Abhijit said:
Hi ,
I am using gcc version 3.3.2. While compiling this code
struct sockaddr_in *CBaseSocket::GetSocketName()
{
socklen_t iLen;
cCriticalSocket.Lock();
// Do we have a socket?
if (sSocket == INVALID_SOCKET)
{
cCriticalSocket.Unlock();
return(NULL);
};
cCriticalSocket.Unlock();

iLen = sizeof(struct sockaddr_in);
if (!::getsockname(sSocket, (struct sockaddr *) &sTAddr, &iLen))
return(&sTAddr);

return(NULL);
};

I am getting this error while compiling :

"invalid conversion from `int*' to `socklen_t*'"

Can anyone help me to get rid of this error message ?

Thanks,
Abhijit

I feel the error is in your &ilen parameter. You should probably type
cast it. Look into man getsockname and its parameters. You should use
the proper variable type which in this case is socklen_t.
And also please give the line number or better point to the line with
the likely cause of error when you are posting code with errors in it.
 
J

JayXie

Surendra Singhi said:
I feel the error is in your &ilen parameter. You should probably type
cast it. Look into man getsockname and its parameters. You should use
the proper variable type which in this case is socklen_t.
And also please give the line number or better point to the line with
the likely cause of error when you are posting code with errors in it.

But if the error is in the &iLen parameter, the error message should be
"invalid conversion form 'socklen_t *' to 'int *'" not "invalid
conversion from `int*' to `socklen_t*'".
 
S

Surendra Singhi

JayXie said:
But if the error is in the &iLen parameter, the error message should be
"invalid conversion form 'socklen_t *' to 'int *'" not "invalid
conversion from `int*' to `socklen_t*'".

My mistake, I got confused by the OP's using prefix "i" i.e., iLen, I
thought he was passing address of an integer..
 
H

Howard

Abhijit Bhadra said:
Hi ,
I am using gcc version 3.3.2. While compiling this code
struct sockaddr_in *CBaseSocket::GetSocketName()
{
socklen_t iLen;
cCriticalSocket.Lock();
// Do we have a socket?
if (sSocket == INVALID_SOCKET)
{
cCriticalSocket.Unlock();
return(NULL);
};
cCriticalSocket.Unlock();

iLen = sizeof(struct sockaddr_in);
if (!::getsockname(sSocket, (struct sockaddr *) &sTAddr, &iLen))
return(&sTAddr);

return(NULL);
};

I am getting this error while compiling :

"invalid conversion from `int*' to `socklen_t*'"

Can anyone help me to get rid of this error message ?

Thanks,
Abhijit

You shouldn't need it, but you might try casting &iLen as (socklen_t*), just
to see if it works.

(Are you getting any other errors? For example, are you getting an error
that "socklen_t" is not defined? That might also cause this error. Just
guessing, though.)

One other note: there's no reason for all those "struct" specifiers
scattered in your code. That looks like some old C code, not C++. Remove
them. The struct specifier is only needed for the original declaration of
the object, not when referring to objects of that type (or pointers to such
objects). So wherever you have "struct sockaddr", replace it with just
"sockaddr".

-Howard
 
S

Stephan Br?nnimann

Hi ,
I am using gcc version 3.3.2. While compiling this code
struct sockaddr_in *CBaseSocket::GetSocketName()
{
socklen_t iLen;
cCriticalSocket.Lock();
// Do we have a socket?
if (sSocket == INVALID_SOCKET)
{
cCriticalSocket.Unlock();
return(NULL);
};
cCriticalSocket.Unlock();

iLen = sizeof(struct sockaddr_in);
if (!::getsockname(sSocket, (struct sockaddr *) &sTAddr, &iLen))
return(&sTAddr);

return(NULL);
};

I am getting this error while compiling :

"invalid conversion from `int*' to `socklen_t*'"

Can anyone help me to get rid of this error message ?

Thanks,
Abhijit


Once you've `sockaddr_in' defined it should work:

try.cc
======

#include <sys/socket.h>
#include <netdb.h>

sockaddr_in* GetSocketName()
{
static struct sockaddr_in sTAddr;

int sSocket;
socklen_t iLen;
iLen = sizeof(struct sockaddr_in);

if (!::getsockname(sSocket, (struct sockaddr *) &sTAddr, &iLen)) {
return(&sTAddr);
}

return 0;
};

Compiles without any error:
g++-3.3 -g -Wall -c -o try.o try.cc

regards,
Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.
 
D

Dave O'Hearn

struct sockaddr_in *CBaseSocket::GetSocketName()
{
socklen_t iLen;
cCriticalSocket.Lock();

if (sSocket == INVALID_SOCKET)
{
cCriticalSocket.Unlock();
return(NULL);
};
cCriticalSocket.Unlock();

iLen = sizeof(struct sockaddr_in);
if (!::getsockname(sSocket, (struct sockaddr *) &sTAddr, &iLen))
return(&sTAddr);

return(NULL);
};

You shouldn't need that semi-colon on the end.

What platform is this being written for? WinSock has no socklen_t, and
POSIX has no INVALID_SOCKET. It looks like the code was ported from
WinSock to POSIX, and still contains artifacts of WinSock.

My guess is, somewhere your code has this line,

#define socklet_t int

This is causing your 'iLen' variable to actually be an int, instead of
socklen_t. First, change it to a typedef. If it was a typedef, the
compiler would have given you a much more accurate error message.
Then, either delete it, or conditionally do it only on Windows.

HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top