Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-09

Multiplexing I/O (Sockets)

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Mills
to Georgi Georgiev

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

Georgi Georgiev wrote 21 lines
--- James Mills 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

Show quoted text 71 lines
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 ((SocketErrorErr_TryAgain) and (SocketErrorErr_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

Show quoted text 55 lines
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 (SocketErrorErr_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.

Reply by email

Unknown author
to James Mills
James Mills wrote 5 lines
I remember a while back with C++ I used select and fd's to check if data was available in the socket. Can that be done with pascal? Could someone show me please, I've almost forgotten how it's done...

Select() is available. Just check the documentation of the linux unit for examples.

Michael.

Reply by email

Sebastian Günther
to Unknown author
Show quoted text 13 lines
On Sat, 21 Sep 2002, James Mills wrote: >I remember a while back with C++ I used select and fd's to check if data >was available in the socket. > >Can that be done with pascal? Could someone show me please, I've almost >forgotten how it's done... Select() is available. Just check the documentation of the linux unit for examples.

and, of course, there are libAsync and fpAsync as wrappers for select()... (libAsync is purely procedural, while fpAsync uses classes) As select() can be quite painful in more complex situations, fpAsync will hide most of this complexity, and adds more functionality (e.g. powerful timers). In the future it will be available on more platforms than Linux/Unix.
Both are part of the FPC packages.

- Sebastian

Reply by email