Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "Error socket hang up" response?

I tried to scrape a website. And I am using the library https://github.com/request/request, but I get this error:

Error: socket hang up
    at createHangUpError (http.js:1476:15)
    at Socket.socketOnEnd (http.js:1572:23)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickDomainCallback (node.js:463:13) +0ms

What is wrong here? I also tried to re-install the package, but I get same error... help is appreciated.

like image 851
Widada Avatar asked Apr 21 '15 08:04

Widada


People also ask

What is socket hang up error?

Socket hang up error is thrown in two cases. When you are a client & When you are a server/proxy When you, as a client, send a request to a remote server, and receive no timely response. Your socket is ended which thrown this error. You should catch this error and decide how to handle it: whether retry the request, queue it for later, etc

What are the common causes of Windows socket errors?

Few of them are: • Malicious software, spyware or virus • Windows registryproblems Windows socket errors are going to take place frequently unless you clean your computer regularly. How to fix windows socket error? Try the following two solutions: Solution#1

Why is the server I'm connecting to not sending content-length?

The server I'm connecting to is not sending a Content-Length header in the response, in which case according to the HTTP spec the server should close the stream after sending all data. I suspect this is being misinterpreted as a socket hang up. Making the same request with cURL works fine. Sorry, something went wrong. Sorry, something went wrong.

What is the function of Windows socket?

Just like an adapter which connects two things that directly cannot be connected, Windows socket plays a role of connecting bridge between software and network. If it gets corrupt or doesn’t work, your windows software might not be able to access internet.


2 Answers

I recently got this socket hang up problem. I researched for a few days until I found a solution that worked for me. So, maybe this can help.

It worked for me to add the http(s)Agent property with keepAlive: true in creating the http client. Here's an example of what it might look like:

const http = require('http')
http.request({
        method: 'GET',
        agent: http.Agent({keepAlive:true}),
        host: 'www.google.com',
    })

This property is responsible for managing the sockets for client-side http connections. The justification for using this property can be found here.

I also found an answer on the stackoverflow that also covers the subject, here.

So, I hope this helps.

like image 153
cazuzaneto Avatar answered Sep 22 '22 15:09

cazuzaneto


check the bellow link for the source code

https://github.com/joyent/node/blob/ba048e72b02e17f0c73e0dcb7d37e76a03327c5a/lib/_http_client.js#L164

or check the following question

NodeJS - What does "socket hang up" actually mean?

like image 37
hussam Avatar answered Sep 23 '22 15:09

hussam