Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:35:54+00:00 2026-06-05T07:35:54+00:00

I need to write socket in nodejs such as in PHP. In PHP language

  • 0

I need to write socket in nodejs such as in PHP. In PHP language I do something like the following:

$http_request  = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "User-Agent: Picatcha/PHP\r\n";
$http_request .= "Content-Length: " . strlen($data) . "\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "\r\n";
$http_request .= $data;

$response = '';
$fs = @fsockopen($host, $port, $errno, $errstr, 10)
if (FALSE == $fs) {
  die('Could not open socket');
}

fwrite($fs, $http_request);

How can I do thing above in nodejs server?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:35:56+00:00Added an answer on June 5, 2026 at 7:35 am

    Take a look at the documentation for the net module.

    net.connect(arguments...)

    Construct a new socket object and opens a socket to the given location.

    The function returns a Socket.

    There’s a small example snippet on the page to demonstrate its use:

    var net = require('net');
    var client = net.connect(8124, function() { //'connect' listener
      console.log('client connected');
      client.write('world!\r\n');
    });
    client.on('data', function(data) {
      console.log(data.toString());
      client.end();
    });
    client.on('end', function() {
      console.log('client disconnected');
    });
    

    It’s been a while since I wrote PHP, but I would try this as a translation of your code:

    var net = require('net');
    
    var http_request;
    http_request  = "POST " + path + " HTTP/1.0\r\n";
    http_request += "Host: " + host + "\r\n";
    http_request += "User-Agent: Picatcha/PHP\r\n";
    http_request += "Content-Length: " + data.length + "\r\n";
    http_request += "Content-Type: application/x-www-form-urlencoded;\r\n";
    http_request += "\r\n";
    http_request += data;
    
    var client = net.connect(80, host, function() {
      client.end(http_request);
    });
    

    It’s worth nothing that, unless there’s a reason not to, you can use the request method of the http module to make HTTP requests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write following query in Linq to SQL but not sure what
i need to write a socket server using perl which will run on a
I need to write my own SSL socket (CSocket ansestor) with server side certificate
I need to write a daemon that supposed to have one TCP socket and
In socket programming in Linux I need to write data in socket but I
I have the following web server class found here . I need to write
I need to debug a socket connection that my PHP frontend makes to a
I am engaging the following problems: I need to write a server program, will
I have a need to write to a remote machine via a Socket from
I need to write a java program which when executed pushes a command into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.