20. コメント取得のサンプルソース(VB.NET)
Dim addr As String = "msgXXX.live.nicovideo.jp" : Dim port As String = "28XX" : Dim thread As String = "XXXXXXXXX"
Dim post_str As String = String.Format _
("<thread thread=""{0}"" version=""20061206"" res_from=""-{1}"" scores=""1"" />", _
thread, "1000") + ControlChars.NullChar 'コメントを要求するXmlを作成
Dim client As New TcpClient() : client.Connect(addr, port) 'コメントサーバへ接続する。
Dim stream As NetworkStream = client.GetStream()
Dim reqData As Byte() = Encoding.UTF8.GetBytes(post_str)
stream.Write(reqData, 0, reqData.Length) '要求を送信
Dim buffer As Byte() = New Byte(8191) {} : Dim readSize As Integer = 0 '受信処理
Dim byteList As New List(Of Byte)()
While True
readSize = stream.Read(buffer, 0, buffer.Length)
For i As Integer = 0 To readSize - 1
If buffer(i) = "¥0" Then '必ず¥0区切りで処理する。
Dim str As String = Encoding.UTF8.GetString(byteList.ToArray())
byteList.Clear()
Dim xmlDoc As New XmlDocument() : xmlDoc.LoadXml(str)
Dim xmlElem As XmlElement = xmlDoc.DocumentElement
Select Case xmlElem.Name
Case "chat"
Console.WriteLine(str) 'コメント表示
End Select
Else
byteList.Add(buffer(i))
End If
Next
End While