I noticed that I should write it like this. Below code seems to work:
console.log(data.toString());
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello!
I am have created a Socket connection code between a C# sharp application and a Node.js application.
My goal is to send a string ("Hello nodejs how are you?") from C# to Node.js. The code do send something to node.js but the information that is received in the console application look like this and is not the string above:
<Buffer 48 65 6c 6c 6f 20 6e 6f 64 65 6a 73 20 68 6f 77 20 61 72 65 20 79 6f 75 3f>
I wonder, what I could be missing in my code?
Please notice that this is a test string. The actual strings that I will send later could have a length between 100 to 100,000 or even more, if anything must be redesigned in the code in order to send large strings?
Thank you!
C# Application code:
private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(connect_sendsocket); thread.IsBackground = true; thread.Start();
}
Socket send_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Socket send_client;
void connect_sendsocket()
{
send_socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3002));
send_socket.Listen(0); send_client = send_socket.Accept();
var buffer = Encoding.UTF8.GetBytes("Hello nodejs how are you?");
send_client.Send(buffer, 0, buffer.Length, 0);
}
Node.js code (javascript code): (server receives "strings" from C# application)
var client3 = new net.Socket();
client3.connect(3002, "127.0.0.1", function () { console.log("client3 Connected"); }) //Connect
client3.on('close', function () { client3.connect(3002, "127.0.0.1", function () { console.log("client3 Connected"); }) }) //Reconnect if connection is closed
client3.on('error', function (ex) { if (ex.message.includes("client3 ECONNREFUSED")) { } }); //error
client3.on('data', function (data) { console.log(data); }); //receive strings
I noticed that I should write it like this. Below code seems to work:
console.log(data.toString());