Find out if server is running, asp.net - c#

I have a server and a website running 100% fine.
The thing is, I want my website to show on the default page "Status" of the server. I was thinking about pinging the server with this code:
Uri url = new Uri("http://www.abhigrgrgrgrgsheksur.com");
string pingurl = string.Format("{0}", url.Host);
string host = pingurl;
bool result = false;
Ping p = new Ping();
try
{
PingReply reply = p.Send(host, 3000);
if (reply.Status == IPStatus.Success)
return true;
}
catch { }
return result;
The code is from a website I found online, not mine.
The thing is, if you put that code on the default page on the website, when people do "Reverse" its always pings the server, my question is, can it drop the server? Or get it stop working? Or anything like that? The server is not a website. It's a running process (MineCraft Server).

If I understood correctly your worried about on every refresh of page it will ping and multiple ping call will be made
So the statement made in Remarks paragraph for Ping Class at http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx might help your concern
You cannot use the same instance of the Ping class to generate multiple simultaneous ICMP Echo requests. Calling Send while a SendAsync call is in progress or calling SendAsync multiple times before all previous calls have completed causes an InvalidOperationException.

It will send multiple ping requests to the server. That's shouldn't make the server crash. Although you might need to check with your network administrator whether multiple PING requests form the same IP (the IP of your web server) won't be dropped by some firewall for example.

Related

WebRequest.GetResponse is not returning anything

I am stuck with this, I am calling a simple report server URL which returns report's PDF, but strangely the WebRequest.GetResponse method doesn't return anything, when I say this, I mean the code just stop executing at that point, no exception, no error, no status code, no event viewer log on server, nothing!! And so I am not able to debug it
This is my code
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.PreAuthenticate = true;
req.UseDefaultCredentials = true;
req.Credentials = CredentialCache.DefaultNetworkCredentials;
req.ImpersonationLevel = TokenImpersonationLevel.Impersonation;
req.Timeout = int.MaxValue;
Log.Write("Log before");
var response = req.GetResponse();
Log.Write("Log after");
It just prints Log before log and then nothing is printed after that.
This code works perfectly fine when I run through visual studio and stops working when it is deployed in dev and test servers!
I am just expecting it to atleast through the exception or return unauthorized or any other status code, then I will be able to debug the issue.
Any suggestions what I can try to debug it?
Have you tried leaving it for 24 days 20 hours 31 minutes and 24 seconds? In other words, have you left it for as long as you have set the timeout to?
The server is not returning a response and the code is waiting int.MaxValue milliseconds to tell you that. The most likely cause of this is that there is a piece of networking infrastructure between your client and server that is stopping the request. This could be a firewall or proxy. It may also be being caused by the server not liking the request and refusing to respond.
Things I would try:
Try accessing the URL though a web browser on the machine that the code is failing on.
Set the timeout to something sensible like one minute and run the request to timeout.
Try pinging the remote server from the client machine.
Use a product like Fiddler to check what is actually being sent and received.
Have a chat with your network provider to see if they can help.
Check the server logs to see if the server has erred.
Change the first log to Log.Write("Log before: " + url); to check what is actually being requested.

how to Identify web service availability?

I have about 500 web services. I tired to use Ping but it dont seem to be accurate. I have tested web client and download to string. Using stopwatch i calculated the download time. Its also not so accurate. What is the best method to identify the availability of the web service?
Stopwatch sw = new Stopwatch();
sw.Start();
pingable = a.IsAddressAvailable(nameOrAddress);
sw.Stop();
public bool IsAddressAvailable(string address)
{
try
{
System.Net.WebClient client = new WebClient();
client.DownloadData(address);
return true;
}
catch
{
return false;
}
}
You can create an Endpoint returning the status of your services. You can call this particular webservice time to time to check availability. If the server dont answer for a predetermined number of retries) you can consider it Down.
Its a webservice to check server and services status.
You will need to think about setting up a healthcheck strategy.
Depends on the layer, you want to check:
Network availability? Ping should be enough, as long as the ICMP packets are not rejected on their way by any firewall. Maybe not a really meaningful test.
Socket/application server availability? A simple recurring socket connection check (for examples, see Java detect lost connection) is the most efficient way of checking, if there is a listener up and running on your application server. But in some cases, this test is not enough in case the http server of your webservice is up and running, but your webservice has for instance issues behind (database problems for instance)
Functional availability? The most reliable but also most complex strategy. You will need to a) implement a ping service on the soap-layer of your webservice or b) invoke existing webservice operations for checking their availability. But, you must be careful when invoking writing services. Either you design them in an idempotent manner (retries/duplicate requests don't change anything), or you prepare some functional harmless invalid request towards your write services and make an assert on your healtcheck-client against the response.

Reliable Ordered Messages, Not read by client after first time. (Lidgren)

This may be a little long of a post
I have a server and a client, and i have the ability to see the sent/rec messages and count them on both machines live, so that i dont have to go into debug in VS2012,
i am running my server on an alternate machine in California so that the server and the client are on completely different IP's and running through the internet for live tests, they are using DNS names and resolving them fine.
Both Physical PC's have no antivirus and no firewall and windows firewall is off on both.
Both machines have wireshark installed for tracking my packets on UDP port 29999
How the sequence works is the client sends a logon, the server verifies the client by some credentials and the server sends player information (stats)
When starting the server executable the first time, the messages come through to the client without failing. every time
if you restart the client and try again the client does not receive the messages.
1) The counter on the server.exe increments properly,
2) Server's Wireshark on the server shows the messages sent
3) Client's Wireshark sees the UDP packets come in on the proper port from the proper IP address
but the client.exe message counter does not increment.
If i run the client in DEBUG mode in VS2012 and set a breakpoint as shown here :
while ((_NetworkIncomingMsg = _NetworkClient.ReadMessage()) != null)
{
ReadInTime = DateTime.Now; // <<-- break point here
// blah blah more code
}
It never hits, no message is ever received.
Its important to note that if i but a break point on the while statement, yes it is firing, but no message is read, and thus its null, and thus skips the code
I believe it has something to do with either the timing or the placement of the ReadServerMessages() method.
i have the ReadServerMessages() method being firing on a Timer as an event under its elapsed as shown here. The timer is constructed to fire ever 1.0 milliseconds. As this works pretty much flawlessly in every other portion of the software including when actully connected to a dedicated server and constantly sending packets.
public System.Timers.Timer ClientNetworkTick = new System.Timers.Timer(1.0);
void update_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// Check if server sent new messages
try
{
ReadServerMessages();
}
catch (Exception ex)
{
}
}
Any thoughts? any thing i left out let me know thanks!

Check status of the server continuously

I have C# based client-server architecture. The client will connect to server and exchange data.
I need to check the server status continuously and if the server goes down (removed from the network or shut down) the client needs to indicate that.
If I am using Ping utility in the client program, which will be the best method in terms of performance i.e, monitor via a separate thread or through a background class?
public static bool GetPingResponse(string IpAddress, int timeout = 3000)
{
var ping = new Ping();
var reply = ping.Send(IpAddress, timeout);
if (reply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
Is there any other option better than Ping in terms of performance and consuming the resource?
You'll want to make sure your call is representative of "being available". For example, if it's an http server, making a ping request just says it's reachable via the network. The web server could be down but the network stack is up. If that's the case, make a call to something that exercises more of the stack like http. If there's backend databases, the call could go through to the database server. It all depends on your definition of "available". If the point is to say the application is available, you should look into a call that exercises a path through the full stack.
It also depends on what the server has available and what's open via firewalls between the clients and the servers.
Ping uses ICMP which some sites choose to block via firewalls. If that's open, it's a cheap call with little overhead.
If that's blocked and it's a web server for example over http, you could have a cheap endpoint on the web server that you could call via http client in C#.
You'll want to poll on a background thread probably with a timer. Look into the BackgroundWorker class or ThreadPool.QueueUserWorkitem.
Finally, you could look into a monitoring solution - there's many out there which could monitor multiple facets of your server(s). If you go that route, the client could simply query the monitoring solution which might be better since you wouldn't have n clients polling your servers - just one monitoring stack.
On a side note, the last block of code can be simplified down to:
return reply.Status == IPStatus.Success;

Cannot get Socket.Poll and Socket.Connected to work as desired

Okay I know there is lots of info out there on this and I promise you I have read it all and tried umpteen different methods to get this working!!
I have a socket server program which runs on a laptop. I then have up to 50 laptops connected wirelessly via the same LAN to the server. The client laptops all connect to the server (using Socket.ConnectAsync) and the server uses async methods as well to send and receive data. The server shows a list of connected client laptops to the user and this list seems to be accurate and picks up whenever a client disconnects and connects. However, the client laptops never seem to detect when connection to the server has been lost under certain circumstances (ie if server program crashes, if server laptop goes in to standby mode etc.) I have got a timer on the client laptops which polls the connection every 5 seconds as follows:
bool SocketConnected(Socket s)
{
bool part1 = s.Poll(0, SelectMode.SelectWrite);
bool part2 = (s.Available == 0);
if (!part1 && part2)
{
return false;
}
else
{
return true;
}
}
I have tried using all selectmodes (SelectWrite,SelectRead,SelectError) and have tried using different time out values. I have tried checking s.Connected value after these operations and have tried all manners of other methods to determine the connection state and nothing seems to produce reliable results!! I think I can achieve the result I desire by sending dummy information every 5 seconds and checking s.Connected after doing so, however I don't really want to do this as each laptop is already sending lots of data to the server as it is. Any help at all is massively appreciated! Thanks
The only reliable way to check if a connection is alive is to send something to the other end and see if it arrives. You can do this either manually by sending and receiving a "ping" value from time to time, or automatically by enabling the KeepAlive socket option.
The MSDN documentation for Socket.Poll is very explicit about the exact situations (server crashes, standby) you mentioned:
This method cannot detect certain kinds of connection problems, such
as a broken network cable, or that the remote host was shut down
ungracefully. You must attempt to send or receive data to detect these
kinds of errors.

Categories

Resources