[fpc-pascal] Sending UDP packets windows, linux

Rainer Stratmann RainerStratmann at t-online.de
Fri Mar 20 21:58:49 CET 2009


With 2 Linux PC's this works, but I wanted to connect a Linux and a Windows 
computer.
Would that be possible with UDP packets?

fpsendto blocks the program now in windows.



function socksend( d : pointer ; l : word ) : longint;
begin
 result := fpsendto( inet_socket , d , l , 0 , @inetaddrsend , 
sizeof( inetaddrsend ) );
end;

function sockreceive( d : pointer ; maxlen : word ) : longint;
var msg : longint;
begin
 memclr( @inetaddrrecv , sizeof( inetaddrrecv ) );
 inetaddrrecv.sin_family := AF_INET;
// inetaddrrecv.sin_addr := strtonetaddr( s1 );    // Empfangsadresse
// inetaddrrecv.sin_port := htons( net_port );
 msg := 0;
 {$ifdef linux}
 msg := msg_dontwait;
 {$endif}
 result := fprecv( inet_socket , d , maxlen , msg );
// result := fprecvfrom( inet_socket , d , maxlen , msg , @inetaddrrecv , 
sizeof( inetaddrrecv ) );
end;

function init_socket( s1 , s2 : string ; exeproc : net_execproc ; var erg : 
longint = -1 ) : boolean;
var
 r : boolean;
 nb : dword;
 n : integer;
const net_port = $8000;
begin
 memclr( @inetaddrbind , sizeof( inetaddrbind ) );
 inetaddrbind.sin_family := AF_INET;
 inetaddrbind.sin_addr   := strtonetaddr( s1 );    // Empfangsadresse
 inetaddrbind.sin_port   := htons( net_port );

 memclr( @inetaddrsend , sizeof( inetaddrsend ) );
 inetaddrsend.sin_family := AF_INET;
 inetaddrsend.sin_addr   := strtonetaddr( s2 );    // Sendeadresse
 inetaddrsend.sin_port   := htons( net_port );

 net_execute := exeproc;

 inet_socket := fpsocket( PF_INET , SOCK_DGRAM , 0 );

 if inet_socket >= 0 then begin

  {$ifdef windows}
  nb := 1; // 1=nonblocking, 0=blocking
  n := ioctlsocket( inet_socket , FIONBIO , @nb );
  {$endif}

  inet_bind := fpbind( inet_socket , @inetaddrbind , sizeof( inetaddrbind ) );
  erg := socketerror;
  result := inet_bind = 0;
 end
 else result := false;

 mylo_os_net_ipxok := result;
end;

procedure exit_socket;
begin
 if inet_socket > 0 then closesocket( inet_socket );
end;



More information about the fpc-pascal mailing list