Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My server code: [node.js]
var net = require("net");
console.log("waiting");
var server = net.createServer(function(socket){
	socket.on("data", function(d){
		console.log("[*] we are taliking to somebody");
		console.log("[*] data")
		console.log(d);
		var buf = new Buffer(10);
		d.copy(buf);
		console.log("[*] buffer:")
		console.log(buf);
		console.log(" ");
		socket.write("Server !O! is online!");
	});
});
server.listen(1111, '127.0.0.1');


my client code: [python]
import socket

buffer = "\x41"* 3000

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 1111))

sock.send(buffer)
print sock.recv(40)
sock.close()


then the server prints:
waiting
[*] we are talking to somebody
[*] data
<Buffer 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 ...>
[*] buffer 
<Buffer 41 41 41 41 41 41 41 41 41 41>


I just don't think it's right.

What I have tried:

I don't know what to do with this, but node.js does not crash
Posted
Updated 17-Mar-18 19:27pm

1 solution

Why would it overrun?
JavaScript
d.copy(buf);

This is copying d to buf until buf is full.
node.js is memory managed, it knows the size of buf and thus it knows that it have to stop when buf is full.
 
Share this answer
 
Comments
Member 13536260 18-Mar-18 1:33am    
Yeah, but shouldn't node.js warn anyone that the "d" is too much to be copied on a very small buffer "buf"?
Patrice T 18-Mar-18 2:01am    
Why?
It expect the programmer (you) to know what he does.
You explicitly tell that you want to copy a large string in a fixed size buffer, why would you want a warning just to do what you requested?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900