How to make a connection checker between a socket server and client?
Question:
I have a socket connection between a python server and a c#
client, so I’m trying to define a boolean variable _status
in the client side on which I store the status of the connection (true or false). I have tried the code below but it does not work; it always returns a false status, what wrong with it? Is there any other good idea to do it better than this?
c# code:
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 code:
while True:
try:
msg = ReceiveQuery(client)
if msg == "Are you here?":
SendQuery("Yes",client)
except Exception as e:
print ('an exception has been generated')
Answers:
Although I don’t know the implementation of your socket connection object sc
I see some problems in your code:
ReceiveStatus()
contains both connecting the socket and receiving data through the socket. You should split this up into connecting and receiving.
- As
ReceiveStatus()
is started in a Task it could be possible that sc.SendQuery("Are you here?");
is called before the socket is connected.
- While
SendQuery()
is called in the infinite loop, ReceiveQuery()
is called only once in the Task. As soon as the task has ended you will never read new information again.
I have a socket connection between a python server and a c#
client, so I’m trying to define a boolean variable _status
in the client side on which I store the status of the connection (true or false). I have tried the code below but it does not work; it always returns a false status, what wrong with it? Is there any other good idea to do it better than this?
c# code:
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 code:
while True:
try:
msg = ReceiveQuery(client)
if msg == "Are you here?":
SendQuery("Yes",client)
except Exception as e:
print ('an exception has been generated')
Although I don’t know the implementation of your socket connection object sc
I see some problems in your code:
ReceiveStatus()
contains both connecting the socket and receiving data through the socket. You should split this up into connecting and receiving.- As
ReceiveStatus()
is started in a Task it could be possible thatsc.SendQuery("Are you here?");
is called before the socket is connected. - While
SendQuery()
is called in the infinite loop,ReceiveQuery()
is called only once in the Task. As soon as the task has ended you will never read new information again.