public int bytesToInt(byte[] b) {
int i = ((p[n+1]&0xff) << 8) | (p[n]&0xff);
return i;
}
public byte[] intToBytes(int i) {
// ????
}
import net.liveforspeed.insim.*;
public class test implements InSimListener
{
public test()
{
InSim is = new InSim("localhost", 29999, "password");
is.addListener(this);
is.connect();
}
public void inSimVersionReply(InSimEvent e) {
VersionPacket vp = (VersionPacket)e.getObject();
System.out.println(vp.getProduct());
}
public void inSimStateChanged(InSimEvent e) {;;}
}
package lfs.udp;
import java.io.*;
import java.net.*;
public class InsimInit
{
protected static final int TIMEOUT = 60000;
private String returnmsg;
public InsimInit(String password, int port)
{
try
{
// Prepare the 24 byte array and zerofill it.
byte[] message = new byte[24];
for(int i=0; i<message.length; i++) message[i] = 0x00;
// Fill it with initvalues
message[6] = (byte)41; // Flags
message[7] = (byte)1; // NodeSecs
System.arraycopy("ISI".getBytes(), 0, message, 0, 3);
System.arraycopy(password.getBytes(), 0, message, 8, password.length());
// Connect and send
String host = "localhost";
InetAddress address = InetAddress.getByName(host);
DatagramPacket packet = new DatagramPacket(message, message.length, address, port);
DatagramSocket dsocket = new DatagramSocket();
dsocket.setSoTimeout(TIMEOUT);
dsocket.send(packet);
// Wait for answer
byte[] buffer = new byte[2048];
while(true)
{
DatagramPacket answerpacket = new DatagramPacket(buffer, buffer.length);
dsocket.receive(answerpacket);
System.out.println("Receiving: " +
new String(buffer, 0, answerpacket.getLength()));
// Reset the length of the packet before reusing it.
answerpacket.setLength(buffer.length);
}
//dsocket.close();
}
catch (Exception e)
{
System.err.println(e);
}
}
public static void main(String args[])
{
new InsimInit("lepper", 3333);
}
}
struct InSimInit // UDP packet to initialise the InSim system
{
char Id [4]; // ISI + zero
word Port; // Port for UDP replies from LFS (0...65535)
byte Flags; // Bit flags for options - see below
byte NodeSecs; // Number of seconds between NLP or MCI packets (0=none)
char Admin [16]; // Admin password (required if set in LFS host options)
};
package lfs.udp;
import java.io.*;
import java.net.*;
public class UDPSend
{
protected static final int TIMEOUT = 5000;
public static void main(String args[])
{
try
{
String host = "localhost";
InetAddress address = InetAddress.getByName(host);
byte[] message = "ISI00000password".getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, address, 15567);
DatagramSocket dsocket = new DatagramSocket();
dsocket.setSoTimeout(TIMEOUT);
dsocket.send(packet);
// Wait for a answer.. Hopefully.
byte[] buffer = new byte[2048];
DatagramPacket answerpacket = new DatagramPacket(buffer, buffer.length);
dsocket.receive(answerpacket);
String msg = new String(buffer, 0, answerpacket.getLength());
System.out.println("recevied: "+msg);
dsocket.close();
}
catch (Exception e)
{
System.err.println(e);
}
}
}