C# Telnet Server 端

C# Telnet Server 端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//使用 Socket
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace Telnet_Server_Example
{
    public partial class Form1 : Form
    {
        int recv;
        bool Telnet_Connected = false;
        byte[] data = new byte[1024];
        int PORT = 23;
        Socket newsock;
        Socket client;
        Thread t1,t2;
        delegate void Display(string buffer);
        
        public Form1()
        {
            InitializeComponent();
        }

        private void WaitConnect()
        {
            try
            {
                PORT = Convert.ToInt16(tb_Port.Text);

                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, PORT);

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

                newsock.Bind(ipep);

                newsock.Listen(10);

                string buf = "Wait for Client...\n";

                Display d = new Display(ShowMessage);
                this.Invoke(d, new Object[] { buf });

                client = newsock.Accept();

                IPEndPoint clienttep = (IPEndPoint)client.RemoteEndPoint;

                buf = string.Format("連接 {0} Port {1}\n", clienttep.Address, clienttep.Port);
                this.Invoke(d, new Object[] { buf });

                string welcome = "Welcome Telnet Server!\n";

                Array.Clear(data, 0, data.Length);

                data = Encoding.ASCII.GetBytes(welcome);

                client.Send(data, data.Length, SocketFlags.None);

                Telnet_Connected = true;             
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void DoReceive()
        {
            Display d = new Display(ShowMessage);

            Array.Clear(data, 0, data.Length);

            while (true)
            {
                if(Telnet_Connected)
                {
                    recv = client.Receive(data);

                    Array.Resize(ref data, recv);
                    string buf = Encoding.ASCII.GetString(data);
                    this.Invoke(d, new Object[] { buf });
                    Array.Resize(ref data, 1024);
                }

                Thread.Sleep(20);
            }
        }

        private void tb_Input_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                data = Encoding.ASCII.GetBytes(tb_Input.Text + "\r\n");
                client.Send(data, data.Length, SocketFlags.None);
                tb_Input.Clear();
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            btn_Start.Enabled = false;

            t1 = new Thread(WaitConnect);
            t1.Start();
            Thread.Sleep(100);

            t2 = new Thread(DoReceive);
            t2.Start();
      
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            client.Close();
            newsock.Close();
            t2.Abort();
        }

        private void ShowMessage(string buffer)
        {
            tb_Output.AppendText(buffer);
        }
    }
}