Recent

Author Topic: how to know there is data in the buffer using sockets unit  (Read 808 times)

arturogr

  • Newbie
  • Posts: 5
how to know there is data in the buffer using sockets unit
« on: December 12, 2023, 03:43:31 am »
Hello:

 I am using windows, and I want to know if there is an option (function), to know if there is data in the in-buffer socket of a connection; so I can call the fpRecv funtion to read the data in the in-buffer.

If I call fpRecv, the program halts, wainting for information to reach the program. But I want to do some thing else in a loop, while wainting for data.

I found something like fpSelect() in the forum, but that function is not recognized in sockets unit, and I do not find any documentation for that:
Code: Pascal  [Select][+][-]
  1. if(fpSelect(FSocket + 1,@RTFD,nil,nil,@Timeout) > 0) then
  2.        begin
  3.          Len := fpRecv(FSocket,@Bytes_In,256,0);
  4.        end;

By the other hand, I have read something like "ioctl", but with no examples to handle the same goal.

thank you.
« Last Edit: December 12, 2023, 03:50:28 am by arturogr »

olly

  • New Member
  • *
  • Posts: 42
Re: how to know there is data in the buffer using sockets unit
« Reply #1 on: December 12, 2023, 04:02:42 am »
Take a look at https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl-net/src/ssockets.pp#L306 lots of cross platform examples there.

For Windows you'll want the winsock2 unit to get access for ioctlsocket and select functions.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8083
Re: how to know there is data in the buffer using sockets unit
« Reply #2 on: December 12, 2023, 09:13:26 am »
Use the FIONREAD ioctl. Example below was originally written for serial data but should be about right

Code: Pascal  [Select][+][-]
  1. FUNCTION MinDataAvailable(handle: INTEGER): INTEGER;
  2.  
  3. begin
  4.  
  5. (* If the handle is invalid (port not open) or there is an error then return < 0. *)
  6.  
  7.   if handle = InvalidSerialHandle then
  8.     result := -1
  9.   else begin
  10.     if fpIoctl(handle, FIONREAD, @result) <> 0 then
  11.       result := -1
  12.   end
  13. end { MinDataAvailable } ;
  14.  

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018