Skip to content

Instantly share code, notes, and snippets.

@bw2012
Created September 3, 2023 11:15
Show Gist options
  • Save bw2012/956bf456ea71886e56559dc74a2a1eca to your computer and use it in GitHub Desktop.
Save bw2012/956bf456ea71886e56559dc74a2a1eca to your computer and use it in GitHub Desktop.
pascal udp server
program UdpServer;
uses Sockets;
const
UDPPackLen = 512;
var
MasterSocket : Longint;
Addr : TInetSockAddr;
RecvLength : Int64;
buff: String;
FromAddr: sockaddr_in;
FromAddrLen: Integer;
begin
writeln ('Start program');
MasterSocket := fpSocket (AF_INET, SOCK_DGRAM, 0);
if MasterSocket < 0 then
begin
writeln ('Program exit: create socket error');
Exit;
end;
Addr.sin_family := AF_INET;
Addr.sin_port := htons(5000);
Addr.sin_addr.s_addr := INADDR_ANY;
if fpbind (MasterSocket, @Addr, SizeOf (Addr)) < 0 then
begin
writeln ('Program exit: bind socket error');
Exit;
end;
while true do
begin
SetLength(buff, UDPPackLen);
RecvLength := fprecvfrom (MasterSocket, @buff[1], Length(buff), 0, @FromAddr, @FromAddrLen);
SetLength(buff, RecvLength);
//ClientAddr := NetAddrToStr(FromAddr.sin_addr);
//ClientPort := NToHs(FromAddr.sin_port);
WriteLn(buff);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment