Receiving 0x1A in UDP datagram

Viewed 72

I am using C#.NET in order to interface with matlab for UDP communication (because i do not have the toolkit for matlab udp support). I made two C# classes (Client Class and Server Class) and built them to DLL's and imported them into matlab using NET.addAssembly();

What I do is:

  1. read a binary file (File1.bin) into a byte[] array
// C#
byte[] data = File.ReadAllBytes();
  1. Send the byte array using UDP sockets.
// C#
sendClient.Send(data, data.Length);
  1. Write the received datagram to another binary file (File2.bin)

In matlab:

% Receive data...
fd = fopen('File2.bin','w');
for i=1 through the length of data:
    fprintf(fd, data(i));

My problem is when I use visdiff('File1.bin', 'File2.bin') in matlab to compare File1.bin and File2.bin, almost everything is identical except for a few (~30) bytes in File2.bin are replaced with 0x1A and I need these two files to be identical.

Any help or suggestions are appreciated!

Thanks

Note: This is done on a single machine in two different matlab windows.

If you need more info let me know. I cant copy and paste code because it is on a machine not internet accessible.

UPDATE:

After more debugging I have found that the problem occurs before any UDP datagrams are sent. Using C# ReadAllBytes() and Matlab fread() both output arrays contain these anomalous 0x1A, but I still do not know why...

1 Answers

I believe I have solved the issue.

When I was writing the byte array to a file I was using the following Matlab code:

fd = fopen('file2.bin', 'w');
Data = server.Receive(); // This received UDP data as a System.byte[] array

for i=1:Data.Length
   fprintf(fd,'%c',char(Data(i)));
end

I assumed this would be equivalent to writing to the file 1 byte at a time as a char should be 1 byte in size.

I then replaced that code with the following:

fd = fopen('file2.bin', 'w');
Data = server.Receive(); // This received UDP data as a System.byte[] array

Data = uint8(Data);
fwrite(fd, Data);

After making this change, when I ran visdiff('file1.bin', 'file2.bin'); the two files were identical. I am not exactly sure why the version I had before didnt work, but maybe it is related to what Sam Mason had mentioned.

Anyway, thanks for the help!

Related