The online racing simulator
InSim Relay request(Solved)
so im using lib from https://github.com/alexmcbride/insimdotnet i followed his exact code from his github wiki but i go no results.
when i run this i get no host list or it doesnt even connect to any host i tired every way possible i know
please correct me if im wrong..

using System;
using System.IO;
using InSimDotNet;
using InSimDotNet.Packets;
using InSimDotNet.Helpers;

namespace LFSInSimRelay
{
class Program
{
// File to log messages to
private static readonly string LogFile = "mso_log.txt";

static void Main(string[] args)
{
Console.WriteLine("Starting LFS InSim Relay...");

InSim insim = new InSim();

// Bind event handlers
insim.Bind<IS_NCN>(NewConnection);
insim.Bind<IS_MSO>(MessageOut); // Add MSO event handler

// Initialize InSim relay
insim.Initialize(new InSimSettings {
IsRelayHost = true
});

Console.WriteLine("InSim relay initialized.");
Console.WriteLine("Selecting host:");

// Select a host
insim.Send(new IR_SEL {
HName = "", // Host to select
Admin = "", // Optional admin pass
Spec = String.Empty // Optional spectator pass
});

Console.WriteLine("Requesting current connection list...");

// Request connection list
insim.Send(new IS_TINY {
ReqI = 1,
SubT = TinyType.TINY_NCN,
});

Console.WriteLine("Monitoring for new connections and MSO messages.");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();

// Clean up
insim.Disconnect();
}

static void NewConnection(InSim insim, IS_NCN ncn)
{
// Output name of each player connected to the host
Console.WriteLine("Player connected: {0}", StringHelper.StripColors(ncn.PName));
}

static void MessageOut(InSim insim, IS_MSO mso)
{
// Strip color codes from message
string cleanMessage = StringHelper.StripColors(mso.Msg);

// Format the message with user info when available
string logMessage;
if (mso.UCID > 0)
{
// Message from a user
logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [USER:{mso.UCID}] {cleanMessage}";
}
else
{
// System message
logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [SYSTEM] {cleanMessage}";
}

// Output to console
Console.WriteLine(logMessage);

// Log to file
try
{
File.AppendAllText(LogFile, logMessage + Environment.NewLine);
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to log file: {ex.Message}");
}
}
}
}

Ah yes. You're using the wrong packet if you're trying to request a list of relay hosts.

This code is used when you are targeting a host that you want to get Relay data from. As specified per documentation.

// Select a host
insim.Send(new IR_SEL {
HName = "", // Host to select
Admin = "", // Optional admin pass
Spec = String.Empty // Optional spectator pass
});

You'll want to use HostListRelay to request data from Relay.

#define IRP_ARQ 250 // Send : request if we are host admin (after connecting to a host)
#define IRP_ARP 251 // Receive : replies if you are admin (after connecting to a host)
#define IRP_HLR 252 // Send : To request a hostlist
#define IRP_HOS 253 // Receive : Hostlist info
#define IRP_SEL 254 // Send : To select a host
#define IRP_ERR 255 // Receive : An error number

You need to send this packet:

struct IR_HLR // HostList Request
{
byte Size; // 4
byte Type; // IRP_HLR
byte ReqI;
byte Sp0;
};

https://en.lfsmanual.net/wiki/InSim_Relay
i have done exactly like you told still not results correct me if im wrong which im proby am Smile
using System;
using InSimDotNet;
using InSimDotNet.Packets;
using InSimDotNet.Helpers;

class Program {
static void Main() {
InSim insim = new InSim();

// Bind handler for HOS (host list) packet.
insim.Bind<IR_HOS>(HostList);

// Initialize connection to InSim Relay
insim.Initialize(new InSimSettings {
Host = "isrelay.lfs.net", // Relay host
Port = 47474, // Default relay port
IsRelayHost = true,
Admin = "", // Not needed for relay
IName = "HostLister" // Name of this client
});

// Request host list.
insim.Send(new IR_HLR { ReqI = 1 });

Console.WriteLine("Requesting host list from InSim Relay...");
Console.ReadLine();
}

static void HostList(InSim insim, IR_HOS hos) {
if (hos.NumHosts == 0) {
Console.WriteLine("No hosts found.");
return;
}

Console.WriteLine("Found {0} host(s):", hos.NumHosts);
foreach (HInfo info in hos.Info) {
Console.WriteLine(
"{0} ({1} / {2} connections)",
StringHelper.StripColors(info.HName),
info.Track,
info.NumConns);
}
}
}

Quote from kristofferandersen :Ah yes. You're using the wrong packet if you're trying to request a list of...

so again i tired but still couldn't do it

using System;
using InSimDotNet;
using InSimDotNet.Packets;

class Program
{
static void Main(string[] args)
{
InSim insim = new InSim();

// Bind the MSO packet handler
insim.Bind<IS_MSO>(MessageMSO);

// Initialize InSim relay
insim.Initialize(new InSimSettings {
IsRelayHost = true,
Admin = "",
Host = "isrelay.lfs.net",
Port = 47474
});

// Send host select packet
insim.Send(new IR_HLR { ReqI = 1 });
insim.Send(new IR_SEL { HName = "Hostname" });
insim.Send(new IS_MTC{UCID = 255, Msg = "This is Testing"});

Console.WriteLine("Connected to relay. Press any key to exit.");
Console.ReadKey();
}

// MSO message handler
static void MessageMSO(InSim insim, IS_MSO packet)
{
Console.WriteLine($"[{packet.UCID}] {packet.Msg}");
}
}

How do you test that it doesn't work? I see you're sending an IS_MTC packet which won't work via InSim Relay unless you connect to the host with the correct admin password using the IR_SEL packet.

Only a subset of InSim packets are allowed to be sent / received:

Quote :Regular insim packets that a relay client can send to host :

For anyone
TINY_VER
TINY_PING
TINY_SCP
TINY_SST
TINY_GTH
TINY_ISM
TINY_NCN
TINY_NPL
TINY_RES
TINY_REO
TINY_RST
TINY_AXI

Admin only
TINY_VTC
ISP_MST
ISP_MSX
ISP_MSL
ISP_MTC
ISP_SCH
ISP_BFN
ISP_BTN

The relay will also accept, but not forward
TINY_NONE // for relay-connection maintenance

For the whole documentation, see the original InSim Relay thread by Victor:

https://www.lfs.net/forum/thread/30740
Quote from Flame CZE :How do you test that it doesn't work? I see you're sending an IS_MTC packet...

Thanks for all the helps guys solved it got a few questions as well the insim relay only works in 2.3.3 InsimDotNet lib from nuget
anything above that version doesnt kris did told me that to use .NET core instead of .NET framework is that true? just asking Smile

1 more thing if im connected to insim relay how can i create commands if Prefix field is ignored when Isrelayhost = true; in insimdotnet lib tho
Can you please say what you did to solve the issue? It's very valueable for others whom may have similair problem.
Quote from rane_nbg :Can you please say what you did to solve the issue? It's very valueable for others whom may have similair problem.

Sure! glad to help others so that they dont suffer Smile i think this code only works if you got insim.net lib 2.3.3 or latest one in https://github.com/alexmcbride/insimdotnet/tree/master

using System;
using InSimDotNet;
using InSimDotNet.Packets;
using InSimDotNet.Helpers;

class Program
{
static void Main(string[] args)
{
InSim insim = new InSim();

// Bind the MSO packet handler
insim.Bind<IS_MSO>(MessageMSO);
insim.Bind<IR_HOS>(HostList);

// Initialize InSim relay
insim.Initialize(new InSimSettings {
IsRelayHost = true,
});

// Send host select packet
insim.Send(new IR_HLR { ReqI = 1 });
insim.Send(new IR_SEL {
HName = "Hostname", // Host to select
Admin = "Passwprd", // Optional admin pass
Spec = String.Empty // Optional spectator pass
});
insim.Send(new IS_TINY { ReqI = 1, SubT = TinyType.TINY_NCN,});
insim.Send(new IS_MTC {UCID= 255, ReqI = 3, Msg = "This is Testing"});

Console.WriteLine("Connected to relay. Press any key to exit.");
Console.ReadKey();
}

static void HostList(InSim insim, IR_HOS hos) {
// Loop through each host connected to the server and print out some details
foreach (HInfo info in hos.Info) {
Console.WriteLine(
"{0} ({1} / {2})",
StringHelper.StripColors(info.HName),
info.Track,
info.NumConns);
}
}

// MSO message handler
static void MessageMSO(InSim insim, IS_MSO packet)
{
Console.WriteLine($"[{packet.UCID}] {packet.Msg}");
}
}

Quote from Flame CZE :How do you test that it doesn't work? I see you're sending an IS_MTC packet...

yeah i didn't notice that i have put admin pass in IR_REL instead of IS_ISI packet so i got confused there IS_MTC packet works now thanks again
Quote from WildGamerz :1 more thing if im connected to insim relay how can i create commands if Prefix field is ignored when Isrelayhost = true; in insimdotnet lib tho

Is there a reason you use InSim Relay to communicate with your server? If you want to create commands etc. you can just connect to your server directly via InSim Uhmm
Quote from Flame CZE :Is there a reason you use InSim Relay to communicate with your server? If you...

oh didnt know that insim can used for connecting to hosts as well i thought it was only for local program lmao
Shrug
okay so after trying that as well i get this response


Unhandled Exception: System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond Insimipiremoved
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at InSimDotNet.TcpSocket.Connect(String host, Int32 port)
at InSimDotNet.InSim.Initialize(InSimSettings settings)
at Program.Main(String[] args) in E:\INSIM FOLDERS(ALL)\Insimapplications\Insim Relay\Program.cs:line 63
You have to allow your inSim host to connect to lfs.net server host. You have to specify allowed IP address.

FGED GREDG RDFGDR GSFDG