Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

NetworkStream.Close() - Last bit of data not being sent

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom - 29 Jun 2007 15:39 GMT
Hello,

This is my first post on google groups btw :)

I am having an issue with the NetworkStream object.

byte[] buffer = new byte[1024];
int bytesRead = 0;
Networkstream ns = new networkstream(socket);

while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
    ns.Write(buffer, 0, bytesRead);
    ns.Flush();
}

ns.Close();      // <- The Issue

This snippet for some reason does not send all the data in the
buffer.
If I remove the last line "ns.Close();"  it works perfectly but causes
other problems such as clients never getting disconnected.

I notice there was a timeout option but I am  unsure how how to use it
correctly.  How do I determine the time needed to send all the data?
Also shouldn't the Flush() write to the stream.

What this is for is a basic webserver.  I browser requests a specific
file and I load it and send it to them.  That is what the outcome is
supposed to be.

Any help would be appreciated!

Thanks,

Thomas
Peter Duniho - 29 Jun 2007 17:11 GMT
> [...]
> This snippet for some reason does not send all the data in the buffer.
> If I remove the last line "ns.Close();"  it works perfectly but causes
> other problems such as clients never getting disconnected.

You need to Shutdown(), and not Close() until you're sure you're done (you  
get notification that the connection has been closed).

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.