【C#】TCP 送受信

■ 受信側

デザイン

 * TextBox x 1
 * Button x 2
 * Timer x 1

サンプル

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace SampleListener
{
    public partial class Form1 : Form
    {
        private TcpListener tcpListener;
        public Form1()
        {
            InitializeComponent();

            var localAddress = IPAddress.Parse("127.0.0.1");
            this.tcpListener = new TcpListener(localAddress, 4443);
        }

        private void button3_Click(object sender, EventArgs e)
        {

            System.Reflection.Assembly assembly =
                System.Reflection.Assembly.GetExecutingAssembly();
            System.Drawing.Icon icon = new System.Drawing.Icon(
                assembly.GetManifestResourceStream("Icons.SampleIcon.png"));
            this.Icon = icon;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text =
                this.textBox1.Text + Environment.NewLine +
                "Starting!";
            try
            {
                this.timer1.Start();
                this.tcpListener.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text =
                this.textBox1.Text + Environment.NewLine +
                "Stopping!";
            try
            {
                this.timer1.Stop();
                this.tcpListener.Stop();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.tcpListener.Pending())
            {
                return;
            }

            using (var client = this.tcpListener.AcceptTcpClient())
            {
                NetworkStream stream = client.GetStream();
                byte[] buffer = new byte[255];
                string recivingData = string.Empty;

                while (true)
                {
                    if (stream.DataAvailable)
                    {
                        int length = stream.Read(buffer, 0, buffer.Length);
                        recivingData = recivingData +
                            Encoding.Default.GetString(buffer).Substring(0, length);
                    }
                    else if (!string.IsNullOrEmpty(recivingData))
                    {
                        this.textBox1.Text = this.textBox1.Text +
                            Environment.NewLine + recivingData;
                        break;
                    }
                }
                
            }
        }
    }
}

■ 送信側

 * 受信側とは別のプロジェクトで作成する

デザイン

 * TextBox x 1
 * Button x 1

サンプル

using System;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace TcpClientSample
{
    public partial class Form1 : Form
    {
        private TcpClient tcpClient;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (this.tcpClient = new TcpClient("127.0.0.1", 4443))
            {
                NetworkStream stream = this.tcpClient.GetStream();
                Byte[] buffer = Encoding.Default.GetBytes(this.textBox1.Text);
                stream.Write(buffer, 0, buffer.Length);
            }
        }
    }
}