Nodejs TCP服务器管理错误

时间:2017-10-02 10:28:17

标签: node.js sockets tcp

我在Debian 8上使用nodejs运行的TCP服务器。

我遇到了TCP端口重用的一些问题,因为我拥有的tcp客户端的tcp堆栈(我无法控制。他们有一个静态tcp源端口)。

我想知道哪个错误处理是正确的:

net.createServer(function(sock) {
    [...]
    sock.on('error', function(e) {
        console.log('Network error ' + e);
        sock.destroy()
    })}).listen(PORT, HOST);

或者

net.createServer(function(sock) {
    [...]
    sock.on('error', function(e) {
        console.log('Network error ' + e);
    })}).listen(PORT, HOST);

1 个答案:

答案 0 :(得分:0)

您不必调用<!DOCTYPE html> <html> <head> <title></title> <meta charset="UTF-8"> <link href="./libs/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <link href="./libs/bootstrap/css/jumbotron-narrow.css" rel="stylesheet"> <link href="./libs/css/styles.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.css"> </head> <body> <dir class="container"> <div class="col-md-12"> <h2>Catàleg de peticions</h2> </div> <div class="col-md-12"> <div class="col-md-3"> <a href="create.php" class=" btn btn-default action-btn">Nou registre</a> </div> <div class="col-md-3"> <button class="btn btn-default action-btn" id="edit-btn">Editar Registre</button> </div> <div class="col-md-3"> <button class="btn btn-default action-btn" id="edit-tree-btn">Editar Arbre</button> <!--a href="create.php?edit-tree=true" class="btn btn-default" id="edit-btn">Editar Arbre</a>--> </div> <div class="col-md-3"> <button class="btn btn-default action-btn" id="delete-btn">Eliminar</button> </div> <div class="col-md-3"> <a href="gestioci/" class="btn btn-default action-btn">Associar CI</a> </div> <div class="col-md-3"> <a href="download.php" download="cataleg_peticions.csv" class="btn btn-default action-btn">Exportar</a> </div> <div class="col-md-3"> <button class="btn btn-default action-btn" id="backup-btn">Backup</button> </div> <div class="col-md-3"> <button class="btn btn-default action-btn" id="restore-backup-btn">Restaurar Backup</button> </div> </div> <div class="col-md-12" style="margin-top: 10px"> <form action="load_csv.php" method="post" enctype="multipart/form-data" id="upload-form"> <div class="col-md-6"> <input type="file" name="csv-file" id="csv-file"> </div> <div class="col-md-3 offset-md-6"> <input class="btn btn-default" type="submit" name="submit" value="Importar"> </div> </form> </div> </dir> <table class="table-bordered table-hover table-fixed" id="mytable"> <thead> <tr> <th>Confimar</th> <th>Árbol Completo</th> <th>Títol</th> <th>Descripció</th> <th>Code</th> <th>Parent Code</th> </tr> </thead> <?php if ($stmt = sqlsrv_query($conn,$query)) { while($tmp = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) { echo setTableFile($tmp); } } ?> </tr> </table> </div> <script src="./libs/jquery-3.2.1.min.js"></script> <script src="./libs/bootstrap/js/bootstrap.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script> <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script> <script src="./libs/script.js"></script> </body> </html> ,因为它是套接字本身遇到错误情况所以它会自行清理(如果您的应用中的其他内容导致错误,您将调用它,并且你想快速关闭套接字。)

但是,您缺少另一个错误处理程序,即连接到服务器本身的错误处理程序。这是sock.destroy()失败后调用的那个:

listen()