How do I shutdown a UdpClient? 
Author Message
 How do I shutdown a UdpClient?

Hi all:

I have a UdpClient in my class.  After it sents some data to the server, it
starts a timer and wait fir response from the server by calling Receive.
Say, if the server does not respond, the timer will expire, how do I Close
the UDP Client?

What I did was that I tried to instantiate theUdpClient in one function, and
then use theUdpClient.Close() in OnTimedEvent, but I got the following
compile error:

An object reference is required for the nonstatic field, method or property
....

Why?

Here is how my code looks like:

------------------------------------------------------------
private UdpClient udpNotifyClient;

public class TcpUdpClient

{

.........................

public bool sendNotify(String serverName)

{

udpNotifyClient = new UdpClient(serverName, UDPNOTIFYPORT);

..................

int nBytesSent = udpNotifyClient.Send(inputToBeSent, inputToBeSent.Length);

this.startUdpTimer();

.........................

receivedNotify = udpNotifyClient.Receive(ref remoteNotifyIpEndPoint);

this.stopUdpTimer();

...................................

Quote:
}

public void startUdpTimer()

{

// Create a new Timer with Interval set to 5 seconds.

udpTimer = new System.Timers.Timer(5000);

udpTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

// Only raise the event the first time Interval elapses.

udpTimer.AutoReset = false;

udpTimer.Enabled = true;

Quote:
}

public void stopUdpTimer()

{

udpTimer.Stop();

Quote:
}

// Specify what you want to happen when the event is raised.

public static void OnTimedEvent(object source, ElapsedEventArgs e)

{

udpNotifyClient.Close();   // The compile error occurs here!

Quote:
}
}

Any help will be very much appreciated.  Thanks a lot in advance,

Patrick.



Sun, 21 Mar 2004 17:50:08 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. delaying shutdown untill service is done handling shutdown message

2. How to shutdown NT service when OS is shutdown

3. Shutdown myself? ie shutdown a COM Server

4. How to shutdown NT service when OS is shutdown

5. UdpClient with a multicast group

6. UdpClient class problem

7. udpclient and syslogd pt2

8. UDPClient and multiple IPs

9. UDPClient hell

10. Bug in Thread.Abort or UdpClient.Receive

11. UdpClient and timeout ?

12. Using udpclient to listen for syslogd

 

 
Powered by phpBB® Forum Software