Right now I'm really getting mad about this... What I've done now, is using the Async Socket BeginReceive-Method, to receive the file. I created a little class, which does all the receiving for me. So, here is what the class looks like:
class AsyncFileRecv
{
private long fileSize;
private long received;
private Socket recvSocket;
private byte[] buffer;
private BinaryWriter writer;
private sic sic_;
private string senderUIN;
public bool done;
private Object myLock = new Object();
public AsyncFileRecv(long FileSize, Socket RecvSocket, BinaryWriter Writer, sic Sic_, string SenderUIN)
{
fileSize = FileSize;
recvSocket = RecvSocket;
writer = Writer;
sic_ = Sic_;
senderUIN = SenderUIN;
received = 0;
done = false;
if (fileSize > 1000)
{
buffer = new byte[1000];
recvSocket.BeginReceive(buffer, 0, 1000, SocketFlags.None, receive, null);
}
else
{
buffer = new byte[(int)fileSize];
recvSocket.BeginReceive(buffer, 0, (int)fileSize, SocketFlags.None, endReceive, null);
}
}
public void receive(IAsyncResult res)
{
if (res.IsCompleted)
{
received += 1000;
lock (myLock)
{
writer.Write(buffer, 0, 1000);
}
sic_.updateFileTransferWindow(senderUIN, (((float)received / (float)fileSize) * 100));
if ((fileSize - received) > 1000)
{
buffer = new byte[1000];
recvSocket.BeginReceive(buffer, 0, 1000, SocketFlags.None, receive, null);
}
else
{
buffer = new byte[(int)(fileSize - received)];
recvSocket.BeginReceive(buffer, 0, (int)(fileSize - received), SocketFlags.None, endReceive, null);
}
}
else
{
MessageBox.Show("Async not completed - receive");
}
}
public void endReceive(IAsyncResult res)
{
if (res.IsCompleted)
{
lock (myLock)
{
writer.Write(buffer, 0, (int)(fileSize - received));
}
received = fileSize;
sic_.updateFileTransferWindow(senderUIN, (((float)received / (float)fileSize) * 100));
done = true;
writer.Close();
}
else
{
MessageBox.Show("Async not completed - endReceive");
}
}
}
Also I changed the code of the function recvFile as follows:
private bool recvFile(Socket tmpSocket, int TotalSize, string filename, string senderUIN, object signal)
{
Socket p2pSocket = tmpSocket;
int totalSize = TotalSize;
byte[] buffer = new byte[1000];
BinaryWriter writer;
if (!System.IO.Directory.Exists(@"./Received Files"))
System.IO.Directory.CreateDirectory(@"./Received Files");
string user_dir = "./Received Files/" + uin;
if (!System.IO.Directory.Exists(@user_dir))
System.IO.Directory.CreateDirectory(@user_dir);
writer = new BinaryWriter(File.Open(user_dir + "/" + filename, FileMode.Create));
AsyncFileRecv fileRecv = new AsyncFileRecv(totalSize, p2pSocket, writer, sic_, senderUIN);
while (!fileRecv.done)
{
}
return true;
}
Yes, I know, the
while (!fileRecv.done)
isn't really well coded, but right now I just want this to work...
To give you a better impression of what my testfiles look like before and after sending, I uploaded them. All in all it's four files. Two images (one before and one after sending) and two textfiles. You can find them here:
click here for the testfiles[
click here for new window]
Please regard, that you need to scroll sideways, to see all of the textfile after sending.
Again, thank you for your help in advance...
Edit: Because the method recvData isn't used anymore, I didn't post the code for that. I hope that's okay for you...
Edit 2: I'm not sure, if my answer did show that, but the problem still exists...
Edit 3: Not sure if this helps, but I recognized, that decreasing the buffer-size to 10 bytes makes the code work, altough the speed is extremely low...