网络编程实验报告.docx
《网络编程实验报告.docx》由会员分享,可在线阅读,更多相关《网络编程实验报告.docx(15页珍藏版)》请在冰豆网上搜索。
网络编程实验报告
贵州大学实验报告
学院:
计算机学院专业:
软件工程班级:
软件123班
姓名
***
学号
***
实验组
实验时间
2015-5-9
指导教师
蔡丽
成绩
实验项目名称
FTP上传下载器编程
实验目的
通过本实验掌握C#中FTP上传下载器编程的方法,了解其区别与适用场合。
实验要求
了解C#的UDP编程方法。
实验原理
使用.NET请求/响应模型的FtpWebRequest类和FtpWebResponse类实现简单的Web浏览器
实验环境
VisualStudio开发环境
实验步骤
1.设计程序界面。
2.实现程序功能。
实验内容
实现简单的Web浏览器,要求使用.NET请求/响应模型的FtpWebRequest类和FtpWebResponse类。
实验数据
服务器核心代码
oString();
="21";
=false;
}
.");
="停止";
}
else
{
();
myTcpListener=null;
();
"Ftp服务已停止!
");
=-1;
="启动";
}
}
.[点击”停止“按钮停止FTP服务]");
while(true)
{
try
{
oUpper();
param=.Trim();
}
;
RepleyCommandToUser(user,sendString);
break;
}
break;
}
}
}
}
;
=param;
;
}
RepleyCommandToUser(user,sendString);
}
;
}
}
else
{
sendString="530Usernameorpasswordincorrect.";
}
}
else
{
sendString="501PASScommandSyntaxerror.";
}
RepleyCommandToUser(user,sendString);
ateTimeFormat;
.");
(path);
AddInfo("删除成功");
sendString="250File"+filename+"hasbeendeleted.";
RepleyCommandToUser(user,sendString);
}
#endregion
#region模式设置命令
ddressList[1];
eplace('.',',');
符串来组合这四个参数得到IP地址
+temp[1]+"."+temp[2]+"."+temp[3];
;
.");
try
{
if
{
byte[]bytes=newbyte[1024];
BinaryReaderbinaryReader=newBinaryReader(fs);
intcount=(bytes,0,;
while(count>0)
{
0,count);
count=(bytes,0,;
}
}
else
{
StreamReaderstreamReader=newStreamReader(fs);
while()>-1)
{
}
}
AddInfo("...]发送完毕!
");
}
finally
{
();
}
}
.");
try
{
if
{
byte[]bytes=newbyte[1024];
BinaryWriterbinaryWriter=newBinaryWriter(fs);
intcount=0,;
while(count>0)
{
(bytes,0,count);
();
count=0,;
}
}
else
{
StreamWriterstreamWriter=newStreamWriter(fs);
while>-1)
{
();
}
}
AddInfo("...]接收完毕");
}
finally
{
();
}
}
}
}
oString();
();
=true;
=false;
=false;
=false;
=false;
=false;
}
#region键盘按下事件
privatevoidtxtUserName_KeyPress(objectsender,KeyPressEventArgse)
{
..");
strings=();
();
();
();
"传输完成");
ndsWith("."))
{
length=ftpdir[i].Length-2;
break;
}
}
for(inti=0;i<;i++)
{
s=ftpdir[i];
intindex=('\t');
if(index==-1)
{
if(length<
{
index=length;
}
else
{
continue;
}
}
stringname=(index+1);
if(name=="."||name=="..")
{
continue;
}
ontains("
")){
string[]namefield=('');
intnamefieldlength=;
stringdirname;
dirname=namefield[namefieldlength-1];
||name=="..")
{
continue;
}
ontains("
"))){
string[]namefield=('');
intnamefieldlength=;
stringfilename;
filename=namefield[namefieldlength-1];
|*.*";
if()!
=
{
return;
}
FileInfofileinfo=newFileInfo;
try
{
stringuri=GetUriString;
FtpWebRequestrequest=CreateFTPWebRequest(uri,=;
intbuflength=8196;
byte[]buffer=newbyte[buflength];
FileStreamfilestream=();
StreamresponseStream=();
"打开上传流,文件上传中...");
intcontenlength=(buffer,0,buflength);
while(contenlength!
=0)
{
(buffer,0,contenlength);
contenlength=(buffer,0,buflength);
}
();
();
FtpWebResponseresponse=GetFTPWebResponse(request);
if(response==null)
{
"服务器未响应...");
=-1;
return;
}
"上传完毕,服务器返回:
"++""+;
=-1;
("上传成功!
");
|(*.*)";
if()!
=
{
return;
}
stringfilePath=;
try
{
stringuri=GetUriString(fileName);
FtpWebRequestrequest=CreateFTPWebRequest(uri,FtpWebResponseresponse=GetFTPWebResponse(request);
if(response==null)
{
"服务器未响应...");
=-1;
return;
}
StreamresponseStream=();
FileStreamfilestream=(filePath);
intbuflength=8196;
byte[]buffer=newbyte[buflength];
intbytesRead=1;
"打开下载通道,文件下载中...");
while(bytesRead!
=0)
{
bytesRead=(buffer,0,buflength);
(buffer,0,bytesRead);
}
();
();
"下载完毕,服务器返回:
"++""+;
=-1;
("下载完成!
");
}
catch(WebExceptionex)
{
"发生错误,返回状态为:
"+;
=-1;
"下载失败");
}
}
.");
=-1;
return;
}
"文件删除成功,服务器返回:
"++""+;
ShowFtpFileAndDirectory();
}
else
{
return;
}
}
catch(WebExceptionex)
{
"发生错误,返回状态为:
"+;
=-1;
"删除失败");
}
}
#endregion
///
///变更目录操作
///
///
///
privatevoidlstboxFTPResource_MouseDoubleClick(objectsender,MouseEventArgse)
{
//点击返回上层目录
if==0)
{
if(_currentDir=="/")
{
("当前目录已经是顶层目录","",,;
return;
}
intindex=("/");
if(index==0)
{
_currentDir="/";
}
else
{
_currentDir=(0,index);
}
//每次更改目录后立即刷新资源列表
ShowFtpFileAndDirectory();
}
else
{
if>0&&"[目录]"))
{
if(_currentDir=="/")
{
_currentDir="/"+}
else
{
_currentDir=_currentDir+"/"+}
string[]_currentDirfield=('');
_currentDir=_currentDirfield[0];
//每次更改目录后立即刷新资源列表
ShowFtpFileAndDirectory();
}
}
}
}
}
启动服务器:
登陆链接:
实验总结
通过实验掌握了C#中FTP上传下载器编程的方法。
指导教师意见
签名:
年月日