
const fs = require('fs');
const net = require('net');

const HOST = '127.0.0.1';
const PORT = 6867;
const JSON_FILE = process.argv[2] || 'cc.active.json';

let jsonData;

try {
    jsonData = fs.readFileSync(JSON_FILE, 'utf8');
    //JSON.parse(jsonData); // Validate JSON before sending
} catch (err) {
    console.error(`Failed to load JSON file: ${err.message}`);
    process.exit(1);
}

const socket = net.createConnection(PORT, HOST, () => {
    console.log(`Connected to ${HOST}:${PORT}`);

    socket.write(jsonData, 'utf8', () => {
        console.log(`Sent ${Buffer.byteLength(jsonData)} bytes`);
        socket.end();
    });
});

socket.on('data', (data) => {
    console.log('Received:', data.toString());
});

socket.on('end', () => {
    console.log('Connection closed by remote host');
});

socket.on('error', (err) => {
    console.error(`Socket error: ${err.message}`);
    process.exit(1);
});