view Test/RewritingTest/SocketListenerThread.cs @ 22:970c7f587126

update AcceptThread
author riono <e165729@ie.u-ryukyu.ac.jp>
date Thu, 03 Dec 2020 19:53:18 +0900
parents Test/RewritingTest/SocketLisnerThread.cs@d488eb23a29f
children 7575980bffc9
line wrap: on
line source

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class SocketListenerThread {
    Socket socket;

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

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

    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);
            // }
         
            SocketListenerThread newThread = new SocketListenerThread (ss);
            newThread.Run ();
            
            // Console.WriteLine("fin");
            // listener.Shutdown(SocketShutdown.Both);
            // listener.Close();
        } catch (Exception e) {
            Console.WriteLine (e.ToString ());
        }

    }
}