Click here to Skip to main content
15,910,411 members
Home / Discussions / C#
   

C#

 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Jacob Dixon12-May-09 15:54
Jacob Dixon12-May-09 15:54 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Luc Pattyn12-May-09 17:12
sitebuilderLuc Pattyn12-May-09 17:12 
AnswerRe: Retrieving USB Barcode scanner data... Pin
Dave Kreskowiak12-May-09 17:19
mveDave Kreskowiak12-May-09 17:19 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Mycroft Holmes12-May-09 19:07
professionalMycroft Holmes12-May-09 19:07 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Jacob Dixon13-May-09 3:06
Jacob Dixon13-May-09 3:06 
AnswerRe: Retrieving USB Barcode scanner data... Pin
cackharot27-May-09 15:03
cackharot27-May-09 15:03 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Jacob Dixon28-May-09 4:17
Jacob Dixon28-May-09 4:17 
QuestionSending file via TCP protocol Pin
nike_arh12-May-09 10:31
nike_arh12-May-09 10:31 
Hi! I have made a Client and a Server applications. I want to send a file from the Server to the Client. This is how I do it:

<br />
ns is a NetworkStream which is connected.<br />
writer is a StreamWriter.<br />
<br />
The Srever:<br />
<br />
FileStream fs = File.OpenRead(path);<br />
long byteCount = fs.Length;<br />
writer.WriteLine(byteCount); //Here I send the length of the file to the Client.<br />
writer.Flush();<br />
int readBytes = 0;<br />
byte[] buffer = new byte[1024];<br />
while (readBytes < byteCount && ns.CanWrite)<br />
{<br />
    int length = fs.Read(buffer, 0, buffer.Length);<br />
    ns.Write(buffer, 0, length);<br />
    ns.Flush(); //I added ns.Flush() to ensure the sending of the bytes.<br />
    readBytes = readBytes + length;<br />
}<br />
<br />
The Client:<br />
<br />
FileStream fs = File.Create(Application.StartupPath + "\\buffer.nkl"); //The buffer file.<br />
long byteCount = long.Parse(reader.ReadLine()); //The length of the file.<br />
int readBytes = 0;<br />
byte[] buffer = new byte[1024];<br />
while (readBytes < byteCount && ns.DataAvailable)<br />
{<br />
    int length = ns.Read(buffer, 0, buffer.Length);<br />
    fs.Write(buffer, 0, length);<br />
    readBytes = readBytes + length;<br />
}


The problem is that the received file is trimmed - it isn't whole (sent file is 198 kb, received file is 23 kb). If I remove ns.DataAvailable it takes a long time and the file is almost complete (sent file is 198 kb, received file is 196 kb).
What am I doing wrong???

Still learning...

AnswerRe: Sending file via TCP protocol Pin
Luc Pattyn12-May-09 14:11
sitebuilderLuc Pattyn12-May-09 14:11 
GeneralRe: Sending file via TCP protocol Pin
nike_arh12-May-09 23:35
nike_arh12-May-09 23:35 
GeneralRe: Sending file via TCP protocol Pin
Luc Pattyn13-May-09 0:03
sitebuilderLuc Pattyn13-May-09 0:03 
QuestionCurious Question about ProgressBar object between two classes Pin
Blubbo12-May-09 9:52
Blubbo12-May-09 9:52 
AnswerRe: Curious Question about ProgressBar object between two classes Pin
Christian Graus12-May-09 10:00
protectorChristian Graus12-May-09 10:00 
GeneralRe: Curious Question about ProgressBar object between two classes Pin
Blubbo13-May-09 2:19
Blubbo13-May-09 2:19 
QuestionConverting between types inside Lambda Expressions Pin
Andre Vianna12-May-09 8:59
Andre Vianna12-May-09 8:59 
QuestionDraw railway using Graphics GDI+ Pin
baranils12-May-09 8:49
baranils12-May-09 8:49 
AnswerRe: Draw railway using Graphics GDI+ Pin
Christian Graus12-May-09 9:14
protectorChristian Graus12-May-09 9:14 
GeneralRe: Draw railway using Graphics GDI+ Pin
baranils12-May-09 9:42
baranils12-May-09 9:42 
GeneralRe: Draw railway using Graphics GDI+ Pin
Christian Graus12-May-09 10:00
protectorChristian Graus12-May-09 10:00 
AnswerRe: Draw railway using Graphics GDI+ Pin
Mark Churchill12-May-09 18:27
Mark Churchill12-May-09 18:27 
GeneralRe: Draw railway using Graphics GDI+ Pin
baranils12-May-09 19:47
baranils12-May-09 19:47 
QuestionMoving an array element to a variable. Pin
Lecutus112-May-09 8:18
Lecutus112-May-09 8:18 
AnswerRe: Moving an array element to a variable. Pin
OriginalGriff12-May-09 8:26
mveOriginalGriff12-May-09 8:26 
AnswerRe: Moving an array element to a variable. Pin
Dave Kreskowiak12-May-09 8:49
mveDave Kreskowiak12-May-09 8:49 
AnswerRe: Moving an array element to a variable. Pin
Lecutus113-May-09 6:02
Lecutus113-May-09 6:02 

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.