温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#如何实现同步模式下的端口映射程序

发布时间:2022-06-17 09:50:57 来源:亿速云 阅读:249 作者:iii 栏目:开发技术

这篇文章主要介绍了C#如何实现同步模式下的端口映射程序的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#如何实现同步模式下的端口映射程序文章都会有所收获,下面我们一起来看看吧。

代码如下:

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.

关于“C#如何实现同步模式下的端口映射程序”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“C#如何实现同步模式下的端口映射程序”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI