最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs & socket io Error: listen EADDRINUSE - Stack Overflow

programmeradmin1浏览0评论

I am giving a try to do a chat with Node.js and socket.io

Now here is my scenario I am using ubuntu 12.04 user and i have folder pp on desktop

inside that i am putted server file server.js

Here is the client:

$(document).ready(function() {
    var urlServer = location.origin + ':8081';
    var socket = io.connect(urlServer);
});

$(document).ready(function() {
    var urlServer = location.origin + ':8081';
    var socket = io.connect(urlServer);

    $("#boton").on('click', function() {
        var mensaje = $("#mensaje").val();
        socket.emit("mensaje", {msg: mensaje});
    });

    socket.on("mensaje", function(msg) {
        console.log("hemos recibido un mensaje", msg);
    });
});

And here the server

var server  = require('http').createServer(),
    sio      = require('socket.io'),
    port    = 8081;
server.listen(port);
var io = sio.listen(server, { log:true });
var channels = {};
io.sockets.on('connection', function (socket) {
    console.log("Cliente conectado");

    socket.on('mensaje', function (msg) {
        console.log(msg);
    socket.broadcast.emit('mensaje', msg);
    });

});
console.log('1- Escuchando en http://localhost:' + port , "");
console.log("");

Now in the same folder I have an html file like

<!DOCTYPE html>
<html>
<head>
    <script src="../jquery.js"></script>
    <script src="../node_modules/socket.io/node_modules/socket.io-client/dist/socket.io"></script>
    <script src="clientechat.js.js"></script>
    <title>Chat con Node</title>
</head>
<body>

    <div id="mensajes"></div>
    <input type="text">
    <input type="submit" id="boton">

</body>
</html>

when i am trying to run the app.js useing node like

node server.js

I am getting the error

   warn  - error raised: Error: listen EADDRINUSE

I try to restart all but it doesn't works

Please tell me what might I am doing wrong.

I am giving a try to do a chat with Node.js and socket.io

Now here is my scenario I am using ubuntu 12.04 user and i have folder pp on desktop

inside that i am putted server file server.js

Here is the client:

$(document).ready(function() {
    var urlServer = location.origin + ':8081';
    var socket = io.connect(urlServer);
});

$(document).ready(function() {
    var urlServer = location.origin + ':8081';
    var socket = io.connect(urlServer);

    $("#boton").on('click', function() {
        var mensaje = $("#mensaje").val();
        socket.emit("mensaje", {msg: mensaje});
    });

    socket.on("mensaje", function(msg) {
        console.log("hemos recibido un mensaje", msg);
    });
});

And here the server

var server  = require('http').createServer(),
    sio      = require('socket.io'),
    port    = 8081;
server.listen(port);
var io = sio.listen(server, { log:true });
var channels = {};
io.sockets.on('connection', function (socket) {
    console.log("Cliente conectado");

    socket.on('mensaje', function (msg) {
        console.log(msg);
    socket.broadcast.emit('mensaje', msg);
    });

});
console.log('1- Escuchando en http://localhost:' + port , "");
console.log("");

Now in the same folder I have an html file like

<!DOCTYPE html>
<html>
<head>
    <script src="../jquery.js"></script>
    <script src="../node_modules/socket.io/node_modules/socket.io-client/dist/socket.io"></script>
    <script src="clientechat.js.js"></script>
    <title>Chat con Node</title>
</head>
<body>

    <div id="mensajes"></div>
    <input type="text">
    <input type="submit" id="boton">

</body>
</html>

when i am trying to run the app.js useing node like

node server.js

I am getting the error

   warn  - error raised: Error: listen EADDRINUSE

I try to restart all but it doesn't works

Please tell me what might I am doing wrong.

Share Improve this question asked Dec 30, 2013 at 14:29 NinjacuNinjacu 1691 gold badge2 silver badges9 bronze badges 1
  • 1 See stackoverflow.com/questions/4075287/… – Romain Meresse Commented Dec 30, 2013 at 14:33
Add a comment  | 

5 Answers 5

Reset to default 10

It means the address you are trying to bind the server to is in use. Do this;

Command:
ps -eaf|grep node

Output:
root     28029 27332  0 14:25 pts/2    00:00:03 node myVNC.js

This shows you the Process id where node is running, which in this case is "28029"

Now kill this process id using;

kill -9 28029

If you cannot start the application at all you might have an application that is already using the given port. Another option is to try and use another port and see if you can start it.

Check @Faisal Ameers answer for linux commands to find program using port.

Check this post for finding the program using the port on windows: https://stackoverflow.com/a/48199/1958344

And this one for OS X: https://stackoverflow.com/a/30029855/1958344

best way to kill server and restart with a kill.sh file

Perhaps it's because you're calling listen(port) twice. The second listen() may be throwing EADDRINUSE because the first listen() is using the port already.

server.listen(port); var io = sio.listen(server, { log:true });

It looks like your server set up is incorrect. Have a look at this example of how you can use socket.io with your http server together: Nodejs & socket io : warn - error raised: Error: listen EADDRINUSE

发布评论

评论列表(0)

  1. 暂无评论