Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:49:38+00:00 2026-06-13T23:49:38+00:00

On Linux using MONO. How do I use C# to pair a port number

  • 0

On Linux using MONO.

How do I use C# to pair a port number to a service ?

example:

port 80 = http
port 443 = https

and output it to the console, I need this for a simple port scanner I built.

I know this function exists in ruby:

Socket.getservbyport(80, "tcp")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:49:39+00:00Added an answer on June 13, 2026 at 11:49 pm

    Mono Linux version

    I had a bit of time on my hands so set up Mono on my Ubuntu Linux box to test with. The Mono PInvoke implementation of getservbyport() and getservbyname() is simpler than on Windows (just load libc which has the networking stuff built in). Here’s the example code for reference in case anyone ever wants it 😉

    namespace SocketUtil
    {
        using System;
        using System.Net;
        using System.Collections.Generic;
        using System.Runtime.InteropServices;
        using System.Runtime.Serialization;
    
        [Serializable]
        public class SocketUtilException : Exception
        {
    
            public SocketUtilException()
            {
            }
    
            public SocketUtilException(string message)
                : base(message)
            {
            }
    
            public SocketUtilException(string message, Exception inner)
                : base(message, inner)
            {
            }
    
            protected SocketUtilException(
                SerializationInfo info, StreamingContext context)
                : base(info, context)
            {
            }
        }
    
        public static class SocketUtil
        {
    
            [StructLayoutAttribute(LayoutKind.Sequential)]
            public struct servent
            {
                public string s_name;
                public IntPtr s_aliases;
                public ushort s_port;
                public string s_proto;
            }
    
    
            [DllImport("libc", SetLastError = true, CharSet = CharSet.Ansi)]
            private static extern IntPtr getservbyname(string name, string proto);
            [DllImport("libc", SetLastError = true, CharSet = CharSet.Ansi)]
            private static extern IntPtr getservbyport(ushort port, string proto);
    
            public static string GetServiceByPort(ushort port, string protocol, out List<string> aliases)
            {
                var netport = unchecked((ushort)IPAddress.HostToNetworkOrder(unchecked((short)port)));
                var result = getservbyport(netport, protocol);
                if (IntPtr.Zero == result)
                {
                    throw new SocketUtilException(
                        string.Format("Could not resolve service for port {0}", port));
                }
                var srvent = (servent)Marshal.PtrToStructure(result, typeof(servent));
                aliases = GetAliases(srvent);
                return srvent.s_name;
    
            }
    
            private static List<string> GetAliases(servent srvent)
            {
                var aliases = new List<string>();
                if (srvent.s_aliases != IntPtr.Zero)
                {
                    IntPtr cb;
    
                    for (var i = 0;
                        (cb = Marshal.ReadIntPtr(srvent.s_aliases, i)) != IntPtr.Zero;
                        i += Marshal.SizeOf(cb))
                    {
                        aliases.Add(Marshal.PtrToStringAnsi(cb));
                    }
                }
                return aliases;
            }
    
            public static ushort GetServiceByName(string service, string protocol, out List<string> aliases)
            {
    
                var result = getservbyname(service, protocol);
                if (IntPtr.Zero == result)
                {
                    throw new SocketUtilException(
                        string.Format("Could not resolve port for service {0}", service));
                }
    
                var srvent = (servent)Marshal.PtrToStructure(result, typeof(servent));
                aliases = GetAliases(srvent);
                var hostport = IPAddress.NetworkToHostOrder(unchecked((short)srvent.s_port));
                return unchecked((ushort)hostport);
    
            }
        }
        class Program
        {
    
            static void Main(string[] args)
            {
    
                try
                {
                    List<string> aliases;
                    var port = SocketUtil.GetServiceByName("https", "tcp", out aliases);
    
                    Console.WriteLine("https runs on port {0}", port);
                    foreach (var alias in aliases)
                    {
                        Console.WriteLine(alias);
                    }
    
                    Console.WriteLine("Reverse call:{0}", SocketUtil.GetServiceByPort(port, "tcp", out aliases));
    
                }
                catch (SocketUtilException exception)
                {
                    Console.WriteLine(exception.Message);
                    if (exception.InnerException != null)
                    {
                        Console.WriteLine(exception.InnerException.Message);
                    }
                }
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am porting a C# program to Linux (using Mono). The only compatibility issues
I'm trying to get a global hotkey working in Linux using Mono. I found
I am trying to run a .NET command line application in Linux using Mono.
I am using C# in Mono and I'm trying to use pinvoke to call
I'm trying to use mono to build a C# application for linux. But it
I'm programming a computer game in C#/Mono using OpenTK library. I want to use
I'm testing my app with mono in prevision of a Linux port, and I
I am developing cross platform(Windows, Linux) desktop application using Mono and Gtk#. Application will
I get the following error when using the ImageResizer module on Mono in Linux:
writing a server that runs on linux (Ubuntu) using mono. and a client that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.