Convert INET_ADDR to WSASTRINGTOADDRESSA lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- prodingus · 2022-12-15 15:28 Hello, I have this in windows 7: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program snd_rcv1;       uses  SysUtils, crt, winsock2;const  UDPPackLen = 512;var        i:integer;        sock: Tsocket;        buff: array[0..UDPPackLen - 1] of char;        destaddr: sockaddr_in;      MessageSize: SizeInt;begin        i:=0;         sock := socket(AF_INET, SOCK_DGRAM, ipproto_udp);         destaddr.sin_family := AF_INET;        destaddr.sin_addr.s_addr := inet_addr('1.1.1.10'); // server IP        destaddr.sin_port := htons(1234); // server port         //bind(sock, @destaddr, SizeOf(destaddr));          repeat              inc(i);                buff := inttostr(i) + ' ' + #0;              sendto(Sock, @Buff[0], 12, 0, @destaddr, sizeof(sockaddr_in));                                        if i=20000 then begin                         writeln(inttostr(i)+ ' ' + buff);                        i:=0;                end;              //sleep(0);        until keypressed = true;        readln; end. and as the title says I need help to convert the deprecated inet_addr to wsastringtoaddressa in the line Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i destaddr.sin_addr.s_addr := inet_addr('1.1.1.10'); Also, do I need to initialize with WSAStartup? If then, how? ---------------------------------------------------------------------- Remy Lebeau · 2022-12-15 17:02 Quote from: prodingus on December 15, 2022, 04:28:55 pmI need help to convert the deprecated inet_addr to wsastringtoaddressa Why not to inet_pton() instead? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i inet_pton(AF_INET, '1.1.1.10', @destaddr.sin_addr); Or even getaddrinfo()? Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  hints: ADDRINFO;  addr: PADDRINFO; ZeroMemory(@hints, sizeof(hints));hints.ai_flags := AI_NUMERICHOST;hints.ai_family := AF_INET;hints.ai_socktype := SOCK_DGRAM;hints.ai_protocol := ipproto_udp; if getaddrinfo('1.1.1.10', '1234', @hints, @addr) = 0 then begin  // use addr.ai_addr as needed...  freeaddrinfo(addr);end;  If you really want to use WSAStringToAddressA() then it would look like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  addrlen: Integer; addrlen := sizeof(destaddr);WSAStringToAddressA('1.1.1.10', AF_INET, nil, LPSOCKADDR(@destaddr), @addrlen);  Quote from: prodingus on December 15, 2022, 04:28:55 pmAlso, I think do I need to initialize with WSAStartup? Yes, always, when dealing with socket APIs on Windows. ---------------------------------------------------------------------- prodingus · 2022-12-15 17:47 Unfortunatelly, when using inet_pton, I get identifier not found error. I have search about it and it's a mess; you need to include headers or dlls to do a fraking simple thing. I was using fpsend and it was staightforward, but as I want to use winsock2, the INET_ADDR is not working and thus the need to use WSASTRINGTOADDRESSA. Also, I get the same error for LPSOCKADDR. ---------------------------------------------------------------------- prodingus · 2022-12-15 18:20 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program snd_rcv1;       uses  SysUtils, crt, winsock2;const  UDPPackLen = 512;var        i:integer;        sock: Tsocket;        buff: array[0..UDPPackLen - 1] of char;        destaddr: sockaddr_in;      MessageSize: SizeInt;    addrlen:  integer;    _wsdata: WSAData;begin        i:=0;     WSAStartup($202, _wsdata);    addrlen := sizeof(destaddr);         sock := socket(AF_INET, SOCK_DGRAM, ipproto_udp);            destaddr.sin_family := AF_INET;        destaddr.sin_addr.s_addr := WSAStringToAddressA('1.1.1.10', AF_INET, nil, destaddr, addrlen); // this compiles, without the "@"        destaddr.sin_port := htons(1234); // server port                  repeat                        inc(i);                        buff := inttostr(i) + ' ' + #0;                         sendto(Sock, @Buff[0], 12, 0, @destaddr, sizeof(sockaddr_in));                        if i=20000 then begin                                                      writeln(inttostr(i)+ ' ' + buff);                                i:=0;                        end;                        //sleep(0);              until keypressed = true;                readln; end. still no success. ---------------------------------------------------------------------- Remy Lebeau · 2022-12-15 21:22 Quote from: prodingus on December 15, 2022, 06:47:41 pmUnfortunatelly, when using inet_pton, I get identifier not found error. inet_pton() is a standard C function. I know that FreePascal defined it in the libc package for Posix platforms: https://www.freepascal.org/daily/packages/libc/libc/inet_pton.html Not sure where it is defined for Windows, though.  Worse case, simply declare it in your own code, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function inet_pton(Family: Integer; pszAddrString: PAnsiChar; pAddrBuf: Pointer): Integer; stdcall; external 'Ws2_32.dll'; Quote from: prodingus on December 15, 2022, 06:47:41 pmI have search about it and it's a mess; you need to include headers or dlls to do a fraking simple thing. All of the Winsock functions are DLL functions to begin with. Quote from: prodingus on December 15, 2022, 06:47:41 pmas I want to use winsock2, the INET_ADDR is not working Why not?  What's wrong with it?  It maybe deprecated (because it doesn't support IPv6), but it still exists and is functional for IPv4. Microsoft is not in the habit of removing deprecated APIs. Quote from: prodingus on December 15, 2022, 06:47:41 pmand thus the need to use WSASTRINGTOADDRESSA. But why that specific function?  It is specific to Winsock on Windows only, whereas other functions are more generic and cross-platform. Quote from: prodingus on December 15, 2022, 06:47:41 pmAlso, I get the same error for LPSOCKADDR. FreePascal's winunits-jedi package has that declared in the jwawinsock2 unit (not sure if that is the best place to access it from, in your case): https://www.freepascal.org/daily/packages/winunits- jedi/jwawinsock2/lpsockaddr.html Same as WSAStringToAddressA(): https://www.freepascal.org/daily/packages/winunits- jedi/jwawinsock2/wsastringtoaddressa.html[/code] ---------------------------------------------------------------------- PascalDragon · 2022-12-16 15:02 Quote from: Remy Lebeau on December 15, 2022, 10:22:48 pmQuote from: prodingus on December 15, 2022, 06:47:41 pmUnfortunatelly, when using inet_pton, I get identifier not found error. inet_pton() is a standard C function. I know that FreePascal defined it in the libc package for Posix platforms: https://www.freepascal.org/daily/packages/libc/libc/inet_pton.html The libc unit is only available for the i386-linux target and for nothing else. It only exists for compatibility with Kylix. Quote from: prodingus on December 15, 2022, 04:28:55 pmHello, I have this in windows 7: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program snd_rcv1;       uses  SysUtils, crt, winsock2;const  UDPPackLen = 512;var        i:integer;        sock: Tsocket;        buff: array[0..UDPPackLen - 1] of char;        destaddr: sockaddr_in;      MessageSize: SizeInt;begin        i:=0;         sock := socket(AF_INET, SOCK_DGRAM, ipproto_udp);         destaddr.sin_family := AF_INET;        destaddr.sin_addr.s_addr := inet_addr('1.1.1.10'); // server IP        destaddr.sin_port := htons(1234); // server port         //bind(sock, @destaddr, SizeOf(destaddr));          repeat              inc(i);                buff := inttostr(i) + ' ' + #0;              sendto(Sock, @Buff[0], 12, 0, @destaddr, sizeof(sockaddr_in));                                        if i=20000 then begin                         writeln(inttostr(i)+ ' ' + buff);                        i:=0;                end;              //sleep(0);        until keypressed = true;        readln; end. and as the title says I need help to convert the deprecated inet_addr to wsastringtoaddressa in the line Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i destaddr.sin_addr.s_addr := inet_addr('1.1.1.10'); Also, do I need to initialize with WSAStartup? If then, how? I would simply use FPC's Sockets unit which needs a few adjustments: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program snd_rcv1;uses  SysUtils, crt, Sockets { instead of WinSock2 };const  UDPPackLen = 512;var        i:integer;        sock: Tsocket;        buff: array[0..UDPPackLen - 1] of char;        destaddr: sockaddr_in;        MessageSize: SizeInt;begin        i:=0;         { a socket is created using fpsocket instead of socket }        sock := fpsocket(AF_INET, SOCK_DGRAM, ipproto_udp);         destaddr.sin_family := AF_INET;        { use StrToNetAddr to convert your address to a sockaddr_in }        destaddr.sin_addr := StrToNetAddr('1.1.1.10'); // server IP        destaddr.sin_port := htons(1234); // server port         //fpbind(sock, @destaddr, SizeOf(destaddr));          repeat                inc(i);              buff := inttostr(i) + ' ' + #0;                { use fpsendto instead of sendto }                fpsendto(Sock, @Buff[0], 12, 0, @destaddr, sizeof(sockaddr_in));                 if i=20000 then begin                         writeln(inttostr(i)+ ' ' + buff);                      i:=0;                end;                //sleep(0);      until keypressed = true;        readln; end. ---------------------------------------------------------------------- prodingus · 2022-12-17 12:54 Sorry for the late reply folks, QuoteAll of the Winsock functions are DLL functions to begin with.I am awaare of that; I meant that the inet_pton would require extra lines/steps to include that needed dll. However, it's nice that you show the method on how to declare it. Stupid me, the inet_addr works now, I had to initialize WSAStartup. The more you know.. @PascalDragon, I was using sockets before, exactly as you showed, I just wanted to specifically try winsock2 unit. For this thread's sake, I agree that WSASTRINGTOADDRESSA is pretty much not needed at this point as @Remy_Lebeau said, and inet_addr works just fine. Thank you for your time folks!