TCPSendData/index.js

23 lines
534 B
JavaScript

const net = require('net');
const fs = require('fs');
const client = new net.Socket();
fs.readFile('to_send.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
} else {
const to_send = data;
client.connect(2000, '127.0.0.1', () => {
console.log('Connected');
client.write(to_send);
client.end(); // Закрываем соединение после отправки данных
});
}
});
client.on('close', () => {
console.log('Connection closed');
});