Click here to Skip to main content
15,861,168 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: error server Pin
Member 1265427723-Jun-20 1:34
Member 1265427723-Jun-20 1:34 
AnswerRe: error server Pin
OriginalGriff22-Jun-20 23:43
mveOriginalGriff22-Jun-20 23:43 
QuestionAnother NetCore question Pin
pkfox31-May-20 4:58
professionalpkfox31-May-20 4:58 
AnswerRe: Another NetCore question Pin
Mycroft Holmes31-May-20 12:22
professionalMycroft Holmes31-May-20 12:22 
GeneralRe: Another NetCore question Pin
pkfox1-Jun-20 1:58
professionalpkfox1-Jun-20 1:58 
GeneralRe: Another NetCore question Pin
Mycroft Holmes1-Jun-20 12:44
professionalMycroft Holmes1-Jun-20 12:44 
AnswerRe: Another NetCore question Pin
Richard Deeming1-Jun-20 3:31
mveRichard Deeming1-Jun-20 3:31 
GeneralRe: Another NetCore question Pin
pkfox1-Jun-20 4:08
professionalpkfox1-Jun-20 4:08 
Hi Richard, what I do to see if my API service is running is, ping it and if I get a reply create a socket on the port and try to open it

This is the code I use ( which I believe I got from OG years ago )
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace BOMBuilder
{
    public class NetworkUtils
    {
        private StringBuilder errormessage = new StringBuilder();
        private bool portIsOpen = false;
        private bool serverIsUp = true;
        private bool hasErrors = false;

        private bool HasErrors
        {
            get
            {
                return hasErrors;
            }
            set
            {
                hasErrors = value;
            }
        }

        private bool ServerIsUp
        {
            get
            {
                return serverIsUp;
            }
            set
            {
                serverIsUp = value;
            }
        }

        private bool PortIsOpen
        {
            get
            {
                return portIsOpen;
            }
            set
            {
                portIsOpen = value;
            }
        }

        private StringBuilder Errormessage
        {
            get
            {
                return errormessage;
            }
            set
            {
                errormessage = value;
            }
        }

        public string GetErrorMessage()
        {
            return Errormessage.ToString();
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        private struct ICMP_OPTIONS
        {
            public byte Ttl;
            public byte Tos;
            public byte Flags;
            public byte OptionsSize;
            public IntPtr OptionsData;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        private struct ICMP_ECHO_REPLY
        {
            public int Address;
            public int Status;
            public int RoundTripTime;
            public short DataSize;
            public short Reserved;
            public IntPtr DataPtr;
            public ICMP_OPTIONS Options;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)]
            public string Data;
        }

        [DllImport("icmp.dll", SetLastError = true)]
        private static extern IntPtr IcmpCreateFile();

        [DllImport("icmp.dll", SetLastError = true)]
        private static extern bool IcmpCloseHandle(IntPtr handle);

        [DllImport("icmp.dll", SetLastError = true)]
        private static extern int IcmpSendEcho(IntPtr icmpHandle, int destinationAddress, string requestData, short requestSize, ref ICMP_OPTIONS requestOptions, ref ICMP_ECHO_REPLY replyBuffer, int replySize, int timeout);
        // Try to ping ip and open port.
        public void Ping(IPAddress ip, int port)
        {
            this.ServerIsUp = false;
            this.PortIsOpen = true;
            IntPtr icmpHandle = IcmpCreateFile();
            ICMP_OPTIONS icmpOptions = new ICMP_OPTIONS();
            icmpOptions.Ttl = 20;
            ICMP_ECHO_REPLY icmpReply = new ICMP_ECHO_REPLY();
            string sData = "x";

            IcmpSendEcho(icmpHandle, BitConverter.ToInt32(ip.GetAddressBytes(), 0), sData, (short)sData.Length, ref icmpOptions, ref icmpReply, Marshal.SizeOf(icmpReply), 1);
            IcmpCloseHandle(icmpHandle);
            ServerIsUp = icmpReply.Status == 0;

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                if (ServerIsUp)
                    s.Connect(ip, port);
            }
            catch (SocketException se)
            {
                PortIsOpen = false;
                HasErrors = true;
                Errormessage.Append(se.Message);
            }
            finally
            {
                if (s.Connected)
                    s.Disconnect(false);
            }
        }

        // ****** I call this method *****
        public bool ServerIsRunningOn(IPAddress iPAddress, int port)
        {
            this.Ping(iPAddress, port);
            return (this.ServerIsUp && this.PortIsOpen);
        }
    }
}
"We can't stop here - this is bat country" - Hunter S Thompson - RIP

AnswerRe: Another NetCore question Pin
jkirkerx1-Jun-20 13:47
professionaljkirkerx1-Jun-20 13:47 
GeneralRe: Another NetCore question Pin
pkfox4-Jun-20 10:54
professionalpkfox4-Jun-20 10:54 
QuestionCheckboxList Styling Problem Pin
feelblue8730-May-20 6:14
feelblue8730-May-20 6:14 
QuestionNet Core API question Pin
pkfox22-May-20 5:28
professionalpkfox22-May-20 5:28 
AnswerRe: Net Core API question Pin
Richard Deeming22-May-20 5:56
mveRichard Deeming22-May-20 5:56 
GeneralRe: Net Core API question Pin
pkfox22-May-20 7:05
professionalpkfox22-May-20 7:05 
QuestionHow do I multiple group in Active Directory ? Pin
ugurarslanm19-May-20 4:58
ugurarslanm19-May-20 4:58 
QuestionHow to invoke POST method by adding [FromBody] attribute in Postman tool Pin
meeram3917-May-20 23:33
professionalmeeram3917-May-20 23:33 
AnswerRe: How to invoke POST method by adding [FromBody] attribute in Postman tool Pin
Richard Deeming18-May-20 1:12
mveRichard Deeming18-May-20 1:12 
GeneralRe: How to invoke POST method by adding [FromBody] attribute in Postman tool Pin
meeram3918-May-20 1:19
professionalmeeram3918-May-20 1:19 
GeneralRe: How to invoke POST method by adding [FromBody] attribute in Postman tool Pin
Stefanie Eberhardt19-May-20 4:29
Stefanie Eberhardt19-May-20 4:29 
QuestionAPI fail to use PUT method Pin
feelblue8717-May-20 22:42
feelblue8717-May-20 22:42 
AnswerRe: API fail to use PUT method Pin
Richard Deeming18-May-20 1:06
mveRichard Deeming18-May-20 1:06 
Questionasp.net Pin
lankaudaranga16-May-20 22:01
lankaudaranga16-May-20 22:01 
AnswerRe: asp.net Pin
Mycroft Holmes17-May-20 12:14
professionalMycroft Holmes17-May-20 12:14 
Questiondévelopper et utiliser le web service Pin
Member 1483395815-May-20 21:27
Member 1483395815-May-20 21:27 
Questiondévelopper et utiliser le web service Pin
Member 1483395815-May-20 21:27
Member 1483395815-May-20 21:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.