BeginAcceptTcpClientについて、スレッドIDを取得してみた。
子スレッドは2つまで?うむむ。 ソース Imports System.Net.Sockets Imports System.Threading Public Class TestNet ' Thread signal. Public Shared tcpClientConnected As New ManualResetEvent(False) Public Shared Sub dp(s As String, ParamArray a() As String) SyncLock tcpClientConnected Debug.Print(s, a) End SyncLock End Sub Public Sub TestServerAndClient() Dim client As Thread Dim procClient As Action(Of Byte) _ = Sub(aByte As Byte) client = New Thread(New ThreadStart(Sub() Thread.Sleep(1000) Dim sckClient As TcpClient sckClient = New TcpClient() dp("cl connect {0}-{1}", Now, aByte) sckClient.Connect(Net.IPAddress.Loopback, 9000) sckClient.GetStream().WriteByte(aByte) End Sub)) client.Start() End Sub procClient(0) procClient(1) procClient(2) procClient(3) procClient(4) Dim tl As TcpListener = New TcpListener(Net.IPAddress.Loopback, 9000) tl.Start(3) DoBeginAcceptTcpClient(tl) dp("sv connect") DoBeginAcceptTcpClient(tl) dp("sv connect") DoBeginAcceptTcpClient(tl) dp("sv connect") DoBeginAcceptTcpClient(tl) dp("sv connect") DoBeginAcceptTcpClient(tl) dp("sv connect") End Sub Public Shared Sub DoBeginAcceptTcpClient(listener As TcpListener) tcpClientConnected.Reset() listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptTcpClientCallback), listener) tcpClientConnected.WaitOne() End Sub Public Shared Sub DoAcceptTcpClientCallback(ar As IAsyncResult) Dim listener As TcpListener = CType(ar.AsyncState, TcpListener) Dim client As TcpClient = listener.EndAcceptTcpClient(ar) tcpClientConnected.Set() Dim bt As Byte = client.GetStream().ReadByte() dp("child accept {0}-{1} Start {2}", Now, bt, Thread.CurrentThread.ManagedThreadId) Thread.Sleep(3000) dp("child accept {0}-{1} End", Now, bt) client.GetStream().Close() End Sub End Class cl connect 2018/03/21 5:46:56-1 cl connect 2018/03/21 5:46:56-2 cl connect 2018/03/21 5:46:56-3 cl connect 2018/03/21 5:46:56-4 スレッド 0x514 はコード 0 (0x0) で終了しました。 スレッド 0x126c はコード 0 (0x0) で終了しました。 スレッド 0x600 はコード 0 (0x0) で終了しました。 child accept 2018/03/21 5:46:57-1 Start 16 スレッド 0x1384 はコード 0 (0x0) で終了しました。 sv connect cl connect 2018/03/21 5:46:56-0 child accept 2018/03/21 5:46:57-2 Start 9 スレッド 0x2948 はコード 0 (0x0) で終了しました。 child accept 2018/03/21 5:47:00-1 End child accept 2018/03/21 5:47:00-2 End sv connect child accept 2018/03/21 5:47:00-3 Start 9 child accept 2018/03/21 5:47:03-3 End sv connect child accept 2018/03/21 5:47:03-4 Start 9 child accept 2018/03/21 5:47:06-4 End sv connect child accept 2018/03/21 5:47:06-0 Start 9 child accept 2018/03/21 5:47:09-0 End sv connect プログラム '[6396] NetTest.vshost.exe' はコード -1 (0xffffffff) で終了しました。 PR |
追試してみましたが
Thread.sleepをコメントアウトして 複数回実行してみると3スレッドの場合もありました(@_@; |
謎ですね。。。
.net frameworkはアンニュイなバグ多めです。 新しい方法を設けては、中身のよく分からない感じ。。。
【2018/03/21 20:08】| | nwpfh #92c855bfb8 [ 編集 ]
|
Hmm it looks like your website ate my first comment (it was extremely
long) so I guess I'll just sum it up what I wrote and say, I'm thoroughly enjoying your blog. I as well am an aspiring blog blogger but I'm still new to the whole thing. Do you have any helpful hints for inexperienced blog writers? I'd genuinely appreciate it. istanbul escort şirinevler escort taksim escort mecidiyeköy escort şişli escort |
|
忍者ブログ [PR] |