try
{
//ホスト名からIPアドレスを取得
IPAddress hostadd = Dns.GetHostEntry(inf.comsrvaddr).AddressList[0];
//IPEndPointを取得
IPEndPoint ephost = new IPEndPoint(hostadd, int.Parse(inf.comsrvport));
//Socketの作成
System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
//接続
sock.Connect(ephost);
//コメントリクエストメッセージを送信
string param = String.Format(
"<thread thread=\"{0}\" version=\"20061206\" res_from=\"-1\"/>\0", inf.comsrvthreID);
byte[] data = Encoding.UTF8.GetBytes(param);
sock.Send(data, data.Length, System.Net.Sockets.SocketFlags.None);
pre = "";
const int MAX_RECEIVE_SIZE = 1048576*64;
byte[] resbytes = new byte[MAX_RECEIVE_SIZE];
int ressize = 0;
while (MainForm.flgthrecancel == false)
{
//受信
ressize = sock.Receive(resbytes, resbytes.Length, System.Net.Sockets.SocketFlags.None);
if (ressize == 0) continue;
string xml = pre + Encoding.UTF8.GetString(resbytes, 0, ressize);
/*途切れた場合に、受信に近い所で早く次の受信に繋げようとしたが駄目だったため、
*コメントアウト
if (!xml.EndsWith("/chat>\0") && !xml.EndsWith("/>\0"))
{
pre = xml;
continue;
}
pre = "";
*/
xml = xml.Replace('\0', '\n');
string[] lines = xml.Split('\n');
foreach (string line in lines)
{
if (line != ""&&!line.EndsWith("/chat>"))
{
//MAX_RECEIVE_SIZEいっぱいに受信した場合等
//XMLが閉じていない場合は次回Receive時結合する
pre = line;
break;
}
if (line.StartsWith("<chat "))
{
//コメント取得
//<chat>ここ</chat>を取り出す
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(line);//**************ここでエラーが起きる
XmlElement root = xdoc.DocumentElement;
Come come = new Come();//コメント情報収納用インスタンス
string comeno="" ,comecmd="",comememstatus = "";
foreach (XmlAttribute attrib in root.Attributes)
{
if (attrib.Name == "no")
{
come.No = "No." + attrib.Value;
}
if (attrib.Name == "mail")
{
come.Command= attrib.Value ;
}
if (attrib.Name == "premium")
{
switch (attrib.Value)
{
case "0": comememstatus = "一般"; break;
case "1": comememstatus = "プレ"; break;
case "2": comememstatus = "アラ"; break;
case "3": comememstatus = "主"; break;
case "6": comememstatus = "運営"; break;
case "7": comememstatus = "BSP"; break;
case "8":
case "9": comememstatus = "iPhone"; break;
default: comememstatus = "不明" + attrib.Value; break;
}
come.Status = comememstatus;
inf.deleary(come);//delegateでcomeオブジェクトを渡す
}
}
come.Coment = xdoc.InnerText.ToString();
}
}
cometext = ""; debug = "";
}
info = "コメント取得スレッドを停止しました。";
MessageBox.Show(null,"コメント取得を停止しました","",MessageBoxButtons.OK,MessageBoxIcon.None);
}
catch (Exception e)
{
info += "\r\n投稿失敗!\r\n";
info += "Message :n" + e.Message + "\r\n";
info += "Type :n" + e.GetType().FullName + "\r\n";
info += "StackTrace :n" + e.StackTrace.ToString() + "\r\n";
info += "エラーライン:" + debug;
inf.deleinfo(info);
}