view Test/RewritingTest/SocketListenerTask.cs @ 26:45ff08d59fda

update CGM
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 12 Jan 2021 21:23:23 +0900
parents 46cfeb0609c5
children 6399d784c6d1
line wrap: on
line source

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;


public class SocketListenerTask {
    Socket socket;

    public SocketListenerTask (Socket socket) {
        this.socket = socket;
    }

    public void Run () {
        // Thread thread = new Thread (new ThreadStart (MethodThread));
        // thread.Start();
        Task task = Task.Run(() => MethodThread());
    }

    private void MethodThread() {
        Socket listener = socket.Accept ();
        while (true) {
            Console.WriteLine ("Accept:" + listener.LocalEndPoint);
            Thread.Sleep(1000);
        }
    }

    public static void Main () {
        IPHostEntry host = Dns.GetHostEntry ("localhost");
        IPAddress ipAddress = host.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 11000);

        try {
            Socket ss = new Socket (ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            ss.Bind(localEndPoint);
            ss.Listen(10);

            // while (true) {
            //     Console.WriteLine("Accept:" + listener.LocalEndPoint);
            // }
         
            SocketListenerTask newThread = new SocketListenerTask(ss);
            newThread.Run ();
            
            // Console.WriteLine("fin");
            // listener.Shutdown(SocketShutdown.Both);
            // listener.Close();
        } catch (Exception e) {
            Console.WriteLine (e.ToString ());
        }

    }
}