0

My code is written on TCP Node JS and its working fine except some multithreading. The other socket is not connected to the server during the first socket transaction for E.g

Code:-

// Start a TCP Server
var clientServer = net.createServer(function (socket) {

  socket.setEncoding('UTF-8');
  socket.on('data',function(data) {

        if(tryParseJSON(data) === false){

            if(data.length != 0){
                transferDataRequest.call({},data,socket);
            }else{

                response = JSON.stringify({Message:'Invalid JSON Object',Response:'Error',result:data});
                response = response+addition_response;
                socket.write(response);
            }

        }else{

            var output = JSON.parse(data);
            for(var i in output){
                switch(i){
                    case "newConnection":
                        newConnection.call({},output[i],socket);
                    break;
                }   
            }
        }
  });




}).listen(3000,"10.1.28.61");
// Put a friendly message on the terminal of the server.
console.log("Sever listing at 3000 port\n");



newConnection = function(data,socket){

    var i = 0;
    do {
        i++;
        console.log(1);
    }
    while (i = Math.random());
}

enter image description here Your response in this regard it will be highly appreciated

1 Answer 1

2

Javascript code is executed in a single thread in node.js. Your newConnection function blocks this thread (Math.random() may return 0, but probability is very low).

Sign up to request clarification or add additional context in comments.

1 Comment

its an example do you have any alternate approach in this regard please reply me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.