Re: multithreaded tcp/ip monitoring application
- From: Punit Kaur <PunitKaur@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 10 Jul 2007 06:42:01 -0700
Thanks a lot for your suggestion. I have read concepts related to sockets,
Begin receive and End receive. I have two pieces of code and not sure which
is the right way to develop the application. I can give u a brief idea and
may be u could guide me which way to adopt and if my coding mechanism is
right.
In my application I intend to connect to a remote pc and I do that in my
btnclick event as follows--
private void btnConnect_Click(object sender, EventArgs e)
{
//some code validating the ipaddress entered in txtbox
lblConnStatus.Text = "Trying to establish connection....";
// reset events
m_EventStopThread.Reset();
m_EventThreadStopped.Reset();
// create worker thread instance that connects with the Network
Recorder periodically and checks its status
m_WorkerThread = new Thread(new
ThreadStart(this.WorkerThreadFunction));
m_WorkerThread.Start();
btnConnect.Enabled = false;
DateTime CurrTime = DateTime.Now;
lblStatusSince.Text = CurrTime.ToShortDateString()+ " " +
CurrTime.ToShortTimeString();
}
The worker thread calls a Start Client function which continuously tries to
connect to the server in a while loop. Like as in the following code:
public void WorkerThreadFunction()
{
IPAddress ip = IPAddress.Parse(tbxIPAddress.Text);
while (true)
{
// some stmts
if (client != null)
{
client.Close();
}
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
StartClient(ip, client, this);
Thread.Sleep(1500);
connected = true;
}
StartClient function looks like this
private static void StartClient(IPAddress ip, Socket client, MainForm m_form)
{
// Connect to a remote PC.
try
{
IPEndPoint remoteEP = new IPEndPoint(ip, 8001);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new
AsyncCallback(ConnectCallback), client);
connectDone.WaitOne(1000, false);
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 1000);
Send(client, "This is a test<EOF>");
sendDone.WaitOne(1000, false);
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 1000);
Receive(client);
status = true;
// Remote Application running. Set the Application icon to
green
m_form.Invoke(m_form.m_DelegateSetAppIcon);
}
catch (Exception e)
{
// Remote application not running. Set the Application Icon
to Red
status = false;
m_form.Invoke(m_form.m_DelegateSetAppIcon);
//MessageBox.Show(e.Message);
}
}
In the above code.... The client tried to continuously connect to the remote
server. The previous connect calls are nullified by assigning null to the
client each time the external loop is called. This is one way of making sure
that the remote application is alive.
Another way I thought was to connec to the application just once and call
the Begin Receive function... and and call beginreceive repeatedly. If in
between the server shuts down, beginreceive will have a socketexception which
needs to appropriately handled by just updating the UI to show that the
remote application is down and then retry connecting repeatedly until the
connection is established and then again start receving data.
Can you please tell me which way is better. The first code seems to be
working. But I find the second one less complicated. But I am having trouble
updating the UI from OnReceive function for which I will post my code to take
your suggestions.
Sorry for the very long post.
Please reply asap.
Thank you
.
- Follow-Ups:
- Re: multithreaded tcp/ip monitoring application
- From: Punit Kaur
- Re: multithreaded tcp/ip monitoring application
- References:
- Re: multithreaded tcp/ip monitoring application
- From: Peter Duniho
- Re: multithreaded tcp/ip monitoring application
- Prev by Date: Re: Updating Date field in a mdb file
- Next by Date: Re: execute reader
- Previous by thread: Re: multithreaded tcp/ip monitoring application
- Next by thread: Re: multithreaded tcp/ip monitoring application
- Index(es):
Relevant Pages
|
Loading