Im working on a project and Im stuck on the file transfering ... so far its working but in my mind it's not the right way. When I send the file I wait 1 second after it was send. After that I send the stop command to the receiver so that he (the receiver) can close the file . Every byte array send is made to string so that I can check if the received byte array is the stop command. Without the sleep command , the stop command wont be send separated (it will be send with another byte array). Is there any way to stop the file transfering more efficient without closing the sockets ? Sending function:
public void sendFile()
{
string filename = Path.GetFileName(fileLocation);
byte[] SendingBuffer = null;
server = client.GetStream();
int BufferSize = 1024;
try
{
using (FileStream fs = File.Open(fileLocation, FileMode.Open, FileAccess.Read, FileShare.None))
{
int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(fs.Length) / Convert.ToDouble(BufferSize)));
int TotalLenght = (int)fs.Length, CurrentPacketLengh = 0;
mainWindowClass.progressBar2.Dispatcher.Invoke(new Action(()=> mainWindowClass.progressBar2.Maximum = NoOfPackets));
sendCommand("sndfil"+ filename, "c0mm@nds t0 s3nd %!3");
for (int i = 0; i < NoOfPackets; i++)
{
//MessageBox.Show(NoOfPackets.ToString() + " " + i);
if (TotalLenght > BufferSize)
{
CurrentPacketLengh = BufferSize;
TotalLenght = TotalLenght - CurrentPacketLengh;
}
else
CurrentPacketLengh = TotalLenght;
SendingBuffer = new byte[CurrentPacketLengh];
fs.Read(SendingBuffer, 0, CurrentPacketLengh);
server.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
mainWindowClass.progressBar2.Dispatcher.Invoke(new Action(() => mainWindowClass.progressBar2.Value++));
}
Thread.Sleep(1000);
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("stopfile");
server.Write(outStream, 0, (int)outStream.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
mainWindowClass.progressBar2.Dispatcher.Invoke(new Action(() => mainWindowClass.progressBar2.Value=0));
}
Receiving function:
public void receiveFile(string name)
{
string fileName = (System.IO.Path.Combine(mainWindowClass.saveFolder, name));
byte[] RecData = new byte[1024];
int RecBytes;
NetworkStream netstream = null;
try
{
int total = 0;
netstream = client.GetStream();
FileStream Fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
RecBytes = netstream.Read(RecData, 0, RecData.Length);
char[] chars = new char[RecBytes];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(RecData, 0, RecBytes, chars, 0);
System.String recv = new System.String(chars);
while (recv!="stopfile")
{
total += RecData.Length;
Fs.Write(RecData, 0, RecBytes);
RecBytes = netstream.Read(RecData, 0, RecData.Length);
chars = new char[RecBytes];
d = System.Text.Encoding.UTF8.GetDecoder();
charLen = d.GetChars(RecData, 0, RecBytes, chars, 0);
recv = new System.String(chars);
}
Fs.Close();
mainWindowClass.testLabel.Dispatcher.Invoke(new Action(() => mainWindowClass.testLabel.Content += total.ToString()));
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
I've searched on google the solution , but all I found was to close the client/server.