Forum > Networking and Web Programming

Easiest way to send UDP packets

<< < (4/4)

TRon:

--- Quote from: dseligo on May 29, 2024, 10:47:56 am ---You example gives error:

--- End quote ---
Thank you very much for testing and the feedback dseligo.

That is strange... according to the fpc documentation it should retrieve the length.


--- Quote ---fpRecvFrom receives data in buffer Buf with maximum length Len from socket S. Receipt is controlled by options in Flags. The location pointed to by from will be filled with the address from the sender, and it's length will be stored in fromlen. The function returns the number of bytes received, or -1 on error. AddrLen.

--- End quote ---

So, it is/was my understanding I did not have to set slen ... but perhaps my interpretation of the bold underlined text is wrong ?

edit:
Indeed according to man:

--- Quote ---The argument addrlen is a value-result argument, which the caller should initialize before the call to the size of the buffer associated with src_addr, and modified on return to indicate the actual size of the source address.

--- End quote ---

So, I stand corrected. And also a good point to show that checking results is important  ;) (though strangely the original code worked without errors for me on linux 64 bit)

ChrisTG:
A small update from my side: I was able to succefully generate and send the WOL-packet using Warfley's neat unit.
Here's my updated code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program udp_send; {$mode objfpc}{$H+} uses SimpleSockets, sysutils, strutils; var  Sock: TSocket;  HostIP: PChar = '192.168.1.255';  HostPort: Word = 7;  AString: String = chr(255) + chr(255) + chr(255) + chr(255) + chr(255) + chr(255);  MAC: String = '112233445566';  MACchr: String;  i: Integer; begin  i := 1;  MACchr := '';  while i < Length(MAC) do  begin    MACchr := MACchr + chr(StrToInt('$'+MAC[i]+MAC[i+1]));    i := i + 2;  end;   MACchr := DupeString(MACchr, 16);  AString := AString + MACchr;   Sock := UDPSocket(stIPv4);  try    SendStrTo(Sock, IN4Address(HostIP), HostPort, AString);  finally    CloseSocket(Sock);  end; end.              
Two points are interesting here:
1) Somehow no socketopts are needed for sending broadcast-packets, even though there is no code to setting this in Warfley's unit.
2) My native approach using the "Sockets"-unit did not work, but this wrapper-unit does. So there must be some magic in Warfley's code somewhere. I think I'll dig through it to understand the mistake(s) in my former tries.

Anyway, thank you all for your input here, especially Warfley for creating this useful unit.

MarkMLl:

--- Quote from: ChrisTG on June 03, 2024, 08:31:29 pm ---1) Somehow no socketopts are needed for sending broadcast-packets, even though there is no code to setting this in Warfley's unit.

--- End quote ---

From memory, I don't think Windows is as strict at rejecting this sort of thing.

However even the unix approach is, by now, missing an important point: for at least the last 20 years it has been relatively easy for a computer's privileged user to get it connected directly to the Internet, i.e. without one or more NAT layers. To assume that every one of those billion or so users is a "responsible netizen" is unrealistic, and sooner or later we are going to see fundamental protocol changes to accommodate that. It is unlikely that those changes will leave network programming anything like as easy as it has been for the last 40+ years.


--- Quote ---2) My native approach using the "Sockets"-unit did not work, but this wrapper-unit does. So there must be some magic in Warfley's code somewhere. I think I'll dig through it to understand the mistake(s) in my former tries.

--- End quote ---

If I had a Windows- and preferably a WoL target- system conveniently to hand I'd take a look at that.

MarkMLl

Navigation

[0] Message Index

[*] Previous page

Go to full version