Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi Vista: GetFreePort - Bind gibt nie SOCKET_ERROR (https://www.delphipraxis.net/88364-vista-getfreeport-bind-gibt-nie-socket_error.html)

Zacherl 14. Mär 2007 17:14


Vista: GetFreePort - Bind gibt nie SOCKET_ERROR
 
Folgenden Code habe ich bisher verwendet um den nächsten freien Port zu ermitteln:

Delphi-Quellcode:
function TfrmMain.GetFreePort(ABeginPort: Integer;
  const ATCP: Boolean = false): Integer;
var
  Wsd: TWSAData;
  S: Integer;
  SockAddr: TSockAddrIn;
begin
  Result := ABeginPort;

  if WSAStartup(MAKEWORD(2, 2), Wsd) <> 0 then
    Exit;

  try
    if ATCP then
      S := Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
    else
      S := Socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);

    if S = SOCKET_ERROR then
      Exit;

    SockAddr.sin_family := AF_INET;
    SockAddr.sin_addr.S_addr := inet_addr(pchar('127.0.0.1'));
    SockAddr.sin_port := htons(ABeginPort);

    while bind(S, SockAddr, SizeOf(SockAddr)) = SOCKET_ERROR do
    begin
      inc(ABeginPort);
      SockAddr.sin_port := htons(ABeginPort);
    end;
    closesocket(S);
  finally
    Result := ABeginPort;
  end;
end;
Leider funktioniert dies unter Vista nicht mehr wies scheint. Die Rückkabe von Bind ist immer 0, egal ob der Port belegt ist oder nicht ..

Hat jemand eine Idee?

Zacherl 15. Mär 2007 16:30

Re: Vista: GetFreePort - Bind gibt nie SOCKET_ERROR
 
*push*

Zacherl 16. Mär 2007 13:22

Re: Vista: GetFreePort - Bind gibt nie SOCKET_ERROR
 
Im MSDN steht irgendwas von IP Protokoll .. wie kann ich dem Socket sagen, dass es IPv4 verwenden soll? Bzw macht das überhaupt einen Unterschied?

Christian Seehase 16. Mär 2007 17:22

Re: Vista: GetFreePort - Bind gibt nie SOCKET_ERROR
 
Moin Florian,

dass Du IPv4 verwenden willst, gibtst Du ja schon durch das AF_INET an. Bei IPv6 müsstest Du AF_INET6 nehmen.
Da die Adressen ja ganz anders angegeben werden, würdest Du sonst mit der 127.0.0.1 wohl auch nicht weit kommen ;-)
Localhost ist in IPv6 ::1/128


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz