Socket corrupting data

D

David Glover

Hi all. This message has also been posted to the
microsoft.public.dotnet.framework newsgroup, and I apologise if you
are reading this again however - I was wondering if you could help
with the following
situation. I have a Mobile Device running PPC2002. In this I am
running the following code (note this code is scrappy in places and
will be optomzied before use :)):

//int numberOfChunks
double fileSized = fileSizei ;
double segmentsd = fileSized / dataSegmentSize ;
int segments = Convert.ToInt32(segmentsd) ;


//Message header is UID FILESIZE NUMBER_OF_SEGMENTS
string headers = fileID.ToString() + " " + fileSizei.ToString() + "
" + segments.ToString() ;
byte[] header = Encoding.ASCII.GetBytes(headers) ;

clientSocket.Send(header, 0, header.Length, SocketFlags.None) ;

byte[] fileUIDasbyte = BitConverter.GetBytes(fileID) ;

//now read the data and send
for(int i = 0 ; i < segments ; i++)
{
//read 8184 bytes from the offset marked by i
int offset = dataSegmentSize * i ;
byte[] fileData = new byte[dataSegmentSize] ;

//int bytesRead = fs.Read(fileData,offset, dataSegmentSize) ;
int bytesRead = fs.Read(fileData,0,dataSegmentSize) ;

byte[] dataToXmit = new byte[8192] ;

//Add the fileID to the start of the byte
fileUIDasbyte.CopyTo(dataToXmit, 0) ;

//Now the segment number
byte[] segmentNumberAsByte = BitConverter.GetBytes(i) ;
segmentNumberAsByte.CopyTo(dataToXmit,4) ;

//Now copy in the data
fileData.CopyTo(dataToXmit,8) ;

//Send the data
clientNetworkStream.Write(dataToXmit,0,dataToXmit.Length);}

Where clientNetworkStream is a NetworkStream object. The purpose of
this code is to send a file to a server application with the following
format
<FILEID><SEGMENTID><SEGMENTDATA>. The data is segmented as I am
sending around 4/5 Mb of data (an MP3 file typically), and I have to
break it into sections of 8K to send.

On the server I am running:

for(int i = 0 ; i < segments ; i++)
{
byte[] segmentData = new byte[8192] ;
sock.Receive(segmentData, 0, 8192, SocketFlags.None) ;

int incommingID = BitConverter.ToInt32(segmentData,0) ;
int segmentID = BitConverter.ToInt32(segmentData, 4) ;

if(segmentID != i)
{
Console.WriteLine("Segment not recieved in expected order -
Expected " + i + " recieved " + segmentID) ;
}

//now partition off the data into the file data
MemoryStream ms = new MemoryStream(segmentData) ;
byte[] segmentFileData = new byte[8184] ;

ms.Seek(8,SeekOrigin.Begin) ;

int bytesRead = ms.Read(segmentFileData,0,segmentFileData.Length)
;

//Now copy the segment file data to the correct position in the
final array
int offset = segmentID * 8184 ;
segmentFileData.CopyTo(fileData,offset) ;
ms.Close() ;
Console.WriteLine(segmentID.ToString() + " Processed") ;

}

//Then store the data to disk


When I am running the code, the data appears to get corrupted some
how. For example the file id which is embedded in the data is
converted to the value -1836etc and the segment ID is also similarly
corrupted, and so the payload of the data must also be corrupted. The
segment which the code fails on changes each time I execute it, and I
am at a loss as to the cause of this bug.

Please can anyone suggest anything which may help in this matter?
Thanks

David
 
A

Alex Feinman [MVP]

I respectfully suggest that you read a book on network communication.
As for why does the code not work - why do you expect that
BitConverter.ToInt32() will be an opposite of ToString() +
Encoding.GetBytes()?
Consider using BinaryWriter and BinaryReader to write your headers into the
stream and then read it later.


David Glover said:
Hi all. This message has also been posted to the
microsoft.public.dotnet.framework newsgroup, and I apologise if you
are reading this again however - I was wondering if you could help
with the following
situation. I have a Mobile Device running PPC2002. In this I am
running the following code (note this code is scrappy in places and
will be optomzied before use :)):

//int numberOfChunks
double fileSized = fileSizei ;
double segmentsd = fileSized / dataSegmentSize ;
int segments = Convert.ToInt32(segmentsd) ;


//Message header is UID FILESIZE NUMBER_OF_SEGMENTS
string headers = fileID.ToString() + " " + fileSizei.ToString() + "
" + segments.ToString() ;
byte[] header = Encoding.ASCII.GetBytes(headers) ;

clientSocket.Send(header, 0, header.Length, SocketFlags.None) ;

byte[] fileUIDasbyte = BitConverter.GetBytes(fileID) ;

//now read the data and send
for(int i = 0 ; i < segments ; i++)
{
//read 8184 bytes from the offset marked by i
int offset = dataSegmentSize * i ;
byte[] fileData = new byte[dataSegmentSize] ;

//int bytesRead = fs.Read(fileData,offset, dataSegmentSize) ;
int bytesRead = fs.Read(fileData,0,dataSegmentSize) ;

byte[] dataToXmit = new byte[8192] ;

//Add the fileID to the start of the byte
fileUIDasbyte.CopyTo(dataToXmit, 0) ;

//Now the segment number
byte[] segmentNumberAsByte = BitConverter.GetBytes(i) ;
segmentNumberAsByte.CopyTo(dataToXmit,4) ;

//Now copy in the data
fileData.CopyTo(dataToXmit,8) ;

//Send the data
clientNetworkStream.Write(dataToXmit,0,dataToXmit.Length);}

Where clientNetworkStream is a NetworkStream object. The purpose of
this code is to send a file to a server application with the following
format
<FILEID><SEGMENTID><SEGMENTDATA>. The data is segmented as I am
sending around 4/5 Mb of data (an MP3 file typically), and I have to
break it into sections of 8K to send.

On the server I am running:

for(int i = 0 ; i < segments ; i++)
{
byte[] segmentData = new byte[8192] ;
sock.Receive(segmentData, 0, 8192, SocketFlags.None) ;

int incommingID = BitConverter.ToInt32(segmentData,0) ;
int segmentID = BitConverter.ToInt32(segmentData, 4) ;

if(segmentID != i)
{
Console.WriteLine("Segment not recieved in expected order -
Expected " + i + " recieved " + segmentID) ;
}

//now partition off the data into the file data
MemoryStream ms = new MemoryStream(segmentData) ;
byte[] segmentFileData = new byte[8184] ;

ms.Seek(8,SeekOrigin.Begin) ;

int bytesRead = ms.Read(segmentFileData,0,segmentFileData.Length)
;

//Now copy the segment file data to the correct position in the
final array
int offset = segmentID * 8184 ;
segmentFileData.CopyTo(fileData,offset) ;
ms.Close() ;
Console.WriteLine(segmentID.ToString() + " Processed") ;

}

//Then store the data to disk


When I am running the code, the data appears to get corrupted some
how. For example the file id which is embedded in the data is
converted to the value -1836etc and the segment ID is also similarly
corrupted, and so the payload of the data must also be corrupted. The
segment which the code fails on changes each time I execute it, and I
am at a loss as to the cause of this bug.

Please can anyone suggest anything which may help in this matter?
Thanks

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top