![]() |
Vista: GetFreePort - Bind gibt nie SOCKET_ERROR
Folgenden Code habe ich bisher verwendet um den nächsten freien Port zu ermitteln:
Delphi-Quellcode:
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 ..
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; Hat jemand eine Idee? |
Re: Vista: GetFreePort - Bind gibt nie SOCKET_ERROR
*push*
|
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?
|
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 00:13 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz