B4J Question Receive UDP Broadcast - where to start?

DonManfred

Expert
Licensed User
Longtime User
A friend of mine ask me to help him to get Racing data out of the PS4 Game Project Cars.

The Game is sharing Data via UDP on Port 5606

-------------------------------
UDP streaming support
Fixed a bug where the streaming rate option in the GUI was not working correctly. The option is enabled via the ‘Game play’ menu ‘UDP mode’. This can be selected between off and 1-9. The number represents the UDP streaming data rate as follows:
UDP off
UDP 1 60/sec (16ms)
UDP 2 50/sec (20ms)
UDP 3 40/sec (25ms)
UDP 4 30/sec (32ms)
UDP 5 20/sec (50ms)
UDP 6 15/sec (66ms)
UDP 7 10/sec (100ms)
UDP 8 05/sec (200ms)
UDP 9 01/sec (1000ms)
The stream is sent to a broadcast address (255.255.255.255) on port 5606.
------------------------------------

Can anybody point me into the right direction on how to capture these broadcasts over UDP to use the data later on a webpage (filtered)... I want use B4J as it will run on a pc and b4j will be able to give these data to the webpage then...

But i never worked wih streams at all. Even nothing in past with UDP.
I dont know where to start

Any hints (best to an Tutorial or an working example) will be highly appreciated.
 

DonManfred

Expert
Licensed User
Longtime User
here is a mini example of reading the socket for data
So i guess it should be something like

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim UDPSocket1 As UDPSocket
    Private memo As TextArea
    Dim bconv As ByteConverter
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    UDPSocket1.Initialize("UDP", 5606, 16384)
      
  
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    memo.Text = ""
    MainForm.Show
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Log("> NEW PACKET")
    Log("Host: "&Packet.Host)
    Log("Adress: "&Packet.HostAddress)
    Log("Port: "&Packet.Port)
    Log("Len: "&Packet.Length)
    Log("ToString: "&Packet.toString)
    Log("HEX: "&bconv.HexFromBytes(Packet.Data))
  
    memo.Text = memo.Text & Chr(13)&Chr(10)&msg
    'Msgbox.show("Message received: " & msg, "")
    Log(msg)
End Sub


I cannot test it as i dont have a ps4 and even not the game needed...
But i can give the jar to my friend to test

Edit: 21th Dec:
- Updated the samplecode
- I guess i should get some infos in the log (hopefully)

Will try this evening to get the jar running on his system
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I would think that will get you started, looking at the data that the game sends, the different packet layouts, will be fun for you to decipher :)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Will try this evening to get the jar running on his system
For yesterday i decided to write a small win32 exe with Delphi using TUDPServer.

B4X:
procedure TCarsMain.IdUDPServer1AfterBind(Sender: TObject);
begin
  AdvMemo1.Lines.Add('UDP afterBIND...');
end;

procedure TCarsMain.IdUDPServer1BeforeBind(AHandle: TIdSocketHandle);
begin
  AdvMemo1.Lines.Add('UDP beforeBIND...');
end;

procedure TCarsMain.IdUDPServer1Status(ASender: TObject;  const AStatus: TIdStatus; const AStatusText: string);
begin
  if AStatus = TIdStatus.hsResolving then AdvMemo1.Lines.Add('UDP Resolving');
  if AStatus = TIdStatus.hsConnecting then AdvMemo1.Lines.Add('UDP Connecting');
  if AStatus = TIdStatus.hsConnected then AdvMemo1.Lines.Add('UDP Connected');
  if AStatus = TIdStatus.hsDisconnecting then AdvMemo1.Lines.Add('UDP DisConnecting');
  if AStatus = TIdStatus.hsDisconnected then AdvMemo1.Lines.Add('UDP DisConnected');
  if AStatus = TIdStatus.hsStatusText then AdvMemo1.Lines.Add('UDP StatusText');
  if AStatus = TIdStatus.ftpTransfer then AdvMemo1.Lines.Add('UDP Transfer');
  if AStatus = TIdStatus.ftpReady then AdvMemo1.Lines.Add('UDP Ready');
  if AStatus = TIdStatus.ftpAborted then AdvMemo1.Lines.Add('UDP Aborted');
  if AStatusText <> '' then AdvMemo1.Lines.Add(AStatusText);
end;

procedure TCarsMain.IdUDPServer1UDPException(AThread: TIdUDPListenerThread;
  ABinding: TIdSocketHandle; const AMessage: string;
  const AExceptionClass: TClass);
begin
  AdvMemo1.Lines.Add('Exception '+AMessage);

end;

procedure TCarsMain.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; AData: array of Byte; ABinding: TIdSocketHandle);
var
  fs: TFileStream;
  i: integer;
begin
  AdvMemo1.Lines.Add('UDP Read...');
  AdvMemo1.Lines.Add('PacketSize: '+IntToStr(SizeOf(AData)));
  inc(carsindex);
  fs := TFileStream.Create('Cars'+IntToStr(carsindex)+'.hex', fmCreate);
  for i := 0 to High(AData) do
    fs.Write(AData[i], SizeOf(byte));
  fs.free;

end;
The last one is the most important. It creates a file and write the complete packetdata to it.
This way we got a lot of files created yesterday. Most of them are telemetrydata and one packet was the sParticipantInfoStrings packet which holds infos about Track, Car used and all the names of the drivers in this race

Now time to analyze the packets in Delphi till i can use B4J ;)
 
Upvote 0
Top