首页 新闻 搜索 专区 学院

关于异步发送多个文件的问题

0
悬赏园豆:20 [已关闭问题]

做了一个练习,想要把列表中的文件一个个的发给客户端。结果发现我在循环中发送,客户端最后只能接收发过去的第一个文件(就是列表中的第一个文件)。
1.我想知道我收不到全部文件是什么原因?是因为服务器发送的速度要大于客户端接收并保存的速度吗?
2.我想知道我该怎样改进?
下面是我的代码:

发送端的
ArrayList slist = GetSelectedClientList(); //将选中的客户端保存在slist中
ArrayList flist = GetMediaFileList("MediaLists.xml"); //得到媒体文件列表中不重复的列表内容

for (int i = 0; i < slist.Count; i++)
{
Socket s
= (Socket)slist[i];
for (int j = 0; j < flist.Count; j++)
{
FileStream fsfile
= new FileStream(flist[i].ToString(), FileMode.Open, FileAccess.Read, FileShare.Read);
int bytesize = 65535; //每个包的大小
int TotalLength = (int)fsfile.Length; //文件的总长度
int PacketsNum = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(fsfile.Length) / Convert.ToDouble(bytesize))); //包的数量
int CurrentLeng = 0; //最后一个包的大小
byte[] bytedata = null;
int count = 0;
string strHead = "<PEOF>" + (char)191 + flist[j].ToString().Substring(flist[j].ToString().LastIndexOf("\\") + 1) + (char)191 + TotalLength.ToString() + (char)191 +
PacketsNum.ToString()
+ (char)191 + (TotalLength - (PacketsNum - 1) * bytesize).ToString(); //发送开始发送文件的头标志
Send(s, strHead); //发送头标志

//发送文件(如果是大文件就分包发送)
for (int k = 0; k < PacketsNum; k++)
{
if (TotalLength > bytesize)
{
CurrentLeng
= bytesize;
TotalLength
-= bytesize;
}
else
{
CurrentLeng
= bytesize;
}
bytedata
= new byte[CurrentLeng];
count
= fsfile.Read(bytedata, 0, CurrentLeng);
byte[] SendingBuffer = new byte[CurrentLeng];
SendingBuffer
= bytedata;
Send(s, SendingBuffer);
}
Send(s,
"<Over>"); //发送文件发送的结束标志
fsfile.Close();
//MessageBox.Show("OK");
}
}

 

接收端:
private void ReadCallback(IAsyncResult ai)
{
try
{
StateObject stateclient
= (StateObject)ai.AsyncState;
Socket client
= stateclient.workSocket;
int byteread = client.EndReceive(ai);
if (byteread > 0)
{
string ss = System.Text.Encoding.UTF8.GetString(stateclient.buffer);
ss
= ss.Substring(0, 6); //ss是头标志,通过该标志判断我发送的是消息还是文件
switch (ss)
{
case "<PEOF>":
//发送文件
HeadMsg = System.Text.Encoding.UTF8.GetString(stateclient.buffer, 0, byteread);
string[] sss = HeadMsg.Split((char)191);
FileName
= sss[1].ToString();
break;
case "<MEOF>":
//发送消息
HeadMsg = System.Text.Encoding.UTF8.GetString(stateclient.buffer, 0, byteread);
SetText(HeadMsg);
break;
case "<WEOF>":
//发送消息
HeadMsg = System.Text.Encoding.UTF8.GetString(stateclient.buffer);
SetText(HeadMsg);
break;
default:
ms.Write(stateclient.buffer,
0, byteread);
break;
}
//发送结束标志
if (ss == "<Over>")
{
SetFile();
//清空networkstream
ms.Dispose();
ms
= new MemoryStream();
}
stateclient.SetBuffer();
client.BeginReceive(stateclient.buffer,
0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), stateclient);
}
}
catch (Exception ex) { }
}
//保存文件
private void SetFile()
{
string[] ss = HeadMsg.Split((char)191);
int fileLen = 0;
fileLen
= (ss.Length == 5) ? Convert.ToInt32(ss[2].ToString()) : fileLen;
FileStream fs
= new FileStream(System.Windows.Forms.Application.StartupPath + "\\" + FileName, FileMode.Create);
byte[] byteD = ms.GetBuffer();
fs.Write(byteD,
0, fileLen);
fs.Close();
//MessageBox.Show("保存成功");
}
//保存消息
private void SaveInfo(string name, string content)
{
if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\" + name))
{
StreamWriter sw
= new StreamWriter(Application.StartupPath + "\\" + name, false, Encoding.GetEncoding("gb2312"));
sw.WriteLine(content);
sw.Close();
MessageBox.Show(
"保存成功");
return;
}
StreamWriter sw2
= new StreamWriter(Application.StartupPath + "\\" + name, false, Encoding.GetEncoding("gb2312"));
sw2.WriteLine(content);
sw2.Close();
MessageBox.Show(
"保存成功");
}

 

 

Pnn的主页 Pnn | 初学一级 | 园豆:90
提问于:2010-04-20 08:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册