TCP Communication Behaviour - c#-4.0

I am sending data asynchronously over a TCP Socket. I am currently connected to a SMSC simulator, on my local computer, just to check that all the packets are created correctly, before connecting to the real thing.
I am only sending a PDU once, and the SMSC receives it perfectly, and generates a response PDU and sends it back, but after that, an error Message pops up on the simulator specifying that it cannot receive 100 messages. The problem is that I only send it once, there is no loop running that constantly sends the messages, and I have debugged and checked that it send only once.
I Think that the problem might be with the creation of the PDU. I start by creating an byte array of size 1024, and then filling as necessary. When filled up, it does not use the entire space of the array. So I am thinking that when the simulator receives it, retrieves the data from the array, and then it reads the '0' bytes in the array after the actual data as a new message, since it gives me a response message saying that the data is not valid.
Is there anyway to avoid this, or am I just missing something here? According to me, when receiving value in byte array, you should only use the necessary encoding to retrieve the data, and the rest of the '0' bytes should be ignored?
Sorry if my question is vague.

The problem was indeed the 0bytes in the array.
I solved it by removing the 0 bytes from the array, after reading an article posted on Stack Overflow:
Here is the Solution:
private byte[] CleanArray(byte[] array)
{
int i = array.Length - 1;
while (array[i] == 0)
{
i--;
}
byte[] cleanedArray = new byte[i + 1];
Array.Copy(array, cleanedArray, i + 1);
return cleanedArray;
}

Related

How to "undo" `bytes()` encoding for a string?

In making a socket chat, I want to do certain behaviour depending on the message string.
"""
c is a client socket
RECV_SIZE is the receiving buffer size
"""
message = c.recv(RECV_SIZE)
if message == bytes("/quit", "utf8"):
# logic specific to quitting
This is using /quit as an example string. But is there a way to decode the message so I can access it in plain text? If message is printed, it's a garbled mess, I'd like to print the actual message decoded. Essentially I want the reverse of bytes(), or "unbytes" if that makes sense. So instead I would do something like:
message = unbytes(c.recv(RECV_SIZE), "utf8")
if message == "/quit":
# logic specific to quitting
You are looking for the decode() method for bytes.
message = c.recv(RECV_SIZE).decode('utf8')

Node.js: Race condition when receiving data on tcp socket

I'm using the net library of Node.js to conect to a server that is publishing data. So I'm listening for 'data'-events on client side. When the data-event is fired, I append the received data to my rx-buffer and check if we got a complete message by reading some bytes. If I got a valid message, I remove the message from the buffer and process it. The source code looks like:
rxBuffer = ''
client.on('data', (data) => {
rxBuffer += data
// for example... 10 stores the message length...
while (rxBuffer.length > 10 && rxBuffer.length >= (10 + rxBuffer[10])) {
const msg = rxBuffer.slice(0, 10 + rxBuffer[10])
rxBuffer = rxBuffer.slice(0, msg.length) // remove message from buffer
processMsg(msg) // process message..
}
})
As far as I know that the typical way. But... what happens if the data event fired multiple times? So, imagine I'm getting a data event and while I append the data to my rx-buffer I'm getting the next data event. So the "new" data event will also append the data to the rxBuffer and starts my while-loop. So I've two handlers that are processing the same messages because they share the same rx-buffer. Is this correct?
How can I handle this? In other languages I'd say use something like a mutex to prevent multiple access to the rx-buffer... but what's the solution forjs?!?! Or maybe I'm wrong and I'm never getting multiple data-events while one event is still active? Any ideas?
JavaScript is single threaded. The second event will not run until the first one either completes or blocks, the latter of which could presumably happen in your processMsg(). If that's the case, multiple executions of processMsg() could be interleaved. If they aren't changing any global data (rxBuffer included), then you shouldn't have a problem.

Is there a way to keep node js cluster send() messages in order

Using some added sequence checks below, I see that messages are sometimes arriving out of order and that breaks the code.
I am thinking I must queue up out-of-order messages upon receive to make sure things get processed in order.
Is this just the nature of NodeJS ?
// In the master process:
msg.sequence = next_sequence[i]++;
worker[i].send(msg)
// In worker(s):
process.on("message",handler);
....
var last_sequence = 0;
function handler(msg){
if ( last_sequence + 1 != msg.sequence ) console.log(...);
last_sequence = msg.sequence;
After using send(JSON.stringify(msg)) and JSON.parse when receiving, the behavior seems more deterministic and message sequence numbers are in order.
So it seems that send() does not immediately copy the data and it can still be changed some short while after calling send().
Can anyone confirm this ?

Node.JS - Deal with DNS packet

I want read DNS packet catch by UDP server event on.message
How can I read fix size data
packet data manage like
I want read all field of DNS packet by separately by size.
--Read nodejs buffer object bit by bit. (specific size of bit)
var s = dgram.createSocket('udp4');
s.bind(53, function() {
});
s.on('message',function(msg,rinfo){
console.log("Length = "+msg.length);
console.log(msg.toString('binary'));
console.log(msg);
console.log("-----------------------------------------------------------------------");
});
how can achieve every field data?
with buffer of on.message param.
Thanks.
Please check https://www.npmjs.org/package/native-dns-packet
It provides exactly what you need:
Packet.parse(buffer) returns an instance of Packet
Packet.write(buffer, packet) writes the given packet into the buffer, truncating where appropriate

Can MessageChannel overflow

I am working on an AS3 project in FDT6. I am using the lastest FLEX 4.6 and AIR 3.7.
I have a worker.swf file that is embedded into the main application to do threading work with.
I am using the MessageChannel class to pass information between the two.
In my main class I have defined
private var mainToWorker:MessageChannel;
private var workerToMain:MessageChannel;
mainToWorker = Worker.current.createMessageChannel(worker);
workerToMain = worker.createMessageChannel(Worker.current);
on the mainToWorker I only ever send messages. In these messages I send a byte array of information. The information is an object that contains a 'command' property and a 'props' property. Basically acting like a function call. The command is a function name and the props is an object that contains data for that function.
mainToWorkerMutex.lock();
mainToWorker.send(ByteArrayUtils.ObjectToByteArray({command:"DoSomething", props:{propA:1,propB:7}}));
mainToWorkerMutex.unlock();
The same occurs for the workerToMain var except I only send byte data that contains the 'message' and 'props' parameters.
workerToMainMutex.lock();
workerToMain.send(ByteArrayUtils.ObjectToByteArray({command:"complete", props:{return:"result"}}));
workerToMainMutex.unlock();
As a sanity check I make sure that the message channels are getting what they should.
It is working fine when I build it in FDT, however when it is built using an ANT script through flash builder I am sometimes getting the 'command' events coming back through in the workerToMain channel.
I am sending quite a lot of data through the message channel. Is it possible that I am overloading it and causing a buffer overflow into the other message channel somehow? How could that only be happening in FB?
I have checked my code many times and I am sure there is nothing in my own code that is sending that message back.
I had similar issue. When sending many bytearrays using channels sometimes things i received was not things i've actually sended. I had 4 channels (message channel to worker, message channel to main, data channel to worker, data channel to main).
I've noticed that data channel to main was affecting message channel to worker. When i turned off data channel to main - message channel to worker stared working just fine :D...
They have a big issue there with sending byte arrays it seems.
But what helped me was using shareable (at first it was not shareable) bytearray for communication via channels, but only for communication, as soon as i am receiving such bytearray i'm copying it to another byte array and parsing a copy.
This removed the problem (made quite hard stress tests there)...
Cheers
P.S. I'm also using static functions (like your ByteArrayUtils) to create bytearray's used for communication, but it seems fine, even made tests using non static functions.
So, it looks like I have found the issue. Looks like it's the ByteArray that is doing it.
ByteArray.toString() is basically sometimes mangles your data meaning you can't really trust it.
http://www.actionscript.org/forums/showthread.php3?t=155067
If you read the comment by "Jim Freer" he mentions how strings sometimes do this.
My solution was to switch to using a JSON encoded string instead of ByteArray data in the message channel. The reason I was using bytearray data to begin with is because I wanted to preserve class definition information, which JSON doesn't do.

Resources