Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem moving socket code from cygwin to linux

4 views
Skip to first unread message

rabbits77

unread,
Nov 20, 2009, 4:06:36 PM11/20/09
to
I have some socket code that compiles without error
with g++ under cygwin but on Linux it gives an
error. I have looked at sys/socket.h on both
platforms and they seem to take the same arguments.
This line:
c=accept(s,(struct sockaddr *) &adr_client,&len_inet);
compiles and runs fine on cygwin. Under linux I get
this compile error:
buffer_manager.cxx:148: error: invalid conversion
from int* to socklen_t*
buffer_manager.cxx:148: error:
initializing argument 3 of int accept(int,
sockaddr*, socklen_t*)

len_inet is simply sizeof(sockaddr)

The cast c=accept(s,(struct sockaddr *) &adr_client,
(socklen_t*)&len_inet);
solves the problem by why isn't this code
immediately
portable between the two platforms?

David Schwartz

unread,
Nov 20, 2009, 4:32:15 PM11/20/09
to
On Nov 20, 1:06 pm, rabbits77 <rabbit...@my-deja.com> wrote:

> I have some socket code that compiles without error
> with g++ under cygwin but on Linux it gives an
> error. I have looked at sys/socket.h on both
> platforms and they seem to take the same arguments.

The type of the last parameter is 'socklen_t *' on both Linux and
Cygwin.

> This line:
>         c=accept(s,(struct sockaddr *)  &adr_client,&len_inet);
> compiles and runs fine on cygwin. Under linux I get
> this compile error:
>         buffer_manager.cxx:148: error: invalid conversion
> from int* to socklen_t*
>          buffer_manager.cxx:148: error:
> initializing argument 3 of int accept(int,
> sockaddr*, socklen_t*)
>
> len_inet is simply sizeof(sockaddr)

Right, but what type is it?

> The cast c=accept(s,(struct sockaddr *) &adr_client,
> (socklen_t*)&len_inet);
> solves the problem

No, it doesn't. It hides the problem. The correct solution to the
problem is to make 'len_inet' of type 'socklen_t'.

> by why isn't this code
> immediately
> portable between the two platforms?

It works by sheer luck on Linux. On Linux, 'socklen_t' and whatever
type 'len_inet' is just happen to be compatible. On Cygwin, they
happen not to be. (As it happens, the 'socklen_t' implementations
different in signedness.)

DS

0 new messages