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

Discussion in 'C++' started by Abhijit Bhadra, Nov 29, 2004.

  1. 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
    Abhijit Bhadra, Nov 29, 2004
    #1
    1. Advertising

  2. Abhijit Bhadra wrote:

    > 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.

    --
    Surendra Singhi

    www.public.asu.edu/~sksinghi
    Surendra Singhi, Nov 29, 2004
    #2
    1. Advertising

  3. Abhijit Bhadra

    JayXie Guest

    Surendra Singhi <> writes:

    > 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*'".
    JayXie, Nov 29, 2004
    #3
  4. JayXie wrote:

    > Surendra Singhi <> writes:
    >
    >
    >>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*'".


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

    --
    Surendra Singhi

    www.public.asu.edu/~sksinghi
    Surendra Singhi, Nov 29, 2004
    #4
  5. Abhijit Bhadra

    Howard Guest

    "Abhijit Bhadra" <> wrote in message
    news:...
    > 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
    Howard, Nov 29, 2004
    #5
  6. (Abhijit Bhadra) wrote in message news:<>...
    > 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

    Open source rating and billing engine for communication networks.
    Stephan Br?nnimann, Nov 30, 2004
    #6
  7. Abhijit Bhadra

    Dave O'Hearn Guest

    (Abhijit Bhadra) wrote:
    > 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.

    --
    Dave O'Hearn
    Dave O'Hearn, Nov 30, 2004
    #7
  8. Abhijit Bhadra

    Dave O'Hearn Guest

    (Dave O'Hearn) wrote:
    > #define socklet_t int


    That was a typo. It should say,

    #define socklen_t int

    Again, it would've been much better as,

    typedef int socklen_t;

    --
    Dave O'Hearn
    Dave O'Hearn, Dec 1, 2004
    #8
  9. Thanks Dave .
    It worked .

    (Dave O'Hearn) wrote in message news:<>...
    > (Dave O'Hearn) wrote:
    > > #define socklet_t int

    >
    > That was a typo. It should say,
    >
    > #define socklen_t int
    >
    > Again, it would've been much better as,
    >
    > typedef int socklen_t;
    Abhijit Bhadra, Dec 1, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,196
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,613
    Old Wolf
    Jan 20, 2004
  3. Roy Yves
    Replies:
    0
    Views:
    612
    Roy Yves
    Jun 10, 2004
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    invalid conversion from void* to int**

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 14, 2006, in forum: C Programming
    Replies:
    5
    Views:
    2,458
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 16, 2006
  5. AMetnik
    Replies:
    1
    Views:
    3,203
    cluracan
    Mar 28, 2009
Loading...

Share This Page