ToolStripProgressBar TspBar, ref ToolStripStatusLabel Speed) {
wordSocket = workSock; fileName = FileName;
buffer = new byte[bufferSize];
downloadTh = new Thread(new ThreadStart(Download)); uploadTh = new Thread(new ThreadStart(Upload)); tspBar = TspBar; speed = Speed; }
public void StartDownload() {
upOrDown = 1; downloadTh.Start(); timer1.Enabled = true; timer1.Interval = 100;
timer1.Tick += new EventHandler(timer1_Tick); startTime = System.Environment.TickCount; timer1.Start(); }
public void StartUpload() {
upOrDown = 0; uploadTh.Start(); timer1.Enabled = true; timer1.Interval = 50;
timer1.Tick += new EventHandler(timer1_Tick); startTime = System.Environment.TickCount; timer1.Start(); }
void timer1_Tick(object sender, EventArgs e) {
int spentTime = System.Environment.TickCount - startTime; if (spentTime != 0) {
if (upOrDown == 1) {
double sp = Convert.ToDouble(receivecount) / Convert.ToDouble(spentTime) * 1000 / 1024; if (sp < 1024)
speed.Text = tspBar.Value.ToString() + \下载速度:\ + string.Format(\, sp) + \;
else speed.Text = tspBar.Value.ToString() + \下载速度:\ + string.Format(\, sp / 1024) + \;
15
} else {
double sp = Convert.ToDouble(sendCount) / Convert.ToDouble(spentTime) * 1000 / 1024; if (sp < 1024)
speed.Text = tspBar.Value.ToString() + \上传速度:\ + string.Format(\, sp) + \;
else speed.Text = tspBar.Value.ToString() + \上传速度:\ + string.Format(\, sp / 1024) + \; } } }
private void Download() { try {
string ins = \ + fileName;
byte[] data = Encoding.BigEndianUnicode.GetBytes(ins); wordSocket.Send(data, data.Length, SocketFlags.None);//发送下载请求
string reFileName = savePath + \ + GetFileName(fileName);
Directory.CreateDirectory(savePath);
FileStream fs = new FileStream(reFileName, FileMode.Create, FileAccess.Write);
wordSocket.Receive(buffer, 8, SocketFlags.None);//接收文件大小
long filesize = BitConverter.ToInt64(buffer, 0); while (receivecount < filesize)//接收文件 {
int readcount = wordSocket.Receive(buffer, bufferSize, SocketFlags.None);
fs.Write(buffer, 0, readcount); receivecount += readcount; tspBar.Value =
Convert.ToInt32(Convert.ToDouble(receivecount) / Convert.ToDouble(filesize) * 100); }
tspBar.Value = 100;
timer1_Tick(new object(), new EventArgs()); fs.Close(); timer1.Stop();
StaticValue.isBusy = false;
16
} catch {
MessageBox.Show(\连接断开.\); timer1.Stop();
if (wordSocket.Connected == true) {
wordSocket.Shutdown(SocketShutdown.Both); wordSocket.Close(); timer1.Stop();
StaticValue.isBusy = false; } } }
private void Upload() { try {
string shortFileName =
fileName.Substring(fileName.LastIndexOf('\\\\') + 1, fileName.Length - fileName.LastIndexOf('\\\\') - 1);
string serverFileName = StaticValue.curServerPath + \ + shortFileName;//指定上传到服务器的哪个路径
string ins = \ + serverFileName;
byte[] byins = Encoding.BigEndianUnicode.GetBytes(ins); wordSocket.Send(byins, byins.Length, SocketFlags.None);//发送上传请求及完整文件名
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
long size = fs.Length;
byte[] bysize = BitConverter.GetBytes(size);
wordSocket.Send(bysize, 8, SocketFlags.None);//发送上传文件大小
BinaryReader br = new BinaryReader(fs); while (sendCount < size)//发送文件 {
int readcount = br.Read(buffer, 0, bufferSize); sendCount += readcount;
wordSocket.Send(buffer, readcount, SocketFlags.None); tspBar.Value =
Convert.ToInt32(Convert.ToDouble(sendCount) / Convert.ToDouble(size) * 100);
}
tspBar.Value = 100;
17
timer1_Tick(new object(), new EventArgs()); fs.Close(); timer1.Stop();
StaticValue.isBusy = false; }
catch (Exception e) {
MessageBox.Show(\连接断开.\);
wordSocket.Shutdown(SocketShutdown.Both); wordSocket.Close(); timer1.Stop();
StaticValue.isBusy = false; } }
public string GetFileName(string fileName) {
return fileName.Substring(fileName.LastIndexOf('\\\\')+1, fileName.Length - fileName.LastIndexOf('\\\\')-1); }
public string GetFileType(string fileName) {
return fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.')); } } }
//下面给出各按钮点击事件 ///
/// 连接服务器按钮点击事件 ///
///
private void btn_Connect_Click(object sender, EventArgs e) {
hostIPAddress = IPAddress.Parse(ttxb_IPAdd.Text); int port =Convert.ToInt32( ttxb_port.Text); Server = new IPEndPoint(hostIPAddress, port); sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try {
sock.Connect(Server);//连接服务器
toolStripStatusLabel1.Text = \与远程主机\ + ttxb_IPAdd.Text + \ + ttxb_port.ToString() + \连接成功\;
18
RefreshListView(GetDtListArray(\, false));//获取服务器根目录
connectDone = true;
tsBtn_Connect.Enabled = false; tsBtn_DisConnect.Enabled = true; } catch {
MessageBox.Show(\连接失败.\); if (sock.Connected == true) {
sock.Shutdown(SocketShutdown.Both); sock.Close(); }
} }
///
///
private void btn_GetFile_Click(object sender, EventArgs e) {
DownLoad(listView1);//调用自定义方法下载文件
} private void DownLoad(ListView listView1) {
if (listView1.SelectedIndices.Count > 0 &&
listView1.SelectedIndices[0] != 0)//有选定项且选定的不是\返回上层\
{
string size=listView1.SelectedItems[0].SubItems[1].Text; if ( size!= \)//如果选定的是文件 {
listView2.Items.Add(listView1.
SelectedItems[0].SubItems[0].Text);
listView2.Items[listView2.Items.Count-1] .SubItems.Add(size);//将文件大小加入listView
listView2.Items[listView2.Items.Count-1]
.ImageIndex =ICOSearcher.GetIcoIndex(listView1 .SelectedItems[0].SubItems[0].Text);//获取该文件的
图标
int index = listView1.SelectedIndices[0] - 1;
19