Disable control+c close program - c#

I have a method that loops ping requests, and I'd like when I click Ctrl+c, it break the loop and give me statics like the normal cmd, but when I click Ctrl+c, I get
Press Any Key To Continue... <<
and then program closes .
For example : ping google.com -t << that will ping google with endless loop, but I need to break the loop ONLY when I click Ctrl+c
private void _t(string website)
{
Ping pingSender = new Ping();
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 10000;
try
{
PingOptions options = new PingOptions(64, false);
PingReply send0 = pingSender.Send(website, timeout, buffer, options);
Console.WriteLine("\nPinging {0} [{1}] With {2} bytes of data :", website, send0.Address.ToString(), send0.Buffer.Length);
while (1 < 2)
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime / 5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}
}
catch
{
Console.WriteLine("Ping request could not find host {0} ", website + ".Please check the name and try again.");
}
}
But instead of using while (1<2), I want to use something like :
while (ConsoleKeyInfo.Equals("Control + c") not clicked) // sure it is wrong , but that`s what I wanna achieve
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime / 5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}

I might be a little late, but here's a better way, one that works on all platforms:
Console.TreatControlCAsInput = true;

There is a fairly complete tutorial (with code) here. I did a quick test and their example actually worked (imagine that).
Here is their code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;
namespace ConsoleApplication1
{
class ControlChecker
{
#region GLOBAL VARS
private static readonly Mutex mutex = new Mutex(true, Assembly.GetExecutingAssembly().GetName().CodeBase);
private static bool _userRequestExit = false;
private static bool _doIStop = false;
static HandlerRoutine consoleHandler;
#endregion
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
// A delegate type to be used as the handler routine for SetConsoleCtrlHandler.
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
/// <summary>
///
/// </summary>
/// <param name="ctrlType"></param>
/// <returns></returns>
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
// Put your own handler here
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
_userRequestExit = true;
Console.WriteLine("CTRL+C received, shutting down");
break;
case CtrlTypes.CTRL_BREAK_EVENT:
_userRequestExit = true;
Console.WriteLine("CTRL+BREAK received, shutting down");
break;
case CtrlTypes.CTRL_CLOSE_EVENT:
_userRequestExit = true;
Console.WriteLine("Program being closed, shutting down");
break;
case CtrlTypes.CTRL_LOGOFF_EVENT:
case CtrlTypes.CTRL_SHUTDOWN_EVENT:
_userRequestExit = true;
Console.WriteLine("User is logging off!, shutting down");
break;
}
return true;
}
/// <summary>
/// Main entry point
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
static int Main(string[] args)
{
try
{
//make sure we only have one....
if (!mutex.WaitOne(TimeSpan.Zero, true))
{
Console.WriteLine("Another instance already running");
Thread.Sleep(5000);
return 1;
}
//save a reference so it does not get GC'd
consoleHandler = new HandlerRoutine(ConsoleCtrlCheck);
//set our handler here that will trap exit
SetConsoleCtrlHandler(consoleHandler, true);
DoMyTask();
return 0;
}
catch (Exception x)
{
Console.WriteLine("Main Error [{0}]", x.Message);
return -1;
}
}
/// <summary>
/// Run the export
/// </summary>
/// <param name="pAuthority"></param>
/// <returns></returns>
private static void DoMyTask()
{
//execcute until we have no more records to process
while (!_doIStop)
{
//did user request exit?
if (_userRequestExit)
{
_doIStop = true; //set flag to exit loop. Other conditions could cause this too, which is why we use a seperate variable
Console.WriteLine("Shutting down, user requested exit");
break;
}
//do some other stuff here
Console.WriteLine(String.Format("{0}, no exit requested yet...", DateTime.Now));
//sleep 1 second
Thread.Sleep(1000);
}
}
}
}

Another way is to handle the Console.CancelKeyPress event:
Console.CancelKeyPress += Console_CancelKeyPress
This allows you to keep the shortcut, but decide whether to exit on a case by case basis.
private static void HandleConsole_CancelKeyPress(ConsoleCancelEventArgs consoleCancelEventArgs)
{
if (doNotExitYet) {
consoleCancelEventArgs.Cancel = true;
}
}

Related

C# Need Help Returning Lowest Ping using SendAsync

I have the following code that is working and pings all of the Runescape game servers and returns a list in the console of the ip addresses and the respective 'roundtrip' time taken.
EDIT 'IGNORE SOME OF THE OLD COMMENTS'
I am having trouble with the following:
A) How can I return the lowest ping out of all of the servers and write it to the console?
B) How can i return the original hostname or 'number' of the current 'server.ToString()' across methods?
public void buttonClick_Click(object sender, EventArgs e)
{
Console.WriteLine();
Ping();
}
public static void Ping()
{
for (int server = 1; server <= 110; server++)
{
string hostname = "oldschool" + server.ToString() + ".runescape.com";
// Get an object that will block the main thread.
AutoResetEvent waiter = new AutoResetEvent(false);
// Ping's the local machine.
Ping pingSender = new Ping();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//Console.WriteLine("Send Before Async.");
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(hostname, 1000, waiter);
//Console.WriteLine("Ping example completed.");
}
}
private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine("Ping failed:");
Console.WriteLine(e.Error.ToString());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
DisplayReply(reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
public static void DisplayReply(PingReply reply)
{
List<long> lag = new List<long>();
if (reply == null)
return;
Console.WriteLine("Status: {0}", reply.Status);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("\r\n");
Console.WriteLine("Address: {0}", reply.Address);
Console.WriteLine("Ping: {0}", reply.RoundtripTime);
}
return;
}
I restructured your code so it should work. I also made pings run in parallel which should speed up results.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
public class State
{
public string ip { get; set;}
public decimal roundTripTime { get; set; }
public IPStatus status { get;set;}
}
public class myPing
{
static const int NUMBER_SERVERS = 110;
public static List<State> states { get; set; }
public static int count = 0;
public myPing()
{
AutoResetEvent waiter = new AutoResetEvent(false);
for (int server = 1; server <= NUMBER_SERVERS; server++)
{
string hostname = "oldschool" + server.ToString() + ".runescape.com";
// Get an object that will block the main thread.
// Ping's the local machine.
Ping pingSender = new Ping();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//Console.WriteLine("Send Before Async.");
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(hostname, 1000, waiter);
//Console.WriteLine("Ping example completed.");
}
waiter.WaitOne();
DisplayReply(states);
Console.ReadLine();
}
private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine("Ping failed:");
Console.WriteLine(e.Error.ToString());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
State state = new State();
Object thisLock = new Object();
lock (thisLock)
{
states.Add(state);
}
state.ip = reply.Address.ToString();
state.roundTripTime = reply.RoundtripTime;
state.status = reply.Status;
count++;
// Let the main thread resume.
if (count == NUMBER_SERVERS)
{
((AutoResetEvent)e.UserState).Set();
}
}
public static void DisplayReply(List<State> replies)
{
foreach (State reply in replies)
{
Console.WriteLine("Status: {0}", reply.status);
if (reply.status == IPStatus.Success)
{
Console.WriteLine("\r\n");
Console.WriteLine("Address: {0}", reply.ip);
Console.WriteLine("Ping: {0}", reply.roundTripTime);
}
}
State shortest = replies.OrderBy(x => x.roundTripTime).FirstOrDefault();
Console.WriteLine("Shortest Route IP : '{0}' Time : '{1}'", shortest.ip.ToString(), shortest.roundTripTime.ToString() );
}
}
}

How can I have a progress bar load with the completion of a process?

I am using the following to ping a host name:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = pingData;
p.Start();
p.WaitForExit();
string result = p.StandardOutput.ReadToEnd();
System.Windows.Forms.Clipboard.SetText(result);
if(p.HasExited)
{
richTextBox1.Text = result;
outPut = result;
MessageBox.Show( "Ping request has completed. \n Results have been copied to the clipboard.");
}
Is there anyway I can have a progress bar "load" during the ping command and complete the loading when the ping has completed?
Thank you.
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingcompletedeventhandler(v=vs.110).aspx even has an example of how to use Ping. Depending on how you want to display the progress bar, you could modify the example to have the event handler set a flag indicating that the ping has completed. Then add a while loop in the main execution thread that fills in the progress bar as a percent of the elapsed time / timeout value. Below is the example reformatted as an example
namespace PingTest
{
using global::System;
using System.Diagnostics;
using global::System.Net.NetworkInformation;
using System.Text;
using System.Threading;
/// <summary>
/// The ping example.
/// </summary>
public class PingExample
{
#region Static Fields
private static bool pingComplete;
#endregion
#region Public Methods and Operators
public static void DisplayReply(PingReply reply)
{
if (reply == null)
{
return;
}
Console.WriteLine("ping status: {0}", reply.Status);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address);
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
}
}
public static void Main(string[] args)
{
if (args.Length == 0)
{
throw new ArgumentException("Ping needs a host or IP Address.");
}
string who = args[0];
var waiter = new AutoResetEvent(false);
var pingSender = new Ping();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += PingCompletedCallback;
// Create a buffer of 32 bytes of data to be transmitted.
const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(Data);
// Wait 12 seconds for a reply.
const int Timeout = 12000;
// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
var options = new PingOptions(64, true);
Console.WriteLine("Time to live: {0}", options.Ttl);
Console.WriteLine("Don't fragment: {0}", options.DontFragment);
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingComplete = false;
var watch = Stopwatch.StartNew();
watch.Start();
pingSender.SendAsync(who, Timeout, buffer, options, waiter);
while (!pingComplete && watch.ElapsedMilliseconds <= Timeout)
{
// Do display stuff for your progress bar here.
}
// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne();
Console.WriteLine("Ping example completed.");
Console.ReadLine();
}
#endregion
#region Methods
private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine("Ping failed:");
Console.WriteLine(e.Error.ToString());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
pingComplete = true;
DisplayReply(reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
#endregion
}
}

How to (quickly) check if UNC Path is available

How can I check if a UNC Path is available?
I have the problem that the check takes about half a minute if the share is not available :
var fi = new DirectoryInfo(#"\\hostname\samba-sharename\directory");
if (fi.Exists)
//...
Is there a faster way to check if a folder is available?
I'm using Windows XP and C#.
How's this for a quick and dirty way to check - run the windows net use command and parse the output for the line with the network path of interest (e.g. \\vault2) and OK. Here's an example of the output:
C:\>net use
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK O: \\smarty\Data Microsoft Windows Network
Disconnected P: \\dummy\Data Microsoft Windows Network
OK \\vault2\vault2 Microsoft Windows Network
The command completed successfully.
It's not a very .netish solution, but it's very fast, and sometimes that matters more :-).
And here's the code to do it (and LINQPad tells me that it only takes 150ms, so that's nice)
void Main()
{
bool available = QuickBestGuessAboutAccessibilityOfNetworkPath(#"\\vault2\vault2\dir1\dir2");
Console.WriteLine(available);
}
public static bool QuickBestGuessAboutAccessibilityOfNetworkPath(string path)
{
if (string.IsNullOrEmpty(path)) return false;
string pathRoot = Path.GetPathRoot(path);
if (string.IsNullOrEmpty(pathRoot)) return false;
ProcessStartInfo pinfo = new ProcessStartInfo("net", "use");
pinfo.CreateNoWindow = true;
pinfo.RedirectStandardOutput = true;
pinfo.UseShellExecute = false;
string output;
using (Process p = Process.Start(pinfo)) {
output = p.StandardOutput.ReadToEnd();
}
foreach (string line in output.Split('\n'))
{
if (line.Contains(pathRoot) && line.Contains("OK"))
{
return true; // shareIsProbablyConnected
}
}
return false;
}
Or you could probably go the route of using WMI, as alluded to in this answer to How to ensure network drives are connected for an application?
In my project I use the System.IO :
if (Directory.Exists(#"\\hostname\samba-sharename\directory")) { ...
and it's pretty fast so far...
In a project of mine, i had to check whether a server connection was established or not. I used a TCP Socket to asynchronically check whether the server could be reached or not. I wonder if you could use this to check on a network share. The async TCP Socket connect goes so fast i tested for the connection 10 times under 60 miliseconds. Maybe you could play around with that a bit ?
EDIT: Here is the Asynchronous Socket i used for my project. Use this class to check for a certain IP or address. Hope it is of any use to you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace Base.BaseObjects
{
public class AsynchronousClient
{
#region Properties
private int _port = 0000, currentTry = 0, _buffersize, _fastpingdelay = 80;
private string _server = "localhost";
private Socket client;
private static IPEndPoint remoteEP;
// Delegates & Events
public delegate void SendMessageDelegate(string message);
public event SendMessageDelegate SendMessageEvent;
public delegate void ConnectionStatusDelegate(bool connected, bool reconnect);
public event ConnectionStatusDelegate ConnectionStatusChanged;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent receiveDone = new ManualResetEvent(false);
/// <summary>
/// Port to monitor
/// </summary>
public int Port { get { return _port; } }
/// <summary>
/// Number of packages to buffer until system reports connection loss
/// </summary>
public int BufferSize { get { return _buffersize; } }
/// <summary>
/// Time in milliseconds between two pings
/// </summary>
public int FastPingDelay { get { return _fastpingdelay; } }
/// <summary>
/// Servername to connect to
/// </summary>
public string Server
{
get { return _server; }
set
{
_server = value;
// Resolve the remote endpoint for the socket.
try
{
IPAddress ipAddress = (IPAddress)Dns.GetHostAddresses(value)[0];
remoteEP = new IPEndPoint(ipAddress, Port);
}
catch (SocketException ex)
{
SendMessage(ex.Message);
}
}
}
#endregion
#region Events & Delegates
protected void SendMessage(string message)
{
if (SendMessageEvent != null)
SendMessageEvent(message);
}
protected void UpdateConnectionStatus(bool connected, bool reconnect)
{
if (ConnectionStatusChanged != null)
ConnectionStatusChanged(connected, reconnect);
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
SendMessage(String.Format("Socket connected to {0}", client.RemoteEndPoint.ToString()));
//UpdateConnectionStatus(true, false);
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
SendMessage(e.ToString());
UpdateConnectionStatus(false, true);
}
}
#endregion
#region methods
public AsynchronousClient(int port, string server)
{
_port = port;
Server = server;
_buffersize = 10;
_fastpingdelay = 20;
}
public void CreateSocket()
{
try
{
StopClient();
}
catch (Exception ex)
{
SendMessage(ex.Message);
}
finally
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
}
public bool FastPingSocket()
{
for (currentTry = 0; currentTry <= BufferSize; currentTry++)
{
try
{
CreateSocket();
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
System.Threading.Thread.Sleep(FastPingDelay);
client.Shutdown(SocketShutdown.Receive);
connectDone.WaitOne();
client.Close();
return true;
}
catch (SocketException ex)
{
SendMessage(ex.Message);
}
catch (ObjectDisposedException ex)
{
currentTry--;
SendMessage(ex.Message);
CreateSocket();
}
catch (NullReferenceException ex)
{
currentTry--;
SendMessage(ex.Message);
CreateSocket();
}
catch (ArgumentNullException ex)
{
SendMessage(ex.Message);
CreateSocket();
}
catch (InvalidOperationException ex)
{
SendMessage(ex.Message);
CreateSocket();
currentTry--;
}
finally
{
StopClient();
}
}
UpdateConnectionStatus(false, true);
return false;
}
public void StopClient()
{
// Release the socket.
try
{
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception) { }
finally
{
UpdateConnectionStatus(false, false);
}
}
#endregion
}
}
Edit: Please don't just copy/paste it. Try to understand the code so you can use it for your benefit, and finetune it for your needs.
I ended up "cheating" and just pinging the host, which is reasonable as that in reality is the case that I'm checking for.
private bool HostExists(string PCName)
{
Ping pinger = new Ping();
try
{
PingReply reply = pinger.Send(PCName);
return reply.Status == IPStatus.Success;
}
catch
{
return false;
}
finally
{
pinger.Dispose();
}
}
This way suits me best because of its speed, simplicity, and reliability.
I used the ping method suggested above and it did not work for me since I am using OpenDNS
Here is a function that worked well for me:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/// <summary>
/// A quick method to test is the path exists
/// </summary>
/// <param name="s"></param>
/// <param name="timeOutMs"></param>
/// <returns></returns>
public static bool CheckPathExists(string s, int timeOutMs = 120) {
if (s.StartsWith(#"\\")) {
Uri uri = new Uri(s);
if (uri.Segments.Length == 0 || string.IsNullOrWhiteSpace(uri.Host))
return false;
if (uri.Host != Dns.GetHostName()) {
WebRequest request;
WebResponse response;
request = WebRequest.Create(uri);
request.Method = "HEAD";
request.Timeout = timeOutMs;
try {
response = request.GetResponse();
} catch (Exception ex) {
return false;
}
return response.ContentLength > 0;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Do a Ping to see if the server is there
// This method doesn't work well using OPenDNS since it always succeeds
// regardless if the IP is a valid or not
// OpenDns always maps every host to an IP. If the host is not valid the
// OpenDNS will map it to 67.215.65.132
/* Example:
C:\>ping xxx
Pinging xxx.RT-AC66R [67.215.65.132] with 32 bytes of data:
Reply from 67.215.65.132: bytes=32 time=24ms TTL=55
*/
//Ping pingSender = new Ping();
//PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
//options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
//string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
//byte[] buffer = Encoding.ASCII.GetBytes(data);
//int timeout = 120;
//PingReply reply = pingSender.Send(uri.Host, timeout, buffer, options);
//if (reply == null || reply.Status != IPStatus.Success)
// return false;
}
}
return File.Exists(s);
}
Thats probably the quickest way, the delay will be the general network speed/disk access etc.
If this is causing a delay for the user, you could try checking this asynchronously?
Maybe you should try to create the folder, if it does exist, it will return an error that you could catch. There should be no default timeout.

NullReferenceException when Firing an Event [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
The problem is: I get a NullReferenceException but I can't seem to find out why. (I just start with C#) and have read the C# for kids from Microsoft. It explained a lot but this I do not understand.
But I don't get the piece of code which throw the exception to my head.
Code is:
using System;
using System.Collections.Generic;
using System.Linq; using System.Text;
using System.IO.Ports;
using PlugwiseLib.BLL.BC;
using plugwiseLib.BLL.BPC;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using PlugwiseLib.BLL.BPC;
using PlugwiseLib.UTIL;
namespace PlugwiseLib {
public class plugwiseControl
{
private SerialPort port;
private PlugwiseActions currentAction;
public delegate void PlugwiseDataReceivedEvent(object
sender, System.EventArgs e,
List<PlugwiseMessage> data);
public event PlugwiseDataReceivedEvent
DataReceived;
private PlugwiseReader reader;
/// <summary>
/// Constructor for the Plugwise Control class
/// </summary>
/// <param name="serialPort">The serial port name that the plugwise stick takes</param>
public plugwiseControl(string serialPort)
{
try
{
port = new SerialPort(serialPort);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.BaudRate = 115200;
currentAction = PlugwiseActions.None;
reader = new PlugwiseReader();
}
catch (Exception e)
{
throw new Exception("Could not connect to plug.");
}
}
/// <summary>
/// This is the method that sends a command to the plugwise plugs.
/// </summary>
/// <param name="mac">The mac adress of the plug that needs to perform the action</param>
/// <param name="action">The action that has to be performed</param>
public void Action(string mac,PlugwiseActions action)
{
try
{
string message = "";
switch (action)
{
case PlugwiseActions.On:
currentAction = PlugwiseActions.On;
message = PlugwiseSerialPortFactory.Create(PlugwiseSerialPortRequest.on,mac);
break;
case PlugwiseActions.Off:
currentAction = PlugwiseActions.Off;
message = PlugwiseSerialPortFactory.Create(PlugwiseSerialPortRequest.off, mac);
break;
case PlugwiseActions.Status:
currentAction = PlugwiseActions.Status;
message = PlugwiseSerialPortFactory.Create(PlugwiseSerialPortRequest.status, mac);
break;
case PlugwiseActions.Calibration:
currentAction = PlugwiseActions.Calibration;
message = PlugwiseSerialPortFactory.Create(PlugwiseSerialPortRequest.calibration, mac);
break;
case PlugwiseActions.powerinfo:
currentAction = PlugwiseActions.powerinfo;
message = PlugwiseSerialPortFactory.Create(PlugwiseSerialPortRequest.powerinfo,mac);
break;
case PlugwiseActions.history:
message = "";
break;
}
if (message.Length > 0)
{
port.WriteLine(message);
Thread.Sleep(10);
}
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// This is the method that sends a command to the plugwise plugs that retrieves the history power information
/// </summary>
/// <param name="mac">The mac adress of the plug that needs to perform the action</param>
/// <param name="logId">The id of the history message that has to be retrieved</param>
/// <param name="action">The action that has to be performed this MUST be history</param>
public void Action(string mac,int logId,PlugwiseActions action)
{
string message = "";
switch(action)
{
case PlugwiseActions.history:
currentAction = PlugwiseActions.history;
message = PlugwiseSerialPortFactory.Create(PlugwiseSerialPortRequest.history, MessageHelper.ConvertIntToPlugwiseLogHex(logId), mac);
break;
}
if (message.Length > 0)
{
port.WriteLine(message);
Thread.Sleep(10);
}
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
string txt = port.ReadExisting();
List<PlugwiseMessage> msg = reader.Read(Regex.Split(txt, "\r\n"));
DataReceived(sender, new System.EventArgs(), msg);
}
/// <summary>
/// This method Opens the connection to the serial port
/// </summary>
public void Open()
{
try
{
if (!port.IsOpen)
{
port.Open();
}
}
catch (System.IO.IOException ex)
{
throw ex;
}
}
/// <summary>
/// This method closes the connection to the serial port
/// </summary>
public void Close()
{
try
{
if (port.IsOpen)
{
port.Close();
}
Thread.Sleep(5);
}
catch (IOException ex)
{
throw ex;
}
}
}
}
I get the exception at this piece (in Visual C# 2008 express)
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
string txt = port.ReadExisting();
List<PlugwiseMessage> msg = reader.Read(Regex.Split(txt, "\r\n"));
DataReceived(sender, new System.EventArgs(), msg);
I checked 'msg' but this is filled with so far i can see valid data.
So I don't know why I get the exception.
Do you have any subscribers for plugwiseControl.DataReceived event? A common way to raise event is
var handler = DataReceived;
if(handler != null) handler(sender, EventArgs.Empty, msg);
I think DataReceived event is not initialized, by the calling method.
In .NET, if you raise an event and there are no registered listeners for that event (nothing has done to your event what you do to your port's DataReceived event in the second line of your try {} block in your constructor), it shows up as null, and raises that exception. In your Watch list screenshot, DataReceived is null, which makes me think this is the problem. So:
if (DataReceived != null)
DataReceived(...arguments here...);

How to configure socket connect timeout

When the Client tries to connect to a disconnected IP address, there is a long timeout over 15 seconds... How can we reduce this timeout? What is the method to configure it?
The code I'm using to set up a socket connection is as following:
try
{
m_clientSocket = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(serverIp);
int iPortNo = System.Convert.ToInt16(serverPort);
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
m_clientSocket.Connect(ipEnd);
if (m_clientSocket.Connected)
{
lb_connectStatus.Text = "Connection Established";
WaitForServerData();
}
}
catch (SocketException se)
{
lb_connectStatus.Text = "Connection Failed";
MessageBox.Show(se.Message);
}
I found this. Simpler than the accepted answer, and works with .NET Framework v2
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect using a timeout (5 seconds)
IAsyncResult result = socket.BeginConnect( sIP, iPort, null, null );
bool success = result.AsyncWaitHandle.WaitOne( 5000, true );
if ( socket.Connected )
{
socket.EndConnect( result );
}
else
{
// NOTE, MUST CLOSE THE SOCKET
socket.Close();
throw new ApplicationException("Failed to connect server.");
}
//...
My take:
public static class SocketExtensions
{
/// <summary>
/// Connects the specified socket.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="endpoint">The IP endpoint.</param>
/// <param name="timeout">The timeout.</param>
public static void Connect(this Socket socket, EndPoint endpoint, TimeSpan timeout)
{
var result = socket.BeginConnect(endpoint, null, null);
bool success = result.AsyncWaitHandle.WaitOne(timeout, true);
if (success)
{
socket.EndConnect(result);
}
else
{
socket.Close();
throw new SocketException(10060); // Connection timed out.
}
}
}
I just wrote an extension class in order to allow timeouts in connections. Use it exactly as you would use the standard Connect() methods, with an extra parameter named timeout.
using System;
using System.Net;
using System.Net.Sockets;
/// <summary>
/// Extensions to Socket class
/// </summary>
public static class SocketExtensions
{
/// <summary>
/// Connects the specified socket.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="host">The host.</param>
/// <param name="port">The port.</param>
/// <param name="timeout">The timeout.</param>
public static void Connect(this Socket socket, string host, int port, TimeSpan timeout)
{
AsyncConnect(socket, (s, a, o) => s.BeginConnect(host, port, a, o), timeout);
}
/// <summary>
/// Connects the specified socket.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="addresses">The addresses.</param>
/// <param name="port">The port.</param>
/// <param name="timeout">The timeout.</param>
public static void Connect(this Socket socket, IPAddress[] addresses, int port, TimeSpan timeout)
{
AsyncConnect(socket, (s, a, o) => s.BeginConnect(addresses, port, a, o), timeout);
}
/// <summary>
/// Asyncs the connect.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="connect">The connect.</param>
/// <param name="timeout">The timeout.</param>
private static void AsyncConnect(Socket socket, Func<Socket, AsyncCallback, object, IAsyncResult> connect, TimeSpan timeout)
{
var asyncResult = connect(socket, null, null);
if (!asyncResult.AsyncWaitHandle.WaitOne(timeout))
{
try
{
socket.EndConnect(asyncResult);
}
catch (SocketException)
{ }
catch (ObjectDisposedException)
{ }
}
}
it might be too late but there is neat solution based on Task.WaitAny (c# 5 +) :
public static bool ConnectWithTimeout(this Socket socket, string host, int port, int timeout)
{
bool connected = false;
Task result = socket.ConnectAsync(host, port);
int index = Task.WaitAny(new[] { result }, timeout);
connected = socket.Connected;
if (!connected) {
socket.Close();
}
return connected;
}
I dont program in C# but in C, we solve the same problem by making the socket non-blocking and then putting the fd in a select/poll loop with a timeout value equal to the amount of time we are willing to wait for the connect to succeed.
I found this for Visual C++ and the explanation there also bends towards the select/poll mechanism I explained before.
In my experience, you cannot change connect timeout values per socket. You change it for all (by tuning OS parameters).
I solved the problem by using Socket.ConnectAsync Method instead of Socket.Connect Method.
After invoking the Socket.ConnectAsync(SocketAsyncEventArgs), start a timer (timer_connection), if time is up, check whether socket connection is connected (if(m_clientSocket.Connected)), if not, pop up timeout error.
private void connect(string ipAdd,string port)
{
try
{
SocketAsyncEventArgs e=new SocketAsyncEventArgs();
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(serverIp);
int iPortNo = System.Convert.ToInt16(serverPort);
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
//m_clientSocket.
e.RemoteEndPoint = ipEnd;
e.UserToken = m_clientSocket;
e.Completed+=new EventHandler<SocketAsyncEventArgs>(e_Completed);
m_clientSocket.ConnectAsync(e);
if (timer_connection != null)
{
timer_connection.Dispose();
}
else
{
timer_connection = new Timer();
}
timer_connection.Interval = 2000;
timer_connection.Tick+=new EventHandler(timer_connection_Tick);
timer_connection.Start();
}
catch (SocketException se)
{
lb_connectStatus.Text = "Connection Failed";
MessageBox.Show(se.Message);
}
}
private void e_Completed(object sender,SocketAsyncEventArgs e)
{
lb_connectStatus.Text = "Connection Established";
WaitForServerData();
}
private void timer_connection_Tick(object sender, EventArgs e)
{
if (!m_clientSocket.Connected)
{
MessageBox.Show("Connection Timeout");
//m_clientSocket = null;
timer_connection.Stop();
}
}
Check this out on MSDN. It does not appear that you can do this with the implemented properties in the Socket class.
The poster on MSDN actually solved his problem using threading. He had a main thread which called out to other threads which run the connection code for a couple seconds and then check the Connected property of the socket:
I created another method wich actually
connected the socket ... had the main
thread sleep for 2 seconds and then
check on the connecting method (wich
is run in a separate thread) if the
socket was connected good otherwise
throw an exception "Timed out " and
that;s all. Thanks again for the
repleies.
What are you trying to do, and why can't it wait for 15-30 seconds before timing out?
I had the Same problem when connecting to a Socket and I came up with the below solution ,It works Fine for me.
`
private bool CheckConnectivityForProxyHost(string hostName, int port)
{
if (string.IsNullOrEmpty(hostName))
return false;
bool isUp = false;
Socket testSocket = null;
try
{
testSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = null;
if (testSocket != null && NetworkingCollaboratorBase.GetResolvedConnecionIPAddress(hostName, out ip))//Use a method to resolve your IP
{
IPEndPoint ipEndPoint = new IPEndPoint(ip, port);
isUp = false;
//time out 5 Sec
CallWithTimeout(ConnectToProxyServers, 5000, testSocket, ipEndPoint);
if (testSocket != null && testSocket.Connected)
{
isUp = true;
}
}
}
}
catch (Exception ex)
{
isUp = false;
}
finally
{
try
{
if (testSocket != null)
{
testSocket.Shutdown(SocketShutdown.Both);
}
}
catch (Exception ex)
{
}
finally
{
if (testSocket != null)
testSocket.Close();
}
}
return isUp;
}
private void CallWithTimeout(Action<Socket, IPEndPoint> action, int timeoutMilliseconds, Socket socket, IPEndPoint ipendPoint)
{
try
{
Action wrappedAction = () =>
{
action(socket, ipendPoint);
};
IAsyncResult result = wrappedAction.BeginInvoke(null, null);
if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
{
wrappedAction.EndInvoke(result);
}
}
catch (Exception ex)
{
}
}
private void ConnectToProxyServers(Socket testSocket, IPEndPoint ipEndPoint)
{
try
{
if (testSocket == null || ipEndPoint == null)
return;
testSocket.Connect(ipEndPoint);
}
catch (Exception ex)
{
}
}
I worked with Unity and had some problem with the BeginConnect and other async methods from socket.
There is something than I don't understand but the code samples before doesn't work for me.
So I wrote this piece of code to make it work. I test it on an adhoc network with android and pc, also in local on my computer. Hope it can help.
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System;
using System.Diagnostics;
class ConnexionParameter : Guardian
{
public TcpClient client;
public string address;
public int port;
public Thread principale;
public Thread thisthread = null;
public int timeout;
private EventWaitHandle wh = new AutoResetEvent(false);
public ConnexionParameter(TcpClient client, string address, int port, int timeout, Thread principale)
{
this.client = client;
this.address = address;
this.port = port;
this.principale = principale;
this.timeout = timeout;
thisthread = new Thread(Connect);
}
public void Connect()
{
WatchDog.Start(timeout, this);
try
{
client.Connect(IPAddress.Parse(address), port);
}
catch (Exception)
{
UnityEngine.Debug.LogWarning("Unable to connect service (Training mode? Or not running?)");
}
OnTimeOver();
//principale.Resume();
}
public bool IsConnected = true;
public void OnTimeOver()
{
try
{
if (!client.Connected)
{
/*there is the trick. The abort method from thread doesn't
make the connection stop immediately(I think it's because it rise an exception
that make time to stop). Instead I close the socket while it's trying to
connect , that make the connection method return faster*/
IsConnected = false;
client.Close();
}
wh.Set();
}
catch(Exception)
{
UnityEngine.Debug.LogWarning("Connexion already closed, or forcing connexion thread to end. Ignore.");
}
}
public void Start()
{
thisthread.Start();
wh.WaitOne();
//principale.Suspend();
}
public bool Get()
{
Start();
return IsConnected;
}
}
public static class Connexion
{
public static bool Connect(this TcpClient client, string address, int port, int timeout)
{
ConnexionParameter cp = new ConnexionParameter(client, address, port, timeout, Thread.CurrentThread);
return cp.Get();
}
//http://stackoverflow.com/questions/19653588/timeout-at-acceptsocket
public static Socket AcceptSocket(this TcpListener tcpListener, int timeoutms, int pollInterval = 10)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutms);
var stopWatch = new Stopwatch();
stopWatch.Start();
while (stopWatch.Elapsed < timeout)
{
if (tcpListener.Pending())
return tcpListener.AcceptSocket();
Thread.Sleep(pollInterval);
}
return null;
}
}
and there a very simple watchdog on C# to make it work:
using System.Threading;
public interface Guardian
{
void OnTimeOver();
}
public class WatchDog {
int m_iMs;
Guardian m_guardian;
public WatchDog(int a_iMs, Guardian a_guardian)
{
m_iMs = a_iMs;
m_guardian = a_guardian;
Thread thread = new Thread(body);
thread.Start(this);
}
private void body(object o)
{
WatchDog watchdog = (WatchDog)o;
Thread.Sleep(watchdog.m_iMs);
watchdog.m_guardian.OnTimeOver();
}
public static void Start(int a_iMs, Guardian a_guardian)
{
new WatchDog(a_iMs, a_guardian);
}
}
This is like FlappySock's answer, but I added a callback to it because I didn't like the layout and how the Boolean was getting returned. In the comments of that answer from Nick Miller:
In my experience, if the end point can be reached, but there is no server on the endpoint able to receive the connection, then AsyncWaitHandle.WaitOne will be signaled, but the socket will remain unconnected
So to me, it seems relying on what is returned can be dangerous - I prefer to use socket.Connected. I set a nullable Boolean and update it in the callback function. I also found it doesn't always finish reporting the result before returning to the main function - I handle for that, too, and make it wait for the result using the timeout:
private static bool? areWeConnected = null;
private static bool checkSocket(string svrAddress, int port)
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(svrAddress), port);
Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
int timeout = 5000; // int.Parse(ConfigurationManager.AppSettings["socketTimeout"].ToString());
int ctr = 0;
IAsyncResult ar = socket.BeginConnect(endPoint, Connect_Callback, socket);
ar.AsyncWaitHandle.WaitOne( timeout, true );
// Sometimes it returns here as null before it's done checking the connection
// No idea why, since .WaitOne() should block that, but it does happen
while (areWeConnected == null && ctr < timeout)
{
Thread.Sleep(100);
ctr += 100;
} // Given 100ms between checks, it allows 50 checks
// for a 5 second timeout before we give up and return false, below
if (areWeConnected == true)
{
return true;
}
else
{
return false;
}
}
private static void Connect_Callback(IAsyncResult ar)
{
areWeConnected = null;
try
{
Socket socket = (Socket)ar.AsyncState;
areWeConnected = socket.Connected;
socket.EndConnect(ar);
}
catch (Exception ex)
{
areWeConnected = false;
// log exception
}
}
Related: How to check if I'm connected?
I have tried with dotnet 6, and after accepting a connection dotnet allows you to set the timeout.
Socket clientSocket = listener.Accept();
clientSocket.ReceiveTimeout = 1000;
and the you have to manage with Receive
try
{
int numByte = clientSocket.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, numByte);
break;
}
catch (System.Net.Sockets.SocketException e)
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
goto NewSocket;
}
Source: Microsoft
There should be a ReceiveTimeout property in the Socket class.
Socket.ReceiveTimeout Property

Categories

Resources