[fpc-pascal]Multiplexing I/O (Sockets)
James Mills
prologic at useoz.com
Sat Sep 21 02:15:30 CEST 2002
Thank you so much... This hasn't really been in the documentation so it
was hard to figure out. I've only done it once in C++...
Thank you
James
On Fri, Sep 20, 2002 at 09:05:15AM +0100, Georgi Georgiev wrote:
> --- James Mills <prologic at useoz.com> wrote:
> > How do you set sockets to non-blocking?
> >
> > I'm using the unit, Sockets...
>
> with line:
>
> fcntl(conn,F_SetFl,Open_NonBlock);
>
>
> See also attached examples !
>
> =====
> Skelet
> ====== http://skelet.hit.bg
>
> __________________________________________________
> Do You Yahoo!?
> Everything you'll ever need on one web page
> from News and Sport to Email and Music Charts
> http://uk.my.yahoo.com
Content-Description: client.pp
> program client;
>
> uses Sockets, Linux;
>
> const
> MyPort=9999;
> MaxClients=5;
> Err_TryAgain=11;
> Err_InProgress=115;
>
> var
> msg:string;
> rd,k:longint;
>
> procedure Error(s:string);
> begin
> writeln('ERROR: ',s);
> halt;
> end;
>
> function SwapWord(w:word):word;
> begin
> SwapWord:=(w shl 8) or (w shr 8);
> end;
>
> var
> conn:longint;
> SA:TInetSockAddr;
> OK:boolean;
>
> begin
> conn:=Socket(AF_INET,SOCK_STREAM,0 {PF_INET});
> if conn<0 then Error('socket');
> fcntl(conn,F_SetFl,Open_NonBlock);
> with SA do begin
> family:=AF_INET;
> port:=SwapWord(MyPort);
> addr:=((1 shl 24) or 127); { localhost }
> end;
>
> repeat
> OK:=Connect(conn,SA,SizeOf(SA));
> if (not OK) and
> (SocketError=Err_InProgress)
> then writeln('In progress ...');
> if (not OK) and
> ((SocketError<>Err_TryAgain) and
> (SocketError<>Err_InProgress))
> then begin
> writeln('LE=',LinuxError);
> writeln('SE=',SocketError);
> Error('connect');
> end;
> until OK;
>
> writeln('New connection to server !!!');
>
> for k:=1 to 4 do begin
> repeat
> rd:=fdRead(conn,msg[1],1);
> until rd>0;
> msg[0]:=chr(rd);
> writeln('Input=[',msg,']');
> end;
>
> fdWrite(conn,msg[1],ord(msg[0]));
>
> shutdown(conn,2);
> fdclose(conn);
> end.
>
Content-Description: server.pp
> program server;
>
> uses Sockets, Linux;
>
> const
> MyPort=9999;
> MaxClients=5;
> Err_TryAgain=11;
> msg='boza';
>
> var
> listener,conn,addrlen,rd:longint;
> SAl,SAc:TInetSockAddr;
> s:string;
>
> procedure Error(s:string);
> begin
> writeln('ERROR: ',s);
> halt;
> end;
>
> function SwapWord(w:word):word;
> begin
> SwapWord:=(w shl 8) or (w shr 8);
> end;
>
> begin
> listener:=Socket(AF_INET,SOCK_STREAM,0 {PF_INET});
> if listener<0 then Error('socket');
> fcntl(listener,F_SetFl,Open_NonBlock);
> with SAl do begin
> family:=AF_INET;
> port:=SwapWord(MyPort);
> addr:=0; { allow connections from anywhere }
> end;
> if not Bind(listener,SAl,SizeOf(SAl)) then Error('bind');
> if not Listen(listener,MaxClients) then Error('listen');
>
> repeat
> conn:=Accept(listener,SAc,addrlen);
> if (conn<0) and (SocketError<>Err_TryAgain)
> then Error('accept');
> until conn>0;
> fcntl(conn,F_SetFl,Open_NonBlock);
> writeln('New connection !!!');
>
> fdWrite(conn,msg[1],ord(msg[0]));
> repeat
> rd:=fdRead(conn,s[1],20);
> until rd>0;
> s[0]:=chr(rd);
> writeln('Input=[',s,']');
>
> end.
>
More information about the fpc-pascal
mailing list