Forum > Networking and Web Programming

UDP Library/Component Suggestions

(1/1)

lgrfbs:
Hello
Now I've found some inspiration again to program a little.
I need a UDP library/component that works for both Linux and Windows, what should I look for?

sstvmaster:
Here is an overview of network libraries: https://wiki.lazarus.freepascal.org/Networking_libraries

You can use: LNet, Indy or Synapse.

franciscoluiz:
pls try upd super components
https://sourceforge.net/projects/udpsupercomponents/

Warfley:
I wouldn't use a library for UDP. UDP is a very simple protocol (as you can only recieve full datagrams every transmission is only a single read or write).

With the sockets unit this is very easy:

--- 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";}};} ---uses sockets; var  Sock: TSocket;  Addr: PSockAddr_in;  AddrLen: PSizeInt;  AString: String;  DataSize: SizeInt;begin  Sock := fpsocket(AF_INET, SOCK_DGRAM, 0);  if Sock < 0 then    WriteLn('Something  went horribly wrong');  New(Addr);  New(AddrLen);  try    // Send a string stored in AString to HOST_IP at PORT:    // Set address of receiver    Addr^.sin_family := AF_INET;    Addr^.sin_port := HToNS(PORT);    Addr^.sin_addr.s_addr := LongWord(StrToNetAddr(HOST_IP));    // perform sending operation    fpSendTo(Sock,  Pointer(AString), AString.Length, 0, Addr, SizeOf(Addr^);    // Receive Data and write to AString:    SetLength(AString, 512); // 512 is the maximum datagram size in many implementations    DataSize := fpRecvFrom(Sock, AString, AString.Length, 0, Addr, AddrLen);    SetLength(AString, DataSize);    WriteLn('Data received from ', NetAddrToStr(Addr^.sin_addr.s_addr), ': ' . AString);  finally    Dispose(Addr);    Dispose(AddrLen);  end;end;
Or to simplify:

--- 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";}};} ---uses sockets; procedure SendString(Sock: TSocket; const AHost: String; APort: Word, const Data: String);var  Addr: TSockAddr_IN;begin  Addr^.sin_family := AF_INET;  Addr^.sin_port := HToNS(APort);  Addr^.sin_addr.s_addr := LongWord(StrToNetAddr(AHost));  fpSendTo(Sock, Pointer(Data), Data.Length, 0, @Addr, SizeOf(Addr));end; procedure ReceiveString(Sock: TSocket; out SenderHost: String; out SenderPort: Word): String;var  addr: PSockAddr_IN;  addrLen: PSizeInt;  DataSize: SizeInt;begin  New(addr);  New(addrLen);  try    SetLength(Result, 512);    DataSize := fpRecvFrom(Sock, Pointer(Result), Result.Length, 0, addr, addrLen);    SetLength(Result, DataSize);    SenderHost := NetAddrToStr(addr^.sin_addr.s_addr);    SenderPort := NToHs(addr.sin_port);  finally    Dispose(addr);    Dispose(addrLen);  end;end; var  Sock: TSocket;  data: String;  HostIP: String = '127.0.0.1';  HostPort: Word = 1337;begin  Sock := fpSocket(AF_INET, SOCK_DGRAM, 0);  if Sock < 0 then    Exit; // error;  // Send console input to host  ReadLn(data);  SendString(Sock, HostIP, Port, data);  // Receive data:  data := ReceiveString(Sock, HostIP, Port);  WriteLn('Received from ', HostIP, ':', Port, ' ', data);end;
This is pretty much all you need to work with UDP. I don't see any reason to include some third party librar for using something that can be expressed with two functions having each less than 10 lines of code

Navigation

[0] Message Index

Go to full version