Click here to Skip to main content
15,902,636 members
Home / Discussions / C#
   

C#

 
GeneralRe: Basic Insert Row Question Pin
AmbiguousName13-Jun-11 2:29
AmbiguousName13-Jun-11 2:29 
GeneralRe: Basic Insert Row Question Pin
phil.o13-Jun-11 2:35
professionalphil.o13-Jun-11 2:35 
GeneralRe: Basic Insert Row Question Pin
AmbiguousName13-Jun-11 2:42
AmbiguousName13-Jun-11 2:42 
GeneralRe: Basic Insert Row Question Pin
phil.o13-Jun-11 2:49
professionalphil.o13-Jun-11 2:49 
AnswerRe: Basic Insert Row Question Pin
RaviRanjanKr13-Jun-11 2:48
professionalRaviRanjanKr13-Jun-11 2:48 
AnswerRe: Basic Insert Row Question Pin
BobJanova13-Jun-11 3:25
BobJanova13-Jun-11 3:25 
AnswerRe: Basic Insert Row Question Pin
AmbiguousName13-Jun-11 18:34
AmbiguousName13-Jun-11 18:34 
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 

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.