C#计算机网络编程实验十三Word文档下载推荐.docx
- 文档编号:16618199
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:21
- 大小:30.09KB
C#计算机网络编程实验十三Word文档下载推荐.docx
《C#计算机网络编程实验十三Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C#计算机网络编程实验十三Word文档下载推荐.docx(21页珍藏版)》请在冰豆网上搜索。
{
publicpartialclassForm1:
Form
{
TcpListenermylistener;
publicForm1()
InitializeComponent();
CheckForIllegalCrossThreadCalls=false;
}
privatevoidbutton1_Click(objectsender,EventArgse)
this.listbox.Items.Add("
开始监听...."
);
mylistener=newTcpListener(IPAddress.Parse("
127.0.0.1"
),21);
mylistener.Start();
Threadmythread=newThread(newThreadStart(ReceiveData));
mythread.Start();
privatevoidReceiveData()
TcpClientnewClient;
while(true)
try
newClient=mylistener.AcceptTcpClient();
catch
mylistener.Stop();
break;
Receivetp=newReceive(newClient,reflistbox,reftextBox1);
Threadthread=newThread(newThreadStart(tp.processService));
thread.Start();
publicclassReceive
privateListBoxlistbox;
privateTextBoxtextbox;
privateTcpClientcontroltc;
NetworkStreamcontrolns;
StreamReadercontrolsr;
StreamWritercontrolsw;
publicReceive(TcpClienttcpclient,refListBoxlistbox,refTextBoxtextbox)
this.controltc=tcpclient;
this.listbox=listbox;
this.textbox=textbox;
controlns=controltc.GetStream();
controlsr=newStreamReader(controlns,System.Text.Encoding.Unicode);
controlsw=newStreamWriter(controlns,System.Text.Encoding.Unicode);
privatevoidcloscontrolconnection()
this.controlns.Close();
this.controlsr.Close();
this.controlsw.Close();
this.controltc.Close();
publicvoidprocessService()
controlsw.WriteLine("
220"
controlsw.Flush();
listbox.Items.Add("
有用户连接,发送:
TcpClientdatatc=null;
NetworkStreamdatans=null;
StreamReaderdatasr=null;
StreamWriterdatasw=null;
stringstr=controlsr.ReadLine();
收到:
"
+str);
stringcommand="
;
stringparameter="
intindex=str.IndexOf("
"
if(index>
-1)
command=str.Substring(0,index);
parameter=str.Substring(index+1);
parameter=parameter.Replace("
server:
textbox.Text);
else
command=str;
FileStreamfs=null;
switch(command)
case"
USER"
:
if(parameter=="
ftpuser"
)
331"
421"
this.closcontrolconnection();
return;
PASS"
ftppass"
230"
PASV"
227"
intdataport=GetAvailablePort();
PORT"
+dataport);
TcpListenerdata1=newTcpListener(IPAddress.Parse("
),dataport);
data1.Start();
datatc=data1.AcceptTcpClient();
datans=datatc.GetStream();
datasr=newStreamReader(datans);
datasw=newStreamWriter(datans);
str=controlsr.ReadLine();
LIST"
string[]dir=Directory.GetDirectories(parameter);
stringsendstr="
for(inti=0;
i<
dir.Length;
i++)
dir[i]=dir[i].Replace(textbox.Text,"
sendstr+=dir[i]+"
@"
if(sendstr.Length>
0)
sendstr=sendstr.Substring(0,sendstr.Length-1);
125"
发送:
datasw.WriteLine(sendstr);
datasw.Flush();
发送目录名:
+sendstr);
string[]files=Directory.GetFiles(parameter);
sendstr="
files.Length;
files[i]=files[i].Replace(textbox.Text,"
server"
sendstr+=files[i]+"
发送文件名:
226"
datatc.Close();
datans.Close();
datasr.Close();
datasw.Close();
RETR"
fs=newFileStream(parameter,FileMode.Open,FileAccess.Read);
datasw.WriteLine(fs.Length.ToString());
+parameter+"
+fs.Length.ToString()+"
字节"
byte[]bytef=newbyte[fs.Length];
fs.Seek(0,SeekOrigin.Begin);
intfileLen=Convert.ToInt32(fs.Length);
inttrn=0;
intrn=0;
while((rn=fs.Read(bytef,trn,fileLen-trn))!
=0)
trn+=rn;
fs.Close();
senddata(datatc.Client,bytef);
QUIT"
221"
关闭与该用户的链接"
catch(Exceptionex)
发生异常"
+ex.Message);
publicstaticIListPortIsUsed()
IPGlobalPropertiesipGlobalProperties=IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[]ipsTCP=ipGlobalProperties.GetActiveTcpListeners();
IPEndPoint[]ipsUDP=ipGlobalProperties.GetActiveUdpListeners();
TcpConnectionInformation[]tcpConnInfoArray=ipGlobalProperties.GetActiveTcpConnections();
IListallPorts=newArrayList();
foreach(IPEndPointepinipsTCP)allPorts.Add(ep.Port);
foreach(IPEndPointepinipsUDP)allPorts.Add(ep.Port);
foreach(TcpConnectionInformationconnintcpConnInfoArray)allPorts.Add(conn.LocalEndPoint.Port);
returnallPorts;
publicstaticintGetAvailablePort()
IListportUsed=PortIsUsed();
intMAX_PORT=65535;
intBEGIN_PORT=1024;
for(inti=BEGIN_PORT;
MAX_PORT;
if(!
portUsed.Contains(i))
returni;
return-1;
publicstaticintsenddata(Socketclient,byte[]data)
inttotal=0;
intsize=data.Length;
intdataleft=size;
intsent;
byte[]datasize=newbyte[4];
datasize=BitConverter.GetBytes(size);
sent=client.Send(datasize);
while(total<
size)
sent=client.Send(data,total,dataleft,SocketFlags.None);
total+=sent;
dataleft-=sent;
returntotal;
privatevoidbutton2_Click_1(objectsender,EventArgse)
privatevoidForm1_FormClosing_1(objectsender,FormClosingEventArgse)
button2_Click(null,null);
privatevoidlistbox_SelectedIndexChanged(objectsender,EventArgse)
}
客户:
namespaceFTPClient
privateTcpClienttc;
privateNetworkStreamns;
privateStreamReadersr;
privateStreamWritersw;
privatestaticstringext=null;
privateLabellbdf;
privatevoidcloseControlConnection()
tc.Close();
ns.Close();
sr.Close();
sw.Close();
privatevoidbuttonCon_Click(objectsender,EventArgse)
tc=newTcpClient("
21);
catch(Exception)
MessageBox.Show("
与服务器建立连接失败!
ns=tc.GetStream();
sr=newStreamReader(ns,Encoding.Unicode);
sw=newStreamWriter(ns,Encoding.Unicode);
stringstr=sr.ReadLine();
listBoxInfo.Items.Add("
sw.WriteLine("
USERftpuser"
sw.Flush();
str=sr.ReadLine();
if(str=="
用户名不正确!
closeControlConnection();
PASSftppass"
密码不正确!
GetDirAndFiles(@"
\"
privatevoidbuttonUpDir_Click(objectsender,EventArgse)
stringpath=groupDir.Text;
path=path.Substring(0,path.LastIndexOf("
\\"
));
intnum=path.LastIndexOf("
path=path.Substring(0,num+1);
GetDirAndFiles(path);
privatevoidbuttonUncon_Click(objectsender,EventArgse)
发送:
收到:
listBoxInfo.SelectedIndex=listBoxInfo.Items.Count-1;
privatevoidlistBoxDir_SelectedIndexChanged(objectsender,EventArgse)
if(listBoxDir.SelectedIndex==-1)
GetDirAndFiles(groupDir.Text);
GetDirAndFiles(listBoxDir.SelectedItem.ToString());
privatevoidGetDirAndFiles(stringpath)
groupDir.Text=path;
if(path==@"
buttonUpDir.Enabled=false;
buttonUpDir.Enabled=true;
TcpClientdatatc=getdataconnection();
NetworkStreamdatans=datatc.GetStream();
StreamReaderdatasr=newStreamReader(datans);
//获取目录和文件列表
LIST"
+path);
listBoxInfo.SelectedIndex=this.listBoxInfo.Items.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 计算机网络 编程 实验 十三