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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:13:36+00:00 2026-06-08T12:13:36+00:00

I’m trying to write a quick and dirty tcp server and have trouble with

  • 0

I’m trying to write a quick and dirty tcp server and have trouble with delimiters. Per this question I’m buffering incoming data and looking for delimiters (in this case ‘\r\n’. However, when I telnet in and send a message

foo\r\nbar

The below server doesn’t recognize the delimiter in the middle, but does see a \r\n at the end- I was under the impression telnet send a \n only. When I send messages via a ruby script, no delimiter is recognized anywhere, even if \r\n is present in the message.

Is there some js string handling behavior that I need to be awared of?

var net = require("net");
var http = require('http');

var HOST = '127.0.0.1';
var PORT = 6969;
var TCP_DELIMITER = '\r\n';
var TCP_BUFFER_SIZE = Math.pow(2,16);
net.createServer(function(sock) {
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    // To buffer tcp data see:
    // https://stackoverflow.com/questions/7034537/nodejs-what-is-the-proper-way-to-handling-tcp-socket-streams-which-delimiter
    buf = new Buffer(TCP_BUFFER_SIZE);  //new buffer with size 2^16

    processTCP = function(msg) {
        // process messages
        console.log("processTCP: "+msg);
    }

    // socket handlers
    sock.on('data', function(data) {
        // look for separator '\r\n'

        console.log("data='"+data+"'");

        data = data.toString('utf-8');
        if(data.indexOf(TCP_DELIMITER) == -1) {
            console.log("1 PART MSG, INCOMING");
            buf.write(data.toString()); // write data to buffer
        } else {
            parts = data.toString().split(TCP_DELIMITER);
            console.log("Parts: "+parts);

            if (parts.length == 2) {
                console.log("2 PART MSG, INCOMING");
                msg = buf.toString() + parts[0]; // and do something with message
                processTCP(msg);
                buf = (new Buffer(TCP_BUFFER_SIZE)).write(parts[1]); // write new, incomplete data to buffer
            } else {
                console.log(parts.length+" PART MSG, INCOMING");
                msg = buf.toString() + parts[0];
                processTCP(msg);
        for (var i = 1; i <= parts.length -1; i++) {
                    if (i !== parts.length-1) {
                        msg = parts[i];
                        processTCP(msg);
                    } else {
                        buf.write(parts[i]);
                    }
        }
            }
        }

        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        // Write the data back to the socket, the client will receive it as data from the server
        sock.write('You said "' + data + '"');   
    });
    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
  • 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-08T12:13:37+00:00Added an answer on June 8, 2026 at 12:13 pm

    Actually, there’s nothing wrong with the code you posted, it does work properly.

    When you send the string “\r\n” using telnet, it would simply pass it as a literal so I didn’t really understand that example (perhaps you pasted it?).

    Also, I commented out the console.log call in processTCP.

    Test it using this code (you can just run it directly from node.js‘s console):

    require('net').connect(6969, '127.0.0.1').write('segment1\r\nsegment2');
    

    The results I got on the server side:

    Server listening on 127.0.0.1:6969
    CONNECTED: 127.0.0.1:34960
    Parts: segment1,segment2
    2 PART MSG, INCOMING
    DATA 127.0.0.1: segment1
    segment2
    CLOSED: undefined undefined
    

    Clearly, your code works just fine.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.