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 8651837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:09:59+00:00 2026-06-12T14:09:59+00:00

What is the Node.js equivalent of the fgetc() function in php? And how would

  • 0

What is the Node.js equivalent of the fgetc() function in php? And how would I apply it to a socket?

I’m working on a node.js port of this php script:
http://code.google.com/p/bf2php/source/browse/trunk/rcon/BF2RConBase.class.php

Basically it uses sockets to connect to Battlefield 2 based game servers. The function I’m looking at is:

protected function read($bare = false) {
    $delim = $bare ? "\n" : "\x04";
    for($buffer = ''; ($char = fgetc($this->socket)) != $delim; $buffer .= $char);
    return trim($buffer);
}

Its supposed to grab the first line directly from the socket (from what I gather) one character at a time up til the ‘\n’. I’m assuming the output is used for grabbing an encryption salt. The function is called in the socket connect event as part of the code that generates the encrypted password needed to login. Can anyone show me what a Node.js equivalent of this function might look like?

  • 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-12T14:10:00+00:00Added an answer on June 12, 2026 at 2:10 pm

    The docs have an excellent example of how to connect to a server over the network.

    var net = require('net');
    var client = net.connect({port: 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');
    });
    

    Simply change the data event handler to buffer incoming data until you’ve recieved the information you want.

    To do that, you’ll want to know how to use a Buffer.


    Here’s a concrete example of how to buffer data from a stream and parse out messages delimited by a specific character. I notice in the linked PHP that the protocol you’re trying to implement delimts messages with a EOT (0x04) character.

    var net = require('net');
    
    
    var max = 1024 * 1024 // 1 MB, the maximum amount of data that we will buffer (prevent a bad server from crashing us by filling up RAM)
        , allocate = 4096; // how much memory to allocate at once, 4 kB (there's no point in wasting 1 MB of RAM to buffer a few bytes)
        , buffer=new Buffer(allocate) // create a new buffer that allocates 4 kB to start
        , nread=0 // how many bytes we've buffered so far
        , nproc=0 // how many bytes in the buffer we've processed (to avoid looping over the entire buffer every time data is received)
        , client = net.connect({host:'example.com', port: 8124}); // connect to the server
    
    client.on('data', function(chunk) {
        if (nread + chunk.length > buffer.length) { // if the buffer is too small to hold the data
            var need = Math.min(chunk.length, allocate); // allocate at least 4kB
            if (nread + need > max) throw new Error('Buffer overflow'); // uh-oh, we're all full - TODO you'll want to handle this more gracefully
    
            var newbuf = new Buffer(buffer.length + need); // because Buffers can't be resized, we must allocate a new one
            buffer.copy(newbuf); // and copy the old one's data to the new one
            buffer = newbuf; // the old, small buffer will be garbage collected
        }
    
        chunk.copy(buffer, nread); // copy the received chunk of data into the buffer
        nread += chunk.length; // add this chunk's length to the total number of bytes buffered
    
        pump(); // look at the buffer to see if we've received enough data to act
    });
    
    client.on('end', function() {
        // handle disconnect
    });
    
    
    client.on('error', function(err) {
        // handle errors
    });
    
    
    function find(byte) { // look for a specific byte in the buffer
        for (var i = nproc; i < nread; i++) { // look through the buffer, starting from where we left off last time
            if (buffer.readUInt8(i, true) == byte) { // we've found one
                return i;
            }
        }
    }
    function slice(bytes) { // discard bytes from the beginning of a buffer
        buffer = buffer.slice(bytes); // slice off the bytes
        nread -= bytes; // note that we've removed bytes
        nproc = 0; // and reset the processed bytes counter
    }
    
    function pump() {
        var pos; // position of a EOT character
    
        while ((pos = find(0x04)) >= 0) { // keep going while there's a EOT (0x04) somewhere in the buffer
            if (pos == 0) { // if there's more than one EOT in a row, the buffer will now start with a EOT
                slice(1); // discard it
                continue; // so that the next iteration will start with data
            }
            process(buffer.slice(0,pos)); // hand off the message
            slice(pos+1); // and slice the processed data off the buffer
        }
    }
    
    function process(msg) { // here's where we do something with a message
        if (msg.length > 0) { // ignore empty messages
            // here's where you have to decide what to do with the data you've received
            // experiment with the protocol
        }
    }
    

    Completely untested, so there’s likely errors. The main thing to gather here is that as data arrives, you buffer it in memory. Once you find a delimiter character in your buffer, you can process the message.

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

Sidebar

Related Questions

How do I make a Http request with node.js that is equivalent to this
what is the equivalent in scala of java's node.getTagName? for instance, if the function
I'm looking for the javascript equivalent of the php function isset() . I've tried
Is there a field_load() function equivalent to node_load() ? I want to get information
Being totally new into node.js environment and philosophy i would like answers to few
I was wondering if using require() in node.js was the equivalent to lazy loading?
I am working on a node-graph-view similar to Maya's HyperGraph in which I can
So I have the following bit of code in Alloy: sig Node { }
I would like to know if we have something in XSL 2.0, equivalent to
I have an object that is equivalent to this BERT (wrapped for legibility): {Hello,[{1,john,john123},{2,Michale,michale123}]}

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.