首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#传输文件夹中的文件到服务器 出有关问题了 麻烦~

2011-12-28 
C#传输文件夹中的文件到服务器 出问题了 麻烦高手救命~~~在线等服务器短接受文件:{IPAddressipIPAddress.

C#传输文件夹中的文件到服务器 出问题了 麻烦高手救命~~~在线等
服务器短接受文件:
{
IPAddress   ip   =   IPAddress.Parse( "127.0.0.1 ");
TcpListener   listen   =   new   TcpListener(ip   ,8888);
listen.Start();
while(true)
{
this.label1.Text= "等待客户端连接............ ";
TcpClient   client   =   listen.AcceptTcpClient();
NetworkStream   ns   =   client.GetStream();

byte[]   b   =   new   byte[255];
ns.Read(b,0,b.Length);
string[]   s   =   Encoding.UTF8.GetString(b).Split( '\n ');

long   fileLength   =   long.Parse(s[1]);

byte[]   fileContent   =   new   byte[fileLength];
ns.Read(fileContent,0,(int)fileLength);

FileStream   f   =   new   FileStream( "C:\\ "   +   s[0].Substring(s[0].LastIndexOf( "\\ ")),FileMode.Create,FileAccess.Write);
f.Write(fileContent,0,(int)fileLength);
f.Close();
client.Close();
this.label1.Text= "接受文件成功! ";
}
}

客户端:
public       void       FindFile(string       dir)                                                       //参数为指定的目录      
{              
//在指定目录及子目录下查找文件,在listBox1中列出子目录及文件      
DirectoryInfo       Dir=new       DirectoryInfo(dir);      
try      
{      
foreach(DirectoryInfo       d       in       Dir.GetDirectories())           //查找子目录          
{      
FindFile(Dir+d.ToString()+ "\\ ");      
//listBox1.Items.Add(Dir+d.ToString()+ "\\ ");               //listBox1中填加目录名      
}      
foreach(FileInfo       f       in       Dir.GetFiles( "*.* "))                           //查找文件      
{      
listBox1.Items.Add(Dir+f.ToString());           //listBox1中填加文件名    
 
string   ip=this.IP.Text.Trim();
string   s=Dir+f.ToString();
TcpClient   client   =   new   TcpClient(this.IP.Text.Trim(),8888);
NetworkStream   ns   =   client.GetStream();
this.label3.Text= "已连接到服务器,准备发送文件! ";

FileInfo   f1=new   FileInfo(s);
if(f.Exists)  
{
long   l   =   f1.Length;
string   fInfo   =   s   +   "\n "   +   l;
byte[]   b   =   Encoding.UTF8.GetBytes(fInfo);
ns.Write(b,   0,   b.Length);
ns.Flush();
this.label3.Text= "正在发送文件....... ";
FileStream   fs   =   new   FileStream(s,   FileMode.Open,   FileAccess.Read);
byte[]   fBytes   =   new   byte[l];
fs.Read(fBytes,   0,   fBytes.Length);
ns.Write(fBytes,   0,   fBytes.Length);


ns.Flush();
fs.Close();

}  
else  
{
MessageBox.Show( "此文件不存在,请重新输入! ");
return;
}

}      
}      
catch(Exception       e)      
{      
MessageBox.Show( "上传失败,注意目录是否正确! "+e.Message);      
}       this.label3.Text= "文件发送成功....... ";
}      
private   void   button1_Click(object   sender,   System.EventArgs   e)
{

string       currentdir= "d:\\1111 ";           //搜索的目录      
if(currentdir[currentdir.Length-1]!= '\\ ')       //非根目录      
currentdir+= "\\ ";          
FindFile(currentdir);           //调用查找文件函数      
}


[解决办法]
这个需要循环吗?(while(true)),C#没做过这个,不过在C++中,当你的Listen启动后,每次数据传过来都会激发一个事件,在里面处理,就是异步的,你这样用循环套起来,会不会死掉啊!
至少应该有个break;之类的来退出循环吧?
[解决办法]
long fileLength = long.Parse(s[1]);

因为是服务器主动收,当客户端没有传,你收到的字节数就是0
异步是客户端主动的,服务器收到后激发事件

热点排行