view Test/RewritingTest/SocketListenerThread.cs @ 71:1169915705ab default tip

fix TopologyNode connect
author KaitoMaeshiro <aosskaito@cr.ie.u-ryukyu.ac.jp>
date Sun, 06 Feb 2022 16:47:41 +0900
parents 7575980bffc9
children
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 (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 ());
        }

    }
}