nodejs_socket_file_server

//  tcp_file_client.js
var net = require("net")
var fs = require("fs");
const { EventEmitter } = require('events');
var fileName;
EventEmitter.setMaxListeners(0)
net.createServer(function (socket) {
    // new connection
    fileName = parseInt(Math.random() * 10000000000000);
    fileName = fileName.toString(16).toUpperCase()+ ".png";
    socket.write('Some string');
    socket.on('data',function (data) {
        fs.writeFile(fileName ,data, { flag: "a+" },( error) => {
            console.log("write file error", error)
        }, cb => {
            console.log("write file success", cb);
        }
        );
        socket.on('end', function (data) {
            // connection closed
            console.log("file transfer end once")
        });
    })
}).listen(4001);

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 ggchzzz@163.com

文章标题:nodejs_socket_file_server

字数:118

本文作者:ggchzzz

发布时间:2022-03-21, 00:19:21

最后更新:2023-12-22, 23:17:54

原始链接:https://anska.info/post/4.html

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

github