Re: multithreaded tcp/ip monitoring application




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


.



Relevant Pages

  • Re: RWW Disconnecting
    ... I understand that remote client encounts following error message when RWW ... I strongly suggest that we rerun the Configure E-mail and Internet ... 825763 How to configure Internet access in Windows Small Business Server ...
    (microsoft.public.windows.server.sbs)
  • Re: Great SWT Program
    ... graphics; one-button mice...all while the machines have tended to be ... the internet are good enough that you can run a modern-as-of-2007 GUI ... * Remote machine has graphical app running that tells the Windows ... network to the client to display a button labeled "foo" at those ...
    (comp.lang.java.programmer)
  • Re: Redirect problems to remote client
    ... sent you a copy of the results file from the client. ... > remote client and redirecting the My Documents folder. ... > - Internet Explorer Maitenance policy processing ... Microsoft is providing this information as a convenience to you. ...
    (microsoft.public.windows.server.sbs)
  • RE: Remote connectivity problems
    ... do you mean you have added a remote client to SBS ... If you have hardware VPN tunnel setup using Linksys or others, ... In this scenario you have to configure the SBS Server computer to enable ...
    (microsoft.public.windows.server.sbs)
  • Re: Mapped Drives over VPN
    ... When the client tries to access a share on the remote machine, ... Microsoft CSS Online Newsgroup Support ... | They have generally two mapped drives. ...
    (microsoft.public.windows.server.sbs)

Loading