Hi.
I am trying to make a client and a server. Server must be work always and sometimes I open and close the client program. When client is opened, I want to show a message on server side "client connected". Then if client program closed, there should be a message on server side "client unconnected.". I mean I want to control connected or unconnected always.
My codes are below; if client opened, "connected" message appeared. But when client closed, I couldnt see any message.
How can I control this?
public void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
thread_dinleyici = new Thread(new ThreadStart(dinle));
thread_dinleyici.Start();
}
public void dinle()
{
tcp_listener = new TcpListener(3333);
tcp_listener.Start();
while (true)
{
try
{
istemcisoketi = tcp_listener.AcceptSocket();
if (istemcisoketi.Connected)
{
label1.Text = "connected";
}
else
{
label1.Text = "unconnected";
}
}
catch
{
}
}
}
Loading
Nilanka DharmadasaPosted Jan 11, 2010, 7:17 AM
I hope the code you have posted is server side code. It's fine.
Now you want to know, if the client is disconnected.
For that you have to write some more code.
You can do some thing like this. The client should continuously send a message (we'll call it a pulse) to the server.You can use a thread on client side to do this.
On server side, the server continuosly checks for messages from client. If server doesnot get a message for sometime(Let's say for 1 minute) server disposes the connecton to the client. Disposing can be done by simply setting your socket to null.
Then you can set you lebel's text to Disconnected.
Hope you got my idea.
If my answer helped you, please tick 'Do you like this answer' checkbox.
yokzuPosted Jan 11, 2010, 6:31 AM
yokzuPosted Jan 10, 2010, 8:51 AM
if (istemcisoketi.Connected)
{
label1.Text = "connected";
}
else if (istemcisoketi.Closed)
{
label1.Text = "unconnected";
}
yokzuPosted Jan 10, 2010, 8:35 AM
if (istemcisoketi.Connected)
{
label1.Text = "connected";
}
else
{
label1.Text = "unconnected";
}