1

So i was working on some project but there is only one problem: after the server receive the file i can't do anything to it... So here is how it is: The user chooses a file... The file changes to some special format i made... then sent to my server... My server keeps waiting for files... then the file comes to me BUT ANYTHING after the code that receives the file doesn't excute i don't know why...

What do i want exactly: i want the code to run(In english I want my files to be converted to normal files)... I tried putting "ConvertFile()" in every place possible

var listener = new TcpListener(IPAddress.Parse("---.---.-.---"), ----);
listener.Start();
while (true)
{
    using (var client = listener.AcceptTcpClient())
    using (var stream = client.GetStream())
    using (FileStream output = new FileStream("result.dat", FileMode.Create))
    {
        var buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
        ConvertFile("result.dat");
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Emily
  • 142
  • 10
  • What does `ConvertFile("result.dat")` actually do? – Matt Hogan-Jones Apr 27 '17 at 15:41
  • Convert the file to the normal format(The client changes the file format... To an unreadable format then here at convertfile the file bytes get back to normal) – Emily Apr 27 '17 at 15:44
  • 2
    Are you getting an Access Denied / File in use error? If so then it is because you have not closed the output stream. Try moving the ConvertFile routine to outside the using, or call output.Close before your convert, – sgmoore Apr 27 '17 at 15:49
  • `AcceptTcpClient` is a blocking call. Does it ever finish? If you cannot run in a debugger, then at least add tracing/logging otherwise we're only guessing. – Cee McSharpface Apr 27 '17 at 15:50
  • Are you saying that the ConvertFile method doesn't actually get called, or that it doesn't work properly? – Matt Hogan-Jones Apr 27 '17 at 15:54
  • It doesn't even get called... Well no Access denied or unhandled exceptions... what do you mean by "does it ever finish" – Emily Apr 27 '17 at 16:32
  • but does the file appear in the file system? is it zero size? please debug and add more information. By "does it ever finish", I mean if execution ever gets any further than the line where you have "AcceptTcpClient". – Cee McSharpface Apr 27 '17 at 16:35
  • Well yes it does the file is FULL in size the data is completely full everything runs except my "ConvertFIle()" i even replaced it with a message box and the message box doesn't show Also i tried putting the message box in the while loop and it keeps giving messages so i am pretty sure that the while loop gets excuted... but everything after that loop doesn't excute at all – Emily Apr 27 '17 at 16:41
  • 1
    i also tried putting a break statment in the while loop... Everything went fine but OFC the file wasn't full... How can i do it in a way that the file is FULLY made and get out of that loop? I mean i have 2 choices: either have the loop running forever or have my file not complete – Emily Apr 27 '17 at 16:49
  • In this case, duplicate of [C# - TcpClient - Detecting end of stream?](http://stackoverflow.com/questions/6522869/c-sharp-tcpclient-detecting-end-of-stream). See also http://stackoverflow.com/a/39337718/1132334 – Cee McSharpface Apr 27 '17 at 16:53
  • Well no... it's just something in the while loop that it always returns true so it keeps continuing forever... I need to know how many bytes are sent in stream... and how many are received then compare them but the thing is IDK how to – Emily Apr 27 '17 at 17:00
  • that's the essence of the two answers I linked in the previous comment. one way is to prefix the transmission with the size of the file and use that in the receiving loop as a break condition. – Cee McSharpface Apr 27 '17 at 17:26
  • So do you mean i more like "send a message saying "NumberOfBytes: (5000000)" then on the server it would take the text between the brackets and convert it to intger and when it does just say if the data i recieved equals that number..." But how do i check the data i already recieved? @dlatikay – Emily Apr 27 '17 at 18:03
  • declare the first 64 bit (=8 byte) of the transmission to be the size descriptor. whenever your code enters the while loop, read the first eight bytes, interpret them as `UInt64`, then break after the additional quantity of bytes read matches that decoded number, and start over waiting for the next transmission (just as a basic idea, not accounting for clients that send malformed, incomplete or invalid data). – Cee McSharpface Apr 27 '17 at 18:11
  • Turns out when i put the messageboxes in the loop the loop doesn't actually go forever... The messages just go with the size of the file (2kbs+ = 2 messageboxes) But still it doesn't go reading more code it just freezes there (No lags or shit) but it doesn't read anything next @dlatikay – Emily Apr 27 '17 at 19:19

0 Answers0