0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio | WPF > TCPサーバ > echo server > 1対1通信 >Thread処理 | link: 自分のIPアドレスを取得する

Last updated at Posted at 2017-12-04
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

WPFでTCP echo serverを実装してみる。

参考

処理概要

code

MainWindow.xaml
<Window x:Class="_171204_t1710_TCPechoServer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_171204_t1710_TCPechoServer"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="B_connect" Content="Connect" Height="30" Click="B_connect_Click"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// 以下を追加した
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace _171204_t1710_TCPechoServer
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {

        private Thread rcvThr;
        TcpListener tcpListener;
        private readonly int kPort_recv = 7000;
        private bool stopThr = false;

        public MainWindow()
        {
            InitializeComponent();
        }

        private static string getMyIpaddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            return null;
        }

        private void B_connect_Click(object sender, RoutedEventArgs e)
        {
            this.Title = "Connected";

            string ipstr = getMyIpaddress();
            System.Net.IPAddress ipadr = System.Net.IPAddress.Parse(ipstr);
            tcpListener = new TcpListener(ipadr, kPort_recv);
            tcpListener.Start();

            rcvThr = new Thread(new ThreadStart(FuncRcvData));
            rcvThr.Start();
        }

        private void FuncRcvData()
        {
            while (stopThr == false)
            {
                TcpClient client = tcpListener.AcceptTcpClient();

                if (client.Connected == false)
                {
                    continue;
                }
                using (var netStream = client.GetStream())
                {
                    using (var sReader = new StreamReader(netStream, Encoding.UTF8))
                    {
                        var sWriter = new StreamWriter(netStream, Encoding.UTF8);

                        do
                        {
                            string str = sReader.ReadLine();
                            if (str == null)
                            {
                                break;
                            }
                            sWriter.WriteLine(str);
                            sWriter.Flush();
                            //
                            Console.WriteLine(str);
                        } while (true);
                        sWriter.Close();
                    }
                }
                client.Close();
            }
        }
    }
}

動作確認

NonSoftさんの「TCP/IPテストツール」にて動作確認しました。
提供感謝です。

気づいた事項

  • 127.0.0.1では受信できない
    • 自分のIPアドレスが必要
    • そのため自分のIPアドレス取得方法を探した
    • tcpListener = new TcpListener(IPAddress.Any, kPort_recv);で良かった。
  • WriteLine()でecho backするときはFlush()をコールする

改善点

1つのクライアントと接続中に別のクライアントへの応答ができない。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?