Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-03

INVALID_SOCKET constant not defined for Linux

7 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Marco van de Voort
to LacaK

In our previous episode, LacaK said:

Show quoted text 11 lines
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.

Michael Van Canneyt
to LacaK: I am porting application from Windows to Linux. I use Sockets unit. There is defined on…
LacaK wrote 13 lines
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.

LacaK
to Marco van de Voort: In our previous episode, LacaK said: Under *nix socket calls are just general runtime…
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.

Giuliano Colla
to LacaK

Il 28/03/2017 12:34, LacaK ha scritto:

Show quoted text 6 lines
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

Ladislav Karrach
to Giuliano Colla
Show quoted text 16 lines
> 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.