C#基于SOCKET通信开发的Telnet模拟工具

本文介绍了一款使用C#基于SOCKET通信开发的Telnet模拟工具,该工具旨在辅助进行SMTP、POP3和IMAP协议的测试。通过关键代码展示其实现过程。
摘要由CSDN通过智能技术生成
最近在折腾网络方面的通讯协议,每次熟悉协议的时候,总是习惯用telnet来测试,但是在测试的过程中,很纠结的是,有的地方输入不能

出错,一出错就得重新输入,而且还不能像CMD命令一样,可以通过方向键回到之前的输入。在痛苦了多次后,决定做一个类似Telnet功能的

模拟工具,更确切的说是基于SOCKET的网络通信工具,以便更好的进行SMTP/POP3/IMAP的测试。实现在的关键代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using UtilSp.ClassLib;
using System.Linq.Expressions;
using System.Threading;

namespace SocketLink
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private Socket socket_ = null;
        private Helper helper_ = new Helper();//In order to automatically update data to UI with binding property.
        private int receiveLen_max_ = 0xFFFF;
        private Thread receiveThread_ = null;
        private Encoding receiveEncoding_ = Encoding.UTF8;
        private bool needConvertBase64Received_ = false;
        private string lineBreak_ = "\r\n";

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            try
            {
                string host = textBoxHost.Text;
                string port = textBoxPort.Text;
                if (string.IsNullOrEmpty(host))
                {
                    showMessage(Tip.hostEmpty);
                    return;
                }

                if (string.IsNullOrEmpty(port))
                {
                    showMessage(Tip.portEmpty);
                    return;
                }

                if (!IpPortSp.isPort(port))
                {
                    MessageBox.Show(Tip.portIllegal);
                    return;
                }


                socket_ = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                socket_.Connect(host, Convert.ToInt32(port));
                if (!socket_.Connected)
                {
                    showBackInfo(Tip.connectFail);
                    return;
                }

                receiveThread_ = new Thread(receiveCallBack);//Create thread to listen receive from socket.
                receiveThread_.IsBackground = true;
                receiveThread_.Start();

                helper_.connectEnabled_pro = false;
                helper_.disconnectEnabled_pro = true;
                helper_.sendEnabled_pro = true;
                showBackInfo(Tip.connectOK);
            }
            catch (System.Exception ex)
            {
                showBackInfo(ex.Message);
            }
        }

        private void showMessage(string message)
        {
            MessageBox.Show(message);
        }

        private void showBackInfo(string backInfo, bool isNeedInvoke = false)
        {
            if (isNeedInvoke)
            {
                this.Invoke((Action)(() =>
                {
                    helper_.backInfo_pro += backInfo + "\r\n";               
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值