@Lasse >> hvilket af eksemplerne bruger du ? det første skal du nok lige læse kommentarerne til ex:
"I added if(ex.SocketErrorCode == SocketError.ConnectionRefused || ex.SocketErrorCode == SocketError.TimedOut)"
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Net.Sockets;
- using System.Threading;
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Response.Write("http: " +IsServerUp("web01.upsite.dk", 80, 500)+"<br>");
- Response.Write("vpn: " +IsServerUp("web01.upsite.dk", 1723, 500)+"<br>");
- Response.Write("rdp: " + IsServerUp("web01.upsite.dk", 3389, 500) + "<br>");
- Response.Write("ftp: " +IsServerUp("web01.upsite.dk", 21, 500)+"<br>");
- Response.Write("unknown: " +IsServerUp("web01.upsite.dk", 666, 500)+ "<br>");
- }
-
- public static bool IsServerUp(string server, int port, int timeout)
- {
- bool isUp;
-
- try
- {
- using (TcpClient tcp = new TcpClient())
- {
- IAsyncResult ar = tcp.BeginConnect(server, port, null, null);
- WaitHandle wh = ar.AsyncWaitHandle;
-
- try
- {
- if (!wh.WaitOne(TimeSpan.FromMilliseconds(timeout), false))
- {
- tcp.EndConnect(ar);
- tcp.Close();
- throw new SocketException();
- }
-
- isUp = true;
- tcp.EndConnect(ar);
- }
- finally
- {
- wh.Close();
- }
- }
- }
- catch (SocketException e)
- {
- HttpContext.Current.Trace.Warn("TCP connection to server {0} failed.", server);
- isUp = false;
- }
-
- return isUp;
- }
- }
Giver mig :
http: True
vpn: False
rdp: True
ftp: True
unknown: False
Indlæg senest redigeret d. 07.11.2011 15:49 af Bruger #5789