77范文网 - 专业文章范例文档资料分享平台

计算机网络课程设计报告文件传输协议的简单实现(4)

来源:网络收集 时间:2018-11-22 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库计算机网络课程设计报告文件传输协议的简单实现(4)在线全文阅读。

计算机网络课程设计报告文件传输协议的简单实现(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/298675.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: