C#實現(xiàn)同步模式下的端口映射程序
今天打算寫一個FtpServer玩一下的,需要看看ftp軟件常用命令形式(完整實現(xiàn)所有ftp命令太麻煩),最開始打算通過抓包看cuteftp是如何訪問ftpserver的,但要把其中的命令保存下來還得一條條復(fù)制,太麻煩,便通過proxy模式寫了一個代理程序,來獲取其交互的命令,寫了一個簡單的同步模式下的端口映射程序后,發(fā)現(xiàn)比常用的異步proxy要簡單的多,便把這段代碼貼出來,以備日后查詢:
class Program { static void Main(string[] args) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, 8000)); listener.Start(); while (true) { var client = listener.AcceptTcpClient(); Console.WriteLine("connected"); var proxy = new TcpClient(); Console.WriteLine("remote connected"); proxy.Connect(new IPEndPoint(IPAddress.Loopback, 21)); new SyncProxy("client->remote",proxy.GetStream(), client.GetStream()); new SyncProxy("remote->client",client.GetStream(), proxy.GetStream()); } } } class SyncProxy { NetworkStream read; NetworkStream write; string name; public SyncProxy(string name, NetworkStream read,NetworkStream write) { this.name = name; this.read = read; this.write = write; System.Threading.ThreadPool.QueueUserWorkItem(PipeStream); } void PipeStream(object state) { byte[] buffer = new byte[1500]; int count = 0; while (true) { try { count = read.Read(buffer, 0, buffer.Length); } catch (Exception) { count = 0; } if (count == 0) { Console.WriteLine(name+" closed"); write.Close(); break; } Console.Write(name + ": "+ Encoding.Default.GetString(buffer, 0, count)); write.Write(buffer, 0, count); } } }
通過它獲取到的cuteFtp交互命令如下:
connected
remote connected
client->remote: 220 Serv-U FTP Server v6.0 for WinSock ready...
remote->client: USER 1
client->remote: 331 User name okay, need password.
remote->client: PASS 1
client->remote: 230 User logged in, proceed.
remote->client: PWD
client->remote: 257 "/" is current directory.
remote->client: FEAT
client->remote: 211-Extension supported
client->remote: CLNT
MDTM
MDTM YYYYMMDDHHMMSS[+-TZ];filename
SIZE
SITE PSWD;EXEC;SET;INDEX;ZONE;CHMOD;MSG
REST STREAM
XCRC filename;start;end
MODE Z
211 End
remote->client: REST 0
client->remote: 350 Restarting at 0. Send STORE or RETRIEVE.
remote->client: PASV
client->remote: 227 Entering Passive Mode (127,0,0,1,29,18)
remote->client: LIST
client->remote: 150 Opening ASCII mode data connection for /bin/ls.
client->remote: 226 Transfer complete.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF+ASP.NET?SignalR實現(xiàn)簡易在線聊天功能的示例代碼
這篇文章將以一個簡單的聊天示例,簡述如何通過WPF+ASP.NET?SignalR實現(xiàn)消息后臺通知,僅供學(xué)習(xí)分享使用,如有不足之處,還請指正2022-09-09圖解如何使用C#創(chuàng)建Windows服務(wù)
本文主要介紹了圖解如何使用C#創(chuàng)建Windows服務(wù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07C# Csv實現(xiàn)基本的讀寫和轉(zhuǎn)換DataTable
本文主要介紹了C# Csv實現(xiàn)基本的讀寫和轉(zhuǎn)換DataTable,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#?將數(shù)據(jù)庫SqlServer數(shù)據(jù)綁定到類中的過程詳解
本文講述的是讀取數(shù)據(jù)庫中數(shù)據(jù)的常用做法,即將數(shù)據(jù)庫中的數(shù)據(jù)綁定到創(chuàng)建的類中,再將類綁定到DataGridView的數(shù)據(jù)源中的做法,對C#將SqlServer數(shù)據(jù)綁定到類中感興趣的朋友一起看看吧2022-06-06C#使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件
這篇文章主要為大家詳細介紹了C#如何使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件(圖片,視頻等),文中的示例代碼講解詳細,感興趣的可以了解一下2022-07-07C#中泛型舉例List<T>與DataTable相互轉(zhuǎn)換
這篇文章介紹了C#中泛型舉例List<T>與DataTable相互轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05