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.Runtime.InteropServices; namespace FtpServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FtpServerForm : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.ListBox lbClients;
private System.Windows.Forms.TextBox tbSocketPort;
private System.Windows.Forms.TextBox tbClientsNum;
public const string ROOTDIR = "c:\\";
//服务器可以支持最多的连接数
public const int MAXNUM = 100;
//clients 数组保存当前在线
//internal 允许同一个命名空间中的类访问
// Hashtable 表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。
internal static Hashtable client = new Hashtable();
//该服务器默认的监听端口号
public TcpListener tcpListener;
//开始服务的标志
internal static bool SocketServiceFlag = false;
//服务器的ip地址
internal string ip;
internal int port;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public FtpServerForm()
{
//
// 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.groupBox1 = new System.Windows.Forms.GroupBox();
this.lbClients = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.tbSocketPort = new System.Windows.Forms.TextBox();
this.tbClientsNum = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnStop);
this.groupBox1.Controls.Add(this.btnStart);
this.groupBox1.Controls.Add(this.tbClientsNum);
this.groupBox1.Controls.Add(this.tbSocketPort);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.lbClients);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(472, 472);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "ftp服务器设置";
this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
//
// lbClients
//
this.lbClients.ItemHeight = 12;
this.lbClients.Location = new System.Drawing.Point(8, 16);
this.lbClients.Name = "lbClients";
this.lbClients.Size = new System.Drawing.Size(224, 436);
this.lbClients.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(248, 46);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "Socket端口号:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(248, 96);
this.label2.Name = "label2";
this.label2.TabIndex = 2;
this.label2.Text = "当前在线用户:";
//
// tbSocketPort
//
this.tbSocketPort.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbSocketPort.Location = new System.Drawing.Point(360, 48);
this.tbSocketPort.Name = "tbSocketPort";
this.tbSocketPort.TabIndex = 3;
this.tbSocketPort.Text = "21";
this.tbSocketPort.TextChanged += new System.EventHandler(this.tbSocketPort_TextChanged);
//
// tbClientsNum
//
this.tbClientsNum.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbClientsNum.Location = new System.Drawing.Point(360, 96);
this.tbClientsNum.Name = "tbClientsNum";
this.tbClientsNum.TabIndex = 4;
this.tbClientsNum.Text = "";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(264, 168);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(184, 24);
this.btnStart.TabIndex = 5;
this.btnStart.Text = "启动FTP服务器";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(264, 216);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(184, 24);
this.btnStop.TabIndex = 6;
this.btnStop.Text = "停止FTP服务器";
//
// FtpServerForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 485);
this.Controls.Add(this.groupBox1);
this.MaximumSize = new System.Drawing.Size(496, 512);
this.Name = "FtpServerForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Sever--U";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.FtpServerForm_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FtpServerForm());
} private void groupBox1_Enter(object sender, System.EventArgs e)
{
} private void FtpServerForm_Load(object sender, System.EventArgs e)
{
} private void tbSocketPort_TextChanged(object sender, System.EventArgs e)
{
this.btnStart.Enabled = (this.tbSocketPort.Text != "");
} public void btnStart_Click(object sender, System.EventArgs e)
{
Console.WriteLine("FTP服务器启动.......");
port = getValidPort(tbSocketPort.Text);
if(port<0)
{
return;
}
//获取本机局域网ip地址
IPAddress[] AddressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; if(AddressList.Length>0)
{
ip = AddressList[0].ToString();
try
{
IPAddress ipAdd = IPAddress.Parse(ip);
//创建服务器套节字
tcpListener = new TcpListener(ipAdd,port);
//开始监听服务器端口
tcpListener.Start();
//启动一个新的线程,执行方法this.StartSocketListen;
//以便在一个独立的进程中执行确认与客户端Socket连接的操作
FtpServerForm.SocketServiceFlag = true;
Thread thread = new Thread(new ThreadStart(this.StartSocketListen));
thread.Start();
this.btnStart.Enabled = false;
this.btnStop.Enabled = true;
this.tbSocketPort.Enabled = false;
}
catch(Exception ex)
{
Console.WriteLine("不能启动服务器"+ex.ToString());
MessageBox.Show(ex.Message,"");
}
}
}
private void StartSocketListen()
{
//暂时无内容
}
private int getValidPort(string port)
{
int lport;
try
{
if(port == "")
{
throw new ArgumentException("端口为空,不能启动服务器");
}
lport = System.Convert.ToInt32(port);
}
catch(Exception e)
{
Console.WriteLine("无效的端口号:"+e.ToString());
return -1; }
return lport;
}
}
}
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.Runtime.InteropServices; namespace FtpServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FtpServerForm : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.ListBox lbClients;
private System.Windows.Forms.TextBox tbSocketPort;
private System.Windows.Forms.TextBox tbClientsNum;
public const string ROOTDIR = "c:\\";
//服务器可以支持最多的连接数
public const int MAXNUM = 100;
//clients 数组保存当前在线
//internal 允许同一个命名空间中的类访问
// Hashtable 表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。
internal static Hashtable client = new Hashtable();
//该服务器默认的监听端口号
public TcpListener tcpListener;
//开始服务的标志
internal static bool SocketServiceFlag = false;
//服务器的ip地址
internal string ip;
internal int port;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public FtpServerForm()
{
//
// 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.groupBox1 = new System.Windows.Forms.GroupBox();
this.lbClients = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.tbSocketPort = new System.Windows.Forms.TextBox();
this.tbClientsNum = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnStop);
this.groupBox1.Controls.Add(this.btnStart);
this.groupBox1.Controls.Add(this.tbClientsNum);
this.groupBox1.Controls.Add(this.tbSocketPort);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.lbClients);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(472, 472);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "ftp服务器设置";
this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
//
// lbClients
//
this.lbClients.ItemHeight = 12;
this.lbClients.Location = new System.Drawing.Point(8, 16);
this.lbClients.Name = "lbClients";
this.lbClients.Size = new System.Drawing.Size(224, 436);
this.lbClients.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(248, 46);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "Socket端口号:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(248, 96);
this.label2.Name = "label2";
this.label2.TabIndex = 2;
this.label2.Text = "当前在线用户:";
//
// tbSocketPort
//
this.tbSocketPort.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbSocketPort.Location = new System.Drawing.Point(360, 48);
this.tbSocketPort.Name = "tbSocketPort";
this.tbSocketPort.TabIndex = 3;
this.tbSocketPort.Text = "21";
this.tbSocketPort.TextChanged += new System.EventHandler(this.tbSocketPort_TextChanged);
//
// tbClientsNum
//
this.tbClientsNum.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbClientsNum.Location = new System.Drawing.Point(360, 96);
this.tbClientsNum.Name = "tbClientsNum";
this.tbClientsNum.TabIndex = 4;
this.tbClientsNum.Text = "";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(264, 168);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(184, 24);
this.btnStart.TabIndex = 5;
this.btnStart.Text = "启动FTP服务器";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(264, 216);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(184, 24);
this.btnStop.TabIndex = 6;
this.btnStop.Text = "停止FTP服务器";
//
// FtpServerForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 485);
this.Controls.Add(this.groupBox1);
this.MaximumSize = new System.Drawing.Size(496, 512);
this.Name = "FtpServerForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Sever--U";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.FtpServerForm_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FtpServerForm());
} private void groupBox1_Enter(object sender, System.EventArgs e)
{
} private void FtpServerForm_Load(object sender, System.EventArgs e)
{
} private void tbSocketPort_TextChanged(object sender, System.EventArgs e)
{
this.btnStart.Enabled = (this.tbSocketPort.Text != "");
} public void btnStart_Click(object sender, System.EventArgs e)
{
Console.WriteLine("FTP服务器启动.......");
port = getValidPort(tbSocketPort.Text);
if(port<0)
{
return;
}
//获取本机局域网ip地址
IPAddress[] AddressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; if(AddressList.Length>0)
{
ip = AddressList[0].ToString();
try
{
IPAddress ipAdd = IPAddress.Parse(ip);
//创建服务器套节字
tcpListener = new TcpListener(ipAdd,port);
//开始监听服务器端口
tcpListener.Start();
//启动一个新的线程,执行方法this.StartSocketListen;
//以便在一个独立的进程中执行确认与客户端Socket连接的操作
FtpServerForm.SocketServiceFlag = true;
Thread thread = new Thread(new ThreadStart(this.StartSocketListen));
thread.Start();
this.btnStart.Enabled = false;
this.btnStop.Enabled = true;
this.tbSocketPort.Enabled = false;
}
catch(Exception ex)
{
Console.WriteLine("不能启动服务器"+ex.ToString());
MessageBox.Show(ex.Message,"");
}
}
}
private void StartSocketListen()
{
//暂时无内容
}
private int getValidPort(string port)
{
int lport;
try
{
if(port == "")
{
throw new ArgumentException("端口为空,不能启动服务器");
}
lport = System.Convert.ToInt32(port);
}
catch(Exception e)
{
Console.WriteLine("无效的端口号:"+e.ToString());
return -1; }
return lport;
}
}
}
解决方案 »
- 自定义设计器中撤销的问题
- 请问能否让MessageBox跳出的信息窗口只是出现在MDI窗体内呢?
- 配置文件中,sectionGroup到底有什么用?
- .net中有没有类似hibernate的东西啊?
- 用C#开发WINCE中应用程序问题?
- 比较有挑战性的问题:做Udp服务时,如何才能获得本地的哪一个IP地址接收到数据包的...
- c# 注册码 【望caozhy版主解答】
- FTP服务器移动文件目录,可以上传,创建目录,删除,重命名,就是做不到移动,说是权限问题
- 性能计数器的GetCategories函数
- 我是NT4系统,如何才能装Framework SDK呀。送分呀!!
- 高手请进
- vs2005中用c#建立的项目,Program.cs文件的几行语法问题?
netstat -an