他们说另开个线程,可我开了线程也不管事呀
private void upload_Click(object sender, System.EventArgs e)
{
try
{
SocketBool=false;
//给dataseid和metaviewid附随机值
for(int i=0;i<2;i++)
{
Guid guid=Guid.NewGuid();
tb[i].Text=guid.ToString();//temp.ToString();
tb[i].Enabled=false;
}
//判断输入元数据的有效性
if(!MetaValide(tb,typeb))
{
MessageBox.Show("输入类型与提示类型不符!");
return;
} //判断主文件/mainfile,上传文件/fileNames,格式/format,是否为空
if(filelist.Items.Count==0)
MessageBox.Show("未选择上传文件");
else
{
if(mainlist.Items.Count==0) MessageBox.Show("未选择主文件格式");
else
{
Thread thread=new Thread(new ThreadStart(SendFiles));
thread.Start();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
SocketBool=true;
panel2.Visible=false;
CloseSocket();
SendfileTimer.Close();
}
}
上传文件的代码载函数:SendFiles()
private void upload_Click(object sender, System.EventArgs e)
{
try
{
SocketBool=false;
//给dataseid和metaviewid附随机值
for(int i=0;i<2;i++)
{
Guid guid=Guid.NewGuid();
tb[i].Text=guid.ToString();//temp.ToString();
tb[i].Enabled=false;
}
//判断输入元数据的有效性
if(!MetaValide(tb,typeb))
{
MessageBox.Show("输入类型与提示类型不符!");
return;
} //判断主文件/mainfile,上传文件/fileNames,格式/format,是否为空
if(filelist.Items.Count==0)
MessageBox.Show("未选择上传文件");
else
{
if(mainlist.Items.Count==0) MessageBox.Show("未选择主文件格式");
else
{
Thread thread=new Thread(new ThreadStart(SendFiles));
thread.Start();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
SocketBool=true;
panel2.Visible=false;
CloseSocket();
SendfileTimer.Close();
}
}
上传文件的代码载函数:SendFiles()
解决方案 »
- 网络Socket编程(急)
- BindingSource的Filter属性
- 如何用C#实现数据库的实时备份
- 连接数据库时发生未将对象引用设置到对象的实例的情况
- 小弟急需免费的ASP.net空间,小一点没关系
- 如何取word文档的属性?
- datagrid中的数据修改并保存后,发现当前正编辑的单元格并没有保存
- C# axWebBrowser用Navigate方法打开url的同时怎么post数据
- 在线等待,即时聊天工具中从数据库中返回好友的函数是?
- IIS容器下,有没有人尝试过获得剪贴板的数据呢,即Clipboard.GetDataObject
- 怎样在代码中对两个文件进行对比校验
- 关于使用C#制作浏览器的问题?谁能提供点相关资料?有源代码最好了!包括使用的控件、组件和类等等,多多益善了?什么都行?
private void SendFiles()
{
try
{
//设置文件传输开始.计时器
Over=false;
status.Text="正在准备上传,请稍后.......";
panel2.Visible=true;
SetSendFileTimer();
//SendTimer timer=new SendTimer(); //存储传送文件的随机名
fileID=new string[filelist.Items.Count+1];
//把要传输的文件列表放入Files,把个文件的随机名字写入fileID
string[] Files=new string[filelist.Items.Count+1];
for(int p=0;p<Files.Length-1;p++)
{
Files[p]=filelist.Items[p].ToString();
}
//把xml文件加入到Files和fileID中的最后一个
Files[Files.Length-1]=clientXmlDirect+MetaXml;
Guid gd=Guid.NewGuid();
fileID[fileID.Length-1]=gd.ToString();
//把要发送的文件个数以filecount;count;的形式发送到服务端
//发送文件命令:0;filecount;个数
pbytes[0]=0;//Encoding.UTF8.GetBytes("0")[0];
pbytes[1]=2;//Encoding.UTF8.GetBytes("0")[0];
byte[] filecount=Encoding.ASCII.GetBytes(";"+"filecount;"+(Files.Length).ToString()+";");
for(int i=0;i<filecount.Length;i++) pbytes[i+2]=filecount[i];
//传输选择文件
SendBool=true;
SendfileTimer.Start();
//连接服务端
connect();
Send(conn_sock,pbytes);
for(int i=0;i<Files.Length;i++)
{ //如果检测到定时器已到时,跳出循环
if(timerStop||SocketBool)
{
CloseSocket();
break;
};
status.Text="正在传输第"+(i+1).ToString()+"个文件,请稍后......";
//开始传输
byte[] qbytes=new byte[num];
bool Startflag=true;
int index=Files[i].LastIndexOf(".");
int count=Files[i].Length-index;
string name=fileID[i]+Files[i].Substring(index,count);
byte[] filename=Encoding.UTF8.GetBytes(name);
sr=new FileStream(Files[i],FileMode.Open,FileAccess.Read);
long allmybyte=sr.Length;
while(allmybyte>0)
{
byte[] bytes=new byte[num];
//读入bytes的数据的当前位置
int point=6;
bytes[0]=1;//bytes[0]=1;
int myByte;
if(Startflag==true)
{
//把文件名加入bytes
for(int p=0;p<filename.Length;p++) bytes[p+6]=filename[p];
bytes[3]=Convert.ToByte(filename.Length);
point+=filename.Length;
//只有头
if((num-filename.Length-6)<allmybyte)
{
bytes[1]=0;//Encoding.UTF8.GetBytes("0")[0];
bytes[2]=1;//Encoding.UTF8.GetBytes("1")[0];
myByte=sr.Read(bytes,point,bytes.Length-point);
for(int p=(myByte+point);p<num;p++) bytes[p]=0;
//MessageBox.Show("first");
}
else
{
//又头又有尾
bytes[1]=1;//Encoding.UTF8.GetBytes("1")[0];
bytes[2]=1;//Encoding.UTF8.GetBytes("1")[0];
myByte=sr.Read(bytes,point,bytes.Length-point);
//判断是不是读满了缓冲
for(int p=(myByte+point);p<num;p++) bytes[p]=0;
bytes[4]=Convert.ToByte((allmybyte)/256);
bytes[5]=Convert.ToByte((allmybyte)%(256));
}
Startflag=false;
}
else
{
//如果文件只有尾
if(allmybyte<(num-6))
{
bytes[1]=1;
bytes[2]=0;
bytes[4]=Convert.ToByte((allmybyte)/256);
bytes[5]=Convert.ToByte((allmybyte)%(256));
myByte=sr.Read(bytes,point,bytes.Length-point);
for(int p=(myByte+point);p<num;p++) bytes[p]=0;
}
//没有头没有尾
else
{
bytes[1]=0;
bytes[2]=0;
myByte=sr.Read(bytes,point,bytes.Length-point);
for(int p=(myByte+point);p<num;p++) bytes[p]=0;
//Send(conn_sock,bytes);
}
}
if(!timerStop&&!SocketBool)
{
Send(conn_sock,bytes);
allmybyte-=myByte;
}
else
{
CloseSocket();
break;
}
}
sr.Close();
//传输结束
}
SendBool=false;
if((!timerStop)&&(!SocketBool))
{
ReceiveBool=true;
SendfileTimer.Stop();
// 接收结束符数据
status.Text="正在等待服务器端回应,请稍后....";
StateObject state=new StateObject();
state.workSocket=conn_sock;
recieveDone.Reset();
conn_sock.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback),state);
recieveDone.WaitOne();
}
Over=true; }
catch(Exception ex)
{
MessageBox.Show(ex.Message);
SocketBool=true;
panel2.Visible=false;
CloseSocket();
SendfileTimer.Close();
}
}
private void Send(Socket Client,byte[] bytes)//Socket Client,byte[] bytes)
{
try
{
//发送字节数组未满1024,用0填充
for(int i=0;i<1024;i++)
{
if(i<bytes.Length)
{
SendByte[i]=bytes[i];
}
else SendByte[i]=0;
}
//MessageBox.Show(content.Substring(1,9));
if((Client!=null)&&(Client.Connected)&&(!SocketBool))
{
sendDone.Reset();
Client.BeginSend(SendByte,0,SendByte.Length,0,new AsyncCallback(SendCallback),Client);
sendDone.WaitOne();
} }
catch(SocketException ex)
{
SocketBool=true;
CloseSocket();
MessageBox.Show(ex.Message);
panel2.Visible=false;
SendfileTimer.Close();
}
} //发送数据回调函数
private void SendCallback(IAsyncResult ar)
{
try
{
if(!SocketBool)
{
Socket client=(Socket)ar.AsyncState;
int bytesSent=client.EndSend(ar);
if(bytesSent!=1024)
MessageBox.Show("不足");
}
sendDone.Set();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
SocketBool=true;
SendfileTimer.Close();
panel2.Visible=false;
CloseSocket();
}
}