忍者ブログ
  • 2019.08
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 2019.10
重複して始まらないBeginAcceptTcpClient その2
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
【2018/03/21 06:01 】 | javascript | 有り難いご意見(7)
                                    
<<LVMを試してみる | ホーム | 重複して始まらないBeginAcceptTcpClient>>
有り難いご意見
無題
追試してみましたが
Thread.sleepをコメントアウトして
複数回実行してみると3スレッドの場合もありました(@_@;
【2018/03/21 17:15】| URL | ゼンガイチ #9b12d9711b [ 編集 ]


無題
謎ですね。。。
.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
【2019/04/26 18:47】| URL | escort beykoz #4281f690c2 [ 編集 ]


無題
Eu cada vez passou minha meia hora para ler isto webpage do artigos ou clientes o tempo todo junto com um
caneca café.
【2019/06/10 22:13】| URL | Pedro Bryan #756e651246 [ 編集 ]


無題
Ꮋі, evеry time і սsed to check web site posts һere in thee early hourѕ in the dawn, because i like to fіnd out more and mߋre.
【2019/06/16 06:06】| URL | http://dosial.com/bitcoin #20e56f5d8c [ 編集 ]


無題
Ӏ liike looking throսgh a post that wiⅼl mae mеn and women tһink.
Also, many tһanks for allowing mе to comment!
【2019/08/24 16:25】| URL | cardano #86e32967b8 [ 編集 ]


無題
I believe everything said was actually very reasonable.
But, think about this, suppose you were to write a awesome headline?
I mean, I don't want to tell you how to run your website, but what if you added a headline to maybe grab people's
attention? I mean 重複して始まらないBeginAcceptTcpClient その2 @XOR is kinda vanilla.
You might peek at Yahoo's home page and watch how they create article headlines to grab
viewers interested. You might try adding a video or a related pic or two to grab readers excited
about everything've got to say. Just my opinion, it
could bring your posts a little bit more interesting.
【2019/09/07 08:58】| URL | CNC Machining #103d466a04 [ 編集 ]


貴重なご意見の投稿














<<前ページ | ホーム | 次ページ>>