Many thanks for the reply. I fear my initial response to your post may
have been lost. In case it has - here is another response:
I have gone back to basics and written a basic full .NET applicaiton
which will segment the file and send it to a client. The output file and
the input files are different when compared in a hex editor (I am using
HexCmp).
The test app below does not implement segment ID fields, however the
final app will. Can anyone see/suggest what may be going wrong here:
***SENDER APPLICAITON***
...
private Socket clientSocket
...
private void button1_Click(object sender, System.EventArgs e)
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp) ;
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 676) ;
serverSocket.Bind(localEndPoint) ;
serverSocket.Listen(10) ;
clientSocket = serverSocket.Accept() ;
}
private void button2_Click(object sender, System.EventArgs e)
{
bool f = clientSocket.Blocking ;
//NetworkStream ns = new NetworkStream(clientSocket,
FileAccess.Write, true) ;
FileStream fs = new FileStream(@"e:\dump\out.mp3",FileMode.Open,
FileAccess.Read, FileShare.Read) ;
BinaryReader b = new BinaryReader(fs, System.Text.Encoding.ASCII) ;
long dataLength = fs.Length ;
//calculate the number of segments that are required to send the file
int fileSizei = (int)dataLength ;
double fileSized = fileSizei ;
double segmentsd = fileSized / 8192 ;
int segments = Convert.ToInt32(segmentsd) ;
MessageBox.Show("File size : " + fileSizei.ToString()) ;
MessageBox.Show("Segments : " + segments.ToString()) ;
int totalBytesWritten = 0;
for(int i = 0 ; i < segments ; i++)
{
//read the data from the buffer
if(i == (segments-1))
{
//last segment
//calculate the number of bytes left
int bytesLeft = fileSizei - totalBytesWritten ;
byte[] send = b.ReadBytes(bytesLeft) ;
fs.Read(send, 0, bytesLeft) ;
int bytesSent = clientSocket.Send(send, 0, bytesLeft,
SocketFlags.None) ;
Console.WriteLine(bytesSent) ;
if(bytesSent != bytesLeft)
{
MessageBox.Show("ERROR WRITING BYTES") ;
}
}
else
{
//byte[] send = new byte[8192] ;
//fs.Read(send, 0,8192) ;
byte[] send = b.ReadBytes(8192) ;
int sentBytes = clientSocket.Send(send,0,8192,SocketFlags.None) ;
Console.WriteLine(sentBytes) ;
if(sentBytes != 8192)
{
MessageBox.Show("ERROR WRITING 8192 bytes") ;
}
totalBytesWritten += 8192 ;
}
//wait for the "OK"
byte[]ok = new byte[16] ;
clientSocket.Receive(ok, 0, 16, SocketFlags.None) ;
}
//fs.Close() ;
b.Close() ;
MessageBox.Show("SEND COMPLETE") ;
}
***RECIEVER***
...
private Socket sock ;
...
private void button1_Click(object sender, System.EventArgs e)
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp) ;
IPAddress sip = IPAddress.Parse("192.168.10.2") ;
IPEndPoint remote = new IPEndPoint(sip,676) ;
sock.Connect(remote) ;
}
private void button2_Click(object sender, System.EventArgs e)
{
//make the user enter hte file size and number of segments
//these aer messagebox'd to the screen in the sender
//adn this is only a test harness when all said and done

int fileSize = Int32.Parse(textBox1.Text) ;
int segments = Int32.Parse(textBox2.Text) ;
bool block = sock.Blocking ;
FileStream f = new FileStream(@"c:\recieved.mp3",
FileMode.OpenOrCreate, FileAccess.ReadWrite) ;
BinaryWriter b = new BinaryWriter(f, System.Text.Encoding.ASCII) ;
int totalrecieved = 0;
for(int i = 0 ; i < segments ; i++)
{
if(i == (segments -1))
{
//last segment
int remaining = fileSize - totalrecieved ;
byte[] read = new byte[remaining] ;
sock.Receive(read, 0, remaining, SocketFlags.None) ;
b.Write(read) ;
}
else
{
byte[] read = new byte[8192] ;
sock.Receive(read, 0, 8192, SocketFlags.None) ;
b.Write(read) ;
totalrecieved += 8192;
}
string OK = "OK" ;
byte[] okb = System.Text.Encoding.ASCII.GetBytes(OK) ;
Console.WriteLine(okb.Length) ;
sock.Send(okb, 0, okb.Length, SocketFlags.None) ;
}
b.Close() ;
MessageBox.Show("DONE") ;
//f.Close() ;
}
I have put the 2 files used at:
http://www.lancs.ac.uk/ug/gloverd/out.mp3 - the file used by the SENDER
http://www.lancs.ac.uk/ug/gloverd/recieved.mp3 - the output file from
the reciever. Using a hex comparer, it can be seen that the errors start
on byte 19519.
Many thanks for any help anyone can provide, as I have spent quite a
while getting nowhere on this problem.
Regards and thanks,
David
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!