static void tcp()
{
MessageBox.Show("Beginning...");
System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 20061);
TcpClient connection = new TcpClient();
connection.Connect(ipEndPoint);
NetworkStream stream = connection.GetStream();
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.WriteLine("/api/subscribe?source=console&key=d41c411558628535bbad927b1ad667c823e37a7e06e1b0a61ce707ed287bb4bb&show_previous=true");
writer.Flush();
var line = reader.ReadLine();
if (line != "success")
throw new InvalidOperationException("Failed to connect");
while ((line = reader.ReadLine()) != null)
{
MessageBox.Show(line);
Program.Form3 Form3 = new Program.Form3();
Form3.textBox1.Text = Form3.textBox1.Text + "\r\n" + line;
}
}
これは私が得たコードです。TCP 経由でリクエストを待機しているサーバーに接続したいと考えています。ただし、このコードは機能せず、その理由がわかりません。メインスレッドにあるとき、プログラムはフリーズしていました。現在、別のスレッド (tcp()) にある場合、実際には何も起こらず、サーバーは何も受信しません。
サーバーが完全に機能し、動作しているかどうかを確認しました。100% 機能しています。SimpleTCP を使用して確認しました。(ポート 20061 経由で 127.0.0.1 に接続し、コマンド「/api/subscribe?source=console&key=d41c411558628535bbad927b1ad667c823e37a7e06e1b0a61ce707ed287bb4bb&show_previous=true」を送信し、必要な文字列を受信し始めました。)
TCP に接続し、1 つのコマンドを送信して、文字列の受信を開始したいだけです。
ああ、確かにスレッドを呼び出します。「接続」ボタンを押すと、メッセージボックスが表示されます。