Recent

Author Topic: SOLVED UDP broadcast listener  (Read 569 times)

samsvl

  • Newbie
  • Posts: 4
SOLVED UDP broadcast listener
« on: November 17, 2018, 05:30:22 pm »
I have an outside thermometer broadcasting temperature to my lan with address 192.168.0.255, port 8088. I am trying to receive the data on my win8.1 pc using a freepascal program below. It runs up till writeln('fpbind ',k), writes zero, and then it seems to hang, while the thermometer is broadcasting. Can anybody help me?

uses sockets;

var
   S: longint;
   SA: SockAddr;
   k: longint;
   buf: array[0..255] of char;
   L: ssize_t;
   len: Tsocklen;
   broadcast: integer;

begin
S := fpSocket(AF_INET,SOCK_DGRAM,0);
writeln('socket ',S);
SA.sin_family := AF_INET;
SA.sin_port := htons(8088);
SA.sin_addr.s_addr := htonl(INADDR_ANY); {StrToHostAddr('');  }
broadcast := 1;
k := fpsetsockopt(S,SOL_SOCKET,SO_BROADCAST,@broadcast,sizeof(broadcast));
writeln('fpsetsockopt ',k);
k := fpBind (S, @SA, sizeof(SA));
writeln('fpbind ',k);
len := sizeof(SA);
L := fprecvfrom(S,@buf[0],255,0,@SA,@len);
write('fprecvfrom ',L); readln;
write(SocketError); readln;
end.
« Last Edit: November 18, 2018, 08:29:25 pm by samsvl »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2030
    • havefunsoft.com
Re: UDP broadcast listener
« Reply #1 on: November 17, 2018, 07:03:20 pm »
Make sure you’re not blocked by the firewall
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

samsvl

  • Newbie
  • Posts: 4
Re: UDP broadcast listener
« Reply #2 on: November 17, 2018, 09:24:01 pm »
Thanks for your reaction. But all is within my lan, so I don't expect firewall problems. Moreover, a simple python implementation on my Raspberry Pi does work.

engkin

  • Hero Member
  • *****
  • Posts: 2121
Re: UDP broadcast listener
« Reply #3 on: November 18, 2018, 01:54:58 am »
I have an outside thermometer
Just curious, which thermometer are you using?

broadcasting temperature to my lan with address 192.168.0.255, port 8088.
instead of 192.168.0.255, try 255.255.255.255

I am trying to receive the data on my win8.1 pc using a freepascal program below.
Which FreePascal version? 32 or 64-bit?
Is your win8.1 64-bit?

It runs up till writeln('fpbind ',k), writes zero, and then it seems to hang, while the thermometer is broadcasting.
This sounds to me like it's waiting to receive data:
Quote
If no incoming data is available at the socket, the recvfrom function blocks and waits for data to arrive according to the blocking rules defined for WSARecv

Can anybody help me?
What is your network setup?
I assume you already used something like WireShark to make sure your computer is receiving the broadcasted temperature?

samsvl

  • Newbie
  • Posts: 4
Re: UDP broadcast listener
« Reply #4 on: November 18, 2018, 01:34:50 pm »
I have an outside thermometer
Just curious, which thermometer are you using?
Homebrew: an RTD connected to the ADC of an ATtiny45 microcontroller which transfers the data via SPI to an ENC28J60 SPI to ethernet converter, connected to my LAN
broadcasting temperature to my lan with address 192.168.0.255, port 8088.
instead of 192.168.0.255, try 255.255.255.255
I'll implement your suggestion, will take some time because the address is hard coded in the ATtiny program
I am trying to receive the data on my win8.1 pc using a freepascal program below.
Which FreePascal version? 32 or 64-bit?
3.0.2 32 bit
Is your win8.1 64-bit?
yes

It runs up till writeln('fpbind ',k), writes zero, and then it seems to hang, while the thermometer is broadcasting.
This sounds to me like it's waiting to receive data:
Quote
If no incoming data is available at the socket, the recvfrom function blocks and waits for data to arrive according to the blocking rules defined for WSARecv
Yes, that's what I think also. But why?

Can anybody help me?
What is your network setup?
I assume you already used something like WireShark to make sure your computer is receiving the broadcasted temperature?
Yes, Wireshark shows the broadcast message, but since there is no client-server communication I can't see if my PC accepts the message.

I just installed the script on my Raspberry PI A connencted to the same LAN, and there it runs without problems. So the issue must be windows related.

Again, thanks for your reaction,

Sam


Bram71

  • New member
  • *
  • Posts: 14
Re: UDP broadcast listener
« Reply #5 on: November 18, 2018, 03:01:20 pm »
Can it be the windows firewall blocking the traffic? Just disable it for a moment to check...
« Last Edit: November 18, 2018, 03:03:03 pm by Bram71 »

lucamar

  • Sr. Member
  • ****
  • Posts: 461
Re: UDP broadcast listener
« Reply #6 on: November 18, 2018, 03:29:49 pm »
Also, while you're (re-)checking the firewall make double-sure that both the IP address and the port are available for UDP to your application. I had a problem like this once and it was because the firewall was blocking the port.

Good luck!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 11..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

samsvl

  • Newbie
  • Posts: 4
Re: UDP broadcast listener
« Reply #7 on: November 18, 2018, 08:27:27 pm »
You all pointed to the firewall, and you all were right !!!
Thanks a lot, problem solved.