INVALID_SOCKET constant not defined for Linux

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

INVALID_SOCKET constant not defined for Linux

LacaK
Hi,

I am porting application from Windows to Linux. I use Sockets unit.

There is defined on Windows (and also on BeOS, FreeBSD, OS2):
   INVALID_SOCKET = TSocket(Not 0); // or -1
   SOCKET_ERROR = -1;

But not for Linux. Does it means, that result of unsuccessfull call to
Sockets.FPSocket can be other than -1 ?
(in fcl-net is used test for -1 to check invalid socket so I think, that
it is crossplatform ... if yes why isn't INVALID_SOCKET defined also for
Linux in RTL?)

Thanks

-Laco.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: INVALID_SOCKET constant not defined for Linux

Marco van de Voort
In our previous episode, LacaK said:

> I am porting application from Windows to Linux. I use Sockets unit.
>
> There is defined on Windows (and also on BeOS, FreeBSD, OS2):
>    INVALID_SOCKET = TSocket(Not 0); // or -1
>    SOCKET_ERROR = -1;
>
> But not for Linux. Does it means, that result of unsuccessfull call to
> Sockets.FPSocket can be other than -1 ?
> (in fcl-net is used test for -1 to check invalid socket so I think, that
> it is crossplatform ... if yes why isn't INVALID_SOCKET defined also for
> Linux in RTL?)

Under *nix socket calls are just general runtime calls, and they use the
same convention and constants for errorhandling as all other calls.

Under Windows, the winsock part is separate from the C runtime and uses own
constants. Invalid_socket is a winsock constant added to sockets for
convenience (but not portable). Strictly speaking we could add a "platform;"
now to it.

I guess I did't want to encourage winsock (which is the odd one out) coding
in general *nix code.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: INVALID_SOCKET constant not defined for Linux

Michael Van Canneyt
In reply to this post by LacaK


On Tue, 28 Mar 2017, LacaK wrote:

> Hi,
>
> I am porting application from Windows to Linux. I use Sockets unit.
>
> There is defined on Windows (and also on BeOS, FreeBSD, OS2):
>   INVALID_SOCKET = TSocket(Not 0); // or -1
>   SOCKET_ERROR = -1;
>
> But not for Linux. Does it means, that result of unsuccessfull call to
> Sockets.FPSocket can be other than -1 ?
> (in fcl-net is used test for -1 to check invalid socket so I think, that
> it is crossplatform ... if yes why isn't INVALID_SOCKET defined also for
> Linux in RTL?)

Because we don't want to pollute the unix units with windows-isms.

If it is a POSIX defined constant, we can add it, but if it is not:
too bad.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: INVALID_SOCKET constant not defined for Linux

LacaK
In reply to this post by Marco van de Voort
> Under *nix socket calls are just general runtime calls, and they use the
> same convention and constants for errorhandling as all other calls.
As I know "nothing" about Linux, then it is safe to check?:
   socket := Sockets.FPSocket(...);
   if socket = -1 then // invalid socket returned due to error ...

This technique is used in fcl-net which should be cross-platform.
So I am right, when I say that also in Linux invald socket = -1
(in other words: in case of error syscall() returns always -1)

Becuase in other place I see another test "if socket < 0 then ..."
(which may mean that any negative value can be returned)

L.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: INVALID_SOCKET constant not defined for Linux

Giuliano Colla
Il 28/03/2017 12:34, LacaK ha scritto:
> This technique is used in fcl-net which should be cross-platform.
> So I am right, when I say that also in Linux invald socket = -1
> (in other words: in case of error syscall() returns always -1)
>
> Becuase in other place I see another test "if socket < 0 then ..."
> (which may mean that any negative value can be returned)

Most likely this is only because "socket < 0" is faster to write than
"socket = -1" :-)

 From Linux man page for socket:

> RETURN VALUE
>        On success, a file descriptor for the new socket is returned.  On
>        error, -1 is returned, and errno is set appropriately.

Giuliano


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: INVALID_SOCKET constant not defined for Linux

Ladislav Karrach

>> This technique is used in fcl-net which should be cross-platform.
>> So I am right, when I say that also in Linux invald socket = -1
>> (in other words: in case of error syscall() returns always -1)
>>
>> Becuase in other place I see another test "if socket < 0 then ..."
>> (which may mean that any negative value can be returned)
>
> Most likely this is only because "socket < 0" is faster to write than
> "socket = -1" :-)
>
> From Linux man page for socket:
>
>> RETURN VALUE
>>        On success, a file descriptor for the new socket is returned.  On
>>        error, -1 is returned, and errno is set appropriately.
>
Thanks for all repies.
For now I add to my unit:

{$IFDEF UNIX}
// not defined for POSIX; Linux syscall() returns -1 in case of error
const
   INVALID_SOCKET = -1;
   SOCKET_ERROR = -1;
{$ENDIF}

and later I use (in cross-platform code):
   Fsocket := fpsocket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
   if Fsocket = INVALID_SOCKET then ...

-Laco.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: INVALID_SOCKET constant not defined for Linux

Ched
In reply to this post by Giuliano Colla
> Most likely this is only because "socket < 0" is faster to write than "socket = -1" :-)

And is probably faster to execute on most processors which offers a sign flag.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...