用VS编写的FTP服务器软件,C#网络程序编程学习用。
代码:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace FtpServer
{
public partial class FtpServerForm : Form
{
TcpListener myTcpListener = null;
private Thread listenThread;
// 保存用户名和密码
Dictionary users;
public FtpServerForm()
{
InitializeComponent();
// 初始化用户名和密码
users = new Dictionary();
users.Add("admin", "admin");
// 设置默认的主目录
tbxFtpRoot.Text = "F:/MyFtpServerRoot/";
IPAddress[] ips = Dns.GetHostAddresses("");
tbxFtpServerIp.Text = ips[5].ToString();
tbxFtpServerPort.Text = "21";
lstboxStatus.Enabled = false;
}
// 启动服务器
private void btnFtpServerStartStop_Click(object sender, EventArgs e)
{
if (myTcpListener == null)
{
listenThread = new Thread(ListenClientConnect);
listenThread.IsBackground = true;
listenThread.Start();
lstboxStatus.Enabled = true;
lstboxStatus.Items.Clear();
lstboxStatus.Items.Add("已经启动Ftp服务...");
btnFtpServerStartStop.Text = "停止";
}
else
{
myTcpListener.Stop();
myTcpListener = null;
listenThread.Abort();
lstboxStatus.Items.Add("Ftp服务已停止!");
lstboxStatus.TopIndex = lstboxStatus.Items.Count - 1;
btnFtpServerStartStop.Text = "启动";
}
}
// 监听端口,处理客户端连接
private void ListenClientConnect()
{
myTcpListener = new TcpListener(IPAddress.Parse(tbxFtpServerIp.Text), int.Parse(tbxFtpServerPort.Text));
//
1