Problems receiving packets from socket  
Author Message
Jordi González





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

Hello! I'm new in this forum and in this awesome programming language. I came from Java and I'm actually doing a speed learning in C#.

We actually have a single problem that it drives us crazy. We are connecting a serial old device in a TCP/IP terminal server (with a special RJ45-RS232 cable). In fact, the device receives the petitions from the client and sends them the requests to its sender. The device, I said, is and old device that sends the values of patient parameters that we ask it to send.

We, then, create a Socket from our computer to the terminal server (is a Lantronix Multiport device server) and make the process of connect to the device, ask the parameters and configure which parameters we want to receive and it starts to send us the values. But, when we configure some cooked wave parameters, we lose the first packet that the device sends us, that is, the packet of data that haves the identifier that the device assigns to every parameter we configure. If we only configure numeric parameters (parameters that all his values are sending in an interval of 200 ms) we don't have any problem receiving it. So... is the frequency of the wave parameters high enough to make that lose of data

I were trying all SocketOptions and we don't make it successfull. Thank you.

Best Regards:



.NET Development16  
 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

Can You share Send and Receive code So we can know what's wrong with that

Best Regards,



 
 
Jordi Gonzalez





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

The code to send-receive is very simple, I'll share the part of sending/receive packets:

// Initialize: We tried with TcpClient and with Socket itself.
TcpClient _socket = new TcpClient();
NetworkStream _nStream;
_socket.Connect(newIP, newPort);
if (_socket.Connected)
{
_nStream = _socket.GetStream();
}

We have a thread reading/sending packets to the socket. We send the packet byte to byte and receive with the same process, because the size of the packets is not always the same. And ever we sleep 32ms for not overload the bandwidth of the device:
// Sending Method
protected void writeStream()
{
Thread.Sleep(32);
for (int i = 0; i < txBuffer.Length; i++)
{
_nStream.WriteByte(txBuffer[ i ]);
_nStream.Flush();
}
}

// Read Method, we always pass a byte[] of only 1 byte
protected int readStream(ref byte[] byteBuffer)
{
return _nStream.Read(byteBuffer, 0, byteBuffer.Length);
}

It works perfectly with all messages, except when we configure the wave parameters, always we lose the first packet. The code for the device it's impossible to get, since we only have the connection protocol for the company that created it. We make the messages on a byte array (byte[]) and send them to the device.

We had all the code in 4D (4th Dimension) language and it works perfectly.


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

The code to send-receive is very simple, I'll share the part of sending/receive packets:

// Initialize: We tried with TcpClient and with Socket itself.
TcpClient _socket = new TcpClient();
NetworkStream _nStream;
_socket.Connect(newIP, newPort);
if (_socket.Connected)
{
_nStream = _socket.GetStream();
}

We have a thread reading/sending packets to the socket. We send the packet byte to byte and receive with the same process, because the size of the packets is not always the same. And ever we sleep 32ms for not overload the bandwidth of the device:
// Sending Method
protected void writeStream()
{
Thread.Sleep(32);
for (int i = 0; i < txBuffer.Length; i++)
{
_nStream.WriteByte(txBuffer[ i ]);
_nStream.Flush();
}
}

// Read Method, we always pass a byte[] of only 1 byte
protected int readStream(ref byte[] byteBuffer)
{
return _nStream.Read(byteBuffer, 0, byteBuffer.Length);
}

It works perfectly with all messages, except when we configure the wave parameters, always we lose the first packet. The code for the device it's impossible to get, since we only have the connection protocol for the company that created it. We make the messages on a byte array (byte[]) and send them to the device.

We had all the code in 4D (4th Dimension) language and it works perfectly.

I want to know something about your code!

What is "txBuffer" I assume its a byte array! and you are sending 1 byte at a time and flushing the stream so it should not wait for buffer to fill and send it with a delay. That's Absolutely Right!

"It works perfectly with all messages, except when we configure the wave parameters, always we lose the first packet."

What do you mean by Wave Paramters and You loose packet from Device to Computer or Computer to Device I think Computer to Device Right!

so:

protected int readStream(ref byte[] byteBuffer)
{
return _nStream.Read(byteBuffer, 0, byteBuffer.Length);
}

You are reading only one bytes at a time Are you sure you have to read exact one byte each time And how frequent you call this ReadStream method you are passing a ref array Are multiple Threads accessing this method

I think your problem lies in ReadStream method you are receiving only 1 byte and this doesnot gives me any sense that 1 byte can contain any usefull information abuot device....

Please tell me more details what I have asked about!

Best Regards,



 
 
Jordi Gonzalez





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

What is "txBuffer" I assume its a byte array! and you are sending 1 byte at a time and flushing the stream so it should not wait for buffer to fill and send it with a delay. That's Absolutely Right!

txBuffer is the array that contains the message formatted for sending it to the device. We send it byte per byte, so that the device reads all the bytes and regenerate the message without a problem.


"It works perfectly with all messages, except when we configure the wave parameters, always we lose the first packet."

What do you mean by Wave Paramters and You loose packet from Device to Computer or Computer to Device I think Computer to Device Right!

Yes, sorry, I dindn't explain it very well. There are, in patient read parameters, 2 types of parameters, cooked wave, like the ECG (Electrocardiogram) and numeric values. When we send a parameter configuration packet about a numeric(s) value(s), we receive from the device a message confirming it (and a parameterID for every parameter), and then, the device starts sending us tons of packets with the device lectures; a packet is sended every 200ms.

When we do the same process for a wave value (aka. cooked wave), we have to receive the same packet of confirmation from the machine, but the first packet we pick up it's a data packet, so the confirmation packet is lost. A packet of wave values is sended every 40~60ms.

so:

protected int readStream(ref byte[] byteBuffer)
{
return _nStream.Read(byteBuffer, 0, byteBuffer.Length);
}

You are reading only one bytes at a time Are you sure you have to read exact one byte each time And how frequent you call this ReadStream method you are passing a ref array Are multiple Threads accessing this method

We are constantly reading from the socket when we send the configuration packet, byte-to-byte, because we don't know the size of the next packet, and the device sends it with escape and sync characters.

I think your problem lies in ReadStream method you are receiving only 1 byte and this doesnot gives me any sense that 1 byte can contain any usefull information abuot device....

We work sending the message packet in a whole too, but it didn't work also.

To receive the packets, the multiport device server has a buffer that stores the messages from the device, but we read the data packets at high speed, so the buffer don't overloads.

Thank you for your patient.



 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

so:

protected int readStream(ref byte[] byteBuffer)
{
return _nStream.Read(byteBuffer, 0, byteBuffer.Length);
}

You are reading only one bytes at a time Are you sure you have to read exact one byte each time And how frequent you call this ReadStream method you are passing a ref array Are multiple Threads accessing this method

We are constantly reading from the socket when we send the configuration packet, byte-to-byte, because we don't know the size of the next packet, and the device sends it with escape and sync characters.

I think your problem lies in ReadStream method you are receiving only 1 byte and this doesnot gives me any sense that 1 byte can contain any usefull information abuot device....

We work sending the message packet in a whole too, but it didn't work also.

To receive the packets, the multiport device server has a buffer that stores the messages from the device, but we read the data packets at high speed, so the buffer don't overloads.

Thank you for your patient.

I would give some recomendations...

  1. Try to Read in a large Buffer like of 1024 bytes and dump it to some file when you receive the first Packet, It may happen that you are missing something in packet analysis at your side!
  2. pass paratemeter to ReadStream function with out keyword rather than ref. It may happen you are reading so fast and second call to ReadStream overwrites data in parameter and it becomes un available.

I hope this will solve the problem or atleast give some clue in identifying problem!

Best of Luck!



 
 
Jordi Gonzalez





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

  1. Try to Read in a large Buffer like of 1024 bytes and dump it to some file when you receive the first Packet, It may happen that you are missing something in packet analysis at your side!
  2. pass paratemeter to ReadStream function with out keyword rather than ref. It may happen you are reading so fast and second call to ReadStream overwrites data in parameter and it becomes un available.

1. We tried this the past week to :(
2. Impossible, it's only 1 byte and we catch it, hehe.



 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top


1. We tried this the past week to :(
2. Impossible, it's only 1 byte and we catch it, hehe.

Without having the hardware and code you are using, I can only pray for you now .

Best of Luck and May God help you in acheiving your goal .

Cheers



 
 
Jordi Gonzalez





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

I'll try my best, thank you :D


 
 
Jordi Gonzalez





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

Oh, actually we solved it! The first data packet arrived always first than the configured parameters. But we don't know how this packet arrived first than the other... In some other languages the configured packet arrives always first...


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

Good to know! Its all due to my prayer I did for you last night .

Best Regards and Cheers ;-)



 
 
Jordi Gonzalez





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

Good to know! Its all due to my prayer I did for you last night .

Best Regards and Cheers ;-)



lol, thank you for your prayer. But I like to know how it does a data packet jump the queue and be the first to be readed

Thank you again!


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, Problems receiving packets from socket Top

Its strange behavior really, May be device is delaying it or what! YOu told with some other language its working fine and order is correct so we cant blame device! So, I really dont have any idead why its happening like this!

Best Regards,