设为首页 收藏本站
查看: 1108|回复: 0

毕业设计FTP服务器端

[复制链接]

尚未签到

发表于 2015-5-29 10:30:53 | 显示全部楼层 |阅读模式
  using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
  using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
  namespace Client
{
///
/// Form1 的摘要说明。
///
public class ftpS : System.Windows.Forms.Form
{
TcpListener myListener;
  private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button buttonStart;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;

  public ftpS()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
  ///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
  #region Windows 窗体设计器生成的代码

private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.buttonStart = new System.Windows.Forms.Button();
this.buttonStop = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(24, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(368, 184);
this.listBox1.TabIndex = 0;
//
// buttonStart
//
this.buttonStart.Location = new System.Drawing.Point(256, 232);
this.buttonStart.Name = "buttonStart";
this.buttonStart.TabIndex = 1;
this.buttonStart.Text = "开始监听";
this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
//
// buttonStop
//
this.buttonStop.Enabled = false;
this.buttonStop.Location = new System.Drawing.Point(352, 232);
this.buttonStop.Name = "buttonStop";
this.buttonStop.TabIndex = 1;
this.buttonStop.Text = "停止监听";
this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.listBox1);
this.groupBox1.Location = new System.Drawing.Point(16, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(408, 216);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "传递情况";

//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 232);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 3;
this.label1.Text = "主目录";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 232);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(152, 21);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "d:\\";
//
// ftpS
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(440, 261);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.buttonStart);
this.Controls.Add(this.buttonStop);
this.Name = "ftpS";
this.Text = "FTPServer";
this.Closing += new System.ComponentModel.CancelEventHandler(this.ftpS_Closing);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
  }
#endregion
  private void buttonStart_Click(object sender, System.EventArgs e)
{
this.listBox1.Items.Add("开始监听……");
myListener=new TcpListener(2770);//端口号2770
myListener.Start();
Thread myThread=new Thread(new ThreadStart(ReceiveData));
myThread.Start();
this.buttonStart.Enabled = false;
this.buttonStop.Enabled =true;
}
  private void ReceiveData()
{
TcpClient newClient;
while(true)
{
try
{
newClient=myListener.AcceptTcpClient();//等待用户进入
}
catch
{
myListener.Stop();//当单击“停止监听”或者退出时AcceptTcpClient()会产生异常
break;
}
Receive tp=new Receive(newClient,ref listBox1,ref textBox1);
Thread thread=new Thread(new ThreadStart(tp.processService));
thread.Start();
}
}
  private void buttonStop_Click(object sender, System.EventArgs e)
{
try
{
myListener.Stop();
}
catch
{}
this.buttonStop.Enabled = false;
this.buttonStart.Enabled = true;
}
  private void ftpS_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
buttonStop_Click(null,null);
}
}
  public class Receive
{
private TcpClient client;
private ListBox listbox;
private TextBox textbox;
public Receive(TcpClient tcpclient,ref ListBox listbox,ref TextBox textbox)
{
client=tcpclient;
this.listbox=listbox;
this.textbox=textbox;
}

  public void processService()
{
NetworkStream netStream=client.GetStream();
StreamReader sr=new StreamReader(netStream,System.Text.Encoding.Unicode);
StreamWriter sw=new StreamWriter(netStream,System.Text.Encoding.Unicode);
sw.WriteLine("220");
sw.Flush();
listbox.Items.Add("有用户连接,发送:220");
while(true)
{
string str="";
try
{
str=sr.ReadLine();//当用户非正常断开连接时,可能会产生异常
}
catch
{
listbox.Items.Add("用户意外断开连接。");
break;
}
//当用户非正常断开连接时,即使不出现异常,接收字符串也会为null
if(str==null)
{
listbox.Items.Add("用户意外断开连接。");
break;
}
else
{
listbox.Items.Add("收到:"+str);
}
string command="";
string parameter="";
int index=str.IndexOf(" ");
if(index>-1)
{
command=str.Substring(0,index);
parameter=str.Substring(index+1);
//将用户发送的虚拟目录转换为实际目录
parameter=parameter.Replace("server:",textbox.Text);
}
else
{
command=str;
}
FileStream fs=null;
switch(command)
{
case "LIST":
//LIST表示用户希望得到指定目录下的子目录列表
string[] dir=Directory.GetDirectories(parameter);
string sendstr="";
for(int i=0;i

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71826-1-1.html 上篇帖子: Please enter a valid URL. Protocol is required (http://, https:// or ftp://) 下篇帖子: FTP下载
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表