Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
Questionhow to monitor downloaded and uploaded data Pin
Aisha sharma12-Jun-11 22:17
Aisha sharma12-Jun-11 22:17 
QuestionHow to create xml file in disk location using SQL Server 2005 & C#...? Pin
Manish_Kumar_Nayak12-Jun-11 21:03
Manish_Kumar_Nayak12-Jun-11 21:03 
AnswerRe: How to create xml file in disk location using SQL Server 2005 & C#...? Pin
Richard MacCutchan12-Jun-11 23:46
mveRichard MacCutchan12-Jun-11 23:46 
AnswerRe: How to create xml file in disk location using SQL Server 2005 & C#...? Pin
jschell13-Jun-11 8:43
jschell13-Jun-11 8:43 
QuestionGetting data from host, convert to string and then to bytes again. Pin
krosty478212-Jun-11 13:06
krosty478212-Jun-11 13:06 
AnswerRe: Getting data from host, convert to string and then to bytes again. Pin
Luc Pattyn12-Jun-11 13:42
sitebuilderLuc Pattyn12-Jun-11 13:42 
GeneralRe: Getting data from host, convert to string and then to bytes again. Pin
krosty478212-Jun-11 15:07
krosty478212-Jun-11 15:07 
AnswerRe: Getting data from host, convert to string and then to bytes again. Pin
Luc Pattyn12-Jun-11 15:21
sitebuilderLuc Pattyn12-Jun-11 15:21 
yes, a typical mistake. A read operation not always returns the amount of data requested. Your first file length isn't necessarily a multiple of 1024, so the last read is bound to return less, yet you write an entire array of 1024 data bytes. The solution is simple: capture the return value of Read() and use it as a parameter to Write().

BTW: all streams have a Dispose method which you should call to clean up; AFAIK such Dispose() automatically calls Flush() and Close() when necessary. A simple way to get Dispose() called automatically is through the using statement, like so:
using (NetworkStream nStream = new NetworkStream(getSocket(numsock).getSocket(),true)) {
    using (FileStream fs = new FileStream("C:\\Users\\Mauri\\Desktop\\" + item.Name, FileMode.Append, FileAccess.Write)) {
        using (BinaryWriter writer = new BinaryWriter(fs)) {           
            for(;;) {
                int len=nStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                if (len==0) break;
                writer.Write(myReadBuffer, 0, len);
            }
        }
    }
}

which avoids a number of potential mistakes and yields the most readable code IMO.

Of course, you might want to reorganize if you need to keep the NetworkStream alive after a file has been transmitted. Then either rearrange the code so it handles a number of files inside the outer using, or drop the using and call nStream.Dispose() explicitly when done.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.4

GeneralRe: Getting data from host, convert to string and then to bytes again. Pin
krosty478215-Jun-11 12:53
krosty478215-Jun-11 12:53 
AnswerRe: Getting data from host, convert to string and then to bytes again. Pin
Luc Pattyn12-Jun-11 15:34
sitebuilderLuc Pattyn12-Jun-11 15:34 
GeneralRe: Getting data from host, convert to string and then to bytes again. Pin
BobJanova13-Jun-11 0:05
BobJanova13-Jun-11 0:05 
QuestionUsing Cobol Copy Books Pin
MumbleB11-Jun-11 0:53
MumbleB11-Jun-11 0:53 
AnswerRe: Using Cobol Copy Books Pin
PIEBALDconsult11-Jun-11 3:33
mvePIEBALDconsult11-Jun-11 3:33 
AnswerRe: Using Cobol Copy Books Pin
Luc Pattyn11-Jun-11 3:53
sitebuilderLuc Pattyn11-Jun-11 3:53 
GeneralRe: Using Cobol Copy Books Pin
PIEBALDconsult11-Jun-11 6:09
mvePIEBALDconsult11-Jun-11 6:09 
AnswerRe: Using Cobol Copy Books Pin
Luc Pattyn11-Jun-11 6:27
sitebuilderLuc Pattyn11-Jun-11 6:27 
GeneralRe: Using Cobol Copy Books Pin
PIEBALDconsult11-Jun-11 14:03
mvePIEBALDconsult11-Jun-11 14:03 
AnswerRe: Using Cobol Copy Books Pin
Not Active11-Jun-11 5:29
mentorNot Active11-Jun-11 5:29 
GeneralRe: Using Cobol Copy Books Pin
MumbleB11-Jun-11 6:53
MumbleB11-Jun-11 6:53 
AnswerRe: Using Cobol Copy Books Pin
DaveyM6911-Jun-11 10:19
professionalDaveyM6911-Jun-11 10:19 
QuestionPrintPreviewControl and HTML Pin
dSolariuM10-Jun-11 23:36
dSolariuM10-Jun-11 23:36 
AnswerRe: PrintPreviewControl and HTML Pin
Luc Pattyn11-Jun-11 0:11
sitebuilderLuc Pattyn11-Jun-11 0:11 
GeneralRe: PrintPreviewControl and HTML Pin
MumbleB11-Jun-11 1:19
MumbleB11-Jun-11 1:19 
GeneralRe: PrintPreviewControl and HTML Pin
dSolariuM12-Jun-11 2:32
dSolariuM12-Jun-11 2:32 
GeneralRe: PrintPreviewControl and HTML Pin
dSolariuM12-Jun-11 2:33
dSolariuM12-Jun-11 2:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.