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.IO;
using System.Threading;namespace FTP服务器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2; //以下定义自定义对象
private bool control = false;
private bool pasv = false;
private bool pasvPort = false;
private int port;
private Socket newClient;//定义套节字对象 string parameter = null;//FTP命令参数
string command = null;//FTP命令 private TcpListener  listener;//
private TcpListener  newlistener;
private int newport;
private FileStream filestream;//文件流对象

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 0;
this.label1.Text = "监听端口:";
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(80, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(328, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
// 
// label2
// 
this.label2.Location = new System.Drawing.Point(16, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 2;
this.label2.Text = "客户信息:";
// 
// richTextBox1
// 
this.richTextBox1.Location = new System.Drawing.Point(80, 56);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(328, 176);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(64, 248);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 4;
this.button1.Text = "开始服务";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(272, 248);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 32);
this.button2.TabIndex = 5;
this.button2.Text = "关闭服务";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 293);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{

} private void button1_Click(object sender, System.EventArgs e)
{
try
{
port = Int32.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("您输入的格式不对!请输入正不整数.");
}

try
{
listener = new TcpListener(port);
listener.Start(); Thread thread = new Thread(new ThreadStart(recieve));
thread.Start();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}
} private void recieve()
{
while(true)
{
newClient = listener.AcceptSocket();
if(newClient.Connected)
{
Thread thread = new Thread(new ThreadStart(round));
thread.Start();
}
}
}

解决方案 »

  1.   

    private void round()
    {
    NetworkStream netStream = new NetworkStream(newClient);
    WriteToNetStream(ref netStream,"220 FTP Server Ready!");
    while(true)
    {
    //接受信息++++++
    byte[] byteMessage = new byte[1024];
    int i = newClient.Receive(byteMessage,byteMessage.Length,0);
    string ss = System.Text.Encoding.ASCII.GetString(byteMessage);
    int x = ss.IndexOf("\r\n",0);
    int y = ss.IndexOf(" " ,0);
    string commMessage = System.Text.Encoding.ASCII.GetString(byteMessage,0,x);
    if(y!=-1)
    {
    command = commMessage.Substring(0,y);
    }
    else
    {
    command = commMessage.Substring(0,x);
    }
    command = command.ToLower();
    if (y!=-1)
    {
    parameter = commMessage.Substring(y+1,x-y-1);
    parameter = parameter.ToLower();
    }
    else
    {
    parameter = "";
    }
    this.richTextBox1.AppendText("Command=" + command + "," + "Parameter=" +parameter + "\r\n");
    commMessage.Remove(0,commMessage.Length);
    if(command =="list")
    {
    try
    {
    string[] str = new string[1024];
    string dir = null;
    for(int k=0;k<Directory.GetDirectories(parameter).Length;k++)
    {
    str[k] = Directory.GetDirectories(parameter)[k];
    dir = dir + "目录:" + str[k] + "\r\n";
    }
    for(int k=0;k<Directory.GetFiles(parameter).Length;k++)
    {
    str[k] = Directory.GetFiles(parameter)[k];
    dir+="文件:" + str[k] + "\r\n";
    }
    WriteToNetStream(ref netStream,"125 the files and directory is here");
    sendDir(ref netStream,dir);
    }
    catch
    {
    WriteToNetStream(ref netStream,"502 unsuccessful,try again");
    }
    }
    else if(command=="retr")//获得文件,下载
    {
    try
    {
    if ((pasv==false)&&(pasvPort==false))
    {
    string path = parameter;
    //在新的端口监听,进行数据转输(文件下载)
    newlistener = new TcpListener(Int32.Parse(textBox1.Text)+1);
    newlistener.Start();
    WriteToNetStream(ref netStream,"150 data link listening");
    Socket nextLink = newlistener.AcceptSocket();
    if(nextLink.Connected)
    {
    WriteToNetStream(ref netStream,"125 now thie file's data will be sended");
    NetworkStream stream = new NetworkStream(nextLink);
    transfer(ref stream,path);
    stream.Close();
    nextLink.Close();
    newlistener.Stop();
    }
    }
    else if((pasv==true)&&(pasvPort==true))
    {
    string path=parameter;
    newlistener = new TcpListener(newport);
    newlistener.Start();
    WriteToNetStream(ref netStream,"150 data link listening");
    Socket nextLink = newlistener.AcceptSocket();
    if(nextLink.Connected)
    {
    WriteToNetStream(ref netStream,"125 now the file's data while be sended");
    NetworkStream stream = new NetworkStream(nextLink);
    transfer(ref stream,path);
    stream.Close();
    nextLink.Close();
    newlistener.Stop();
    pasv=false;
    pasvPort=false;
    }
    }
    else
    {
    WriteToNetStream(ref netStream,"350 PASV OR PORT command needed");
    }
    }
    catch
    {
    WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
    }
    }
    else if(command=="pasv")
    {
    try
    {
    pasv = true;
    WriteToNetStream(ref netStream,"227 now the server pasv");
    }
    catch
    {
    WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
    }
    }
    else if (command=="port")
    {
    try
    {
    if (pasv==true)
    {
    pasvPort=true;
    newport=Int32.Parse(parameter);
    WriteToNetStream(ref netStream,"200 new data link listen begin");
    }
    else
    {
    WriteToNetStream(ref netStream,"300 PASV command needed");
    }
    }
    catch
    {
    WriteToNetStream(ref netStream,"504 the command can't be performed with the parameter");
    }
    }
    else if(command=="help")
    {
    try
    {
    WriteToNetStream(ref netStream,"215 the FTP system ready,it can the comman such aa list PORT, PASV,");
    }
    catch
    {
    WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
    }
    }
    else if (command=="quit")
    {
    try
    {
    WriteToNetStream(ref netStream,"221 commection Closed");
    newClient.Close();
    }
    catch
    {
    WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
    }
    }
    else
    {
    WriteToNetStream(ref netStream,"500 unrecognized command");
    }
    }
    } private void transfer(ref NetworkStream stream,string path)
    {
    filestream = new FileStream(path,FileMode.Open,FileAccess.Read);
    int number;
    //定义缓冲区
    byte[] bb = new byte[8];
    //循环读文件
    while((number=filestream.Read(bb,0,8))!=0)
    {
    //向客户端发送流
    stream.Write(bb,0,8);
    stream.Flush();
    bb = new byte[8];
    }
    filestream.Close();
    stream.Close();
    } private void WriteToNetStream(ref NetworkStream NetStream,string ToSend)
    {
    string stringToSend = ToSend + "\r\n";
    byte[] arrayToSend = System.Text.Encoding.ASCII.GetBytes(stringToSend.ToCharArray());
    NetStream.Write(arrayToSend,0,arrayToSend.Length);
    NetStream.Flush();
    } private void sendDir(ref NetworkStream NetStream, string Dir)
    {
    string stringToSend = Dir + "\r\n";
    byte[] arrayToSend = System.Text.Encoding.ASCII.GetBytes(stringToSend.ToCharArray());
    NetStream.Write(arrayToSend,0,arrayToSend.Length);
    NetStream.Flush();
    }

      }
    }
      

  2.   

    to:helimin19(冷风) 
    能不能解释一下你的程序?
      

  3.   

    hai用过了
    雏形还可以
    不过命令集还需要扩展
    user
    pass
    都没有
      

  4.   

    http://www.csharphelp.com/archives/archive9.html