日期:2014-05-20  浏览次数:20720 次

网络发送文件的问题在线等待
发送
NetworkStream   n   =   STcpL.GetStream();  
NetWork.Flush();  
NetWork   =   STcpL.GetStream();
byte[]   by   =   new   byte[FStr.Length];  
long   start   =   0;
long   end       =   FStr.Length;
Fa(strname);
while(end   !=0)
{
int   count   =   FStr.Read(by,(int)start,(int)end);
start   +=   count;
end       -=   count;
}
 
NetWork.Write(by,(int)end,(int)start);  
n.Close();  


n.Close();这一句要是没有的话文件就会很大,因为我用一个循环把所要发送的文件动发完,但是加上n.Close();   下会就不能用了呀.

接收
NetworkStream   nt   =     Tct.GetStream();
        String   myCompleteMessage   =   " ";
byte[]   myReadBuffer   =   new   byte[1024];
byte[]   myRead               =   new   byte[1024];  
int   numberOfBytesRead   =   0;
       
numberOfBytesRead     =   nt.Read(myRead,0,myRead.Length);
myCompleteMessage   =   Encoding.UTF8.GetString(myRead,0,numberOfBytesRead);

if(myCompleteMessage.Trim()!= " ")
{      
FileStream         Fs   =   new   FileStream(Application.StartupPath+@ "\ "+myCompleteMessage.Trim(),FileMode.Create);        
while(!false)
{      
numberOfBytesRead     =   nt.Read(myReadBuffer,0,myReadBuffer.Length);
Fs.Write(myReadBuffer,0,numberOfBytesRead);
if   (numberOfBytesRead==0)
{
Fs.Flush();
Fs.Close();  
listBox1.Items.Add( "接受完毕。 ");
return;
}
 
}
}

大家有没有连续发送和接受多个文件的例子。

------解决方案--------------------
帮你顶.