0
私はPythonサーバーとc#
クライアントとの間のソケット接続を持っているので、接続状態(真または偽)を格納するクライアント側にブール変数_status
を定義しようとしています。私は以下のコードを試しましたが、うまくいきません。それは常に偽の状態を返す、何が間違っている?これよりも良いことをするための他の良いアイデアはありますか?ソケットサーバーとクライアントの間に接続チェッカーを作成するにはどうすればいいですか?
C#コード:
public string ReceiveStatus()
{
sc.SetAddress("127.0.0.1");
sc.SetPort("20015");
sc.ServerConnect();
return sc.ReceiveQuery();
}
new Thread(() =>
{
var task = Task.Run(() => ReceiveStatus());
Thread.CurrentThread.IsBackground = true;
while(true)
{
sc.SendQuery("Are you here?");
if (task.Wait(TimeSpan.FromSeconds(1)))
{
if (task.Result == "Yes")
{
_status = true;
}
else
{
_status = false;
}
}
else
{
_status = false;
}
}
}).Start();
のpythonコード:
while True:
try:
msg = ReceiveQuery(client)
if msg == "Are you here?":
SendQuery("Yes",client)
except Exception as e:
print ('an exception has been generated')
アイデアは良いです。 pingプロトコル(エコー要求/エコー応答)は、接続を確認する方法です。 'task.Wait'の後にPythonから返される' task.Result'の値は何ですか? – phd
'task.Result'の値は'まだ計算されていません ' –
'ReceiveStatus()'のコードも提供できますか? –