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

  • SEARCH
  • Home
  • 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 6961795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:34:36+00:00 2026-05-27T15:34:36+00:00

Possible Duplicate: converting C to C# i have problem in converting by c code

  • 0

Possible Duplicate:
converting C to C#

i have problem in converting by c code to c#
i hav two structers

  struct sockaddr {
      ushort  sa_family;
      char    sa_data[14];
  };

struct sockaddr_in {
    short   sin_family;
    u_short sin_port;
    struct  in_addr sin_addr;
    char    sin_zero[8];
};

and i use to cast it like this…
to get the ip address. from the sockaddr_in structure

 ip = inet_ntoa((( sockaddr_in*)name)->sin_addr); 

now i converted the structure in c# as

 public struct sockaddr
    {
         public static ushort sa_family;            
         public static char[] sa_data = new char[14];

    };

and..

 [StructLayout(LayoutKind.Sequential, Size = 16)]
     public struct sockaddr_in
     {
         public const int Size = 16;

         public short sin_family;
         public ushort sin_port;
         public struct in_addr
         {
             public uint S_addr;
             public struct _S_un_b
             {
                 public byte s_b1, s_b2, s_b3, s_b4;
             }
             public _S_un_b S_un_b;
             public struct _S_un_w
             {
                 public ushort s_w1, s_w2;
             }
             public _S_un_w S_un_w;
         }
         public in_addr sin_addr;
     }

can any one plz help me to get ip address from casting the sockaddr to sockaddr_in as i tried it in c… its working fine… how do i do the same in C#…

thanks u in Advance..

  • 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-05-27T15:34:37+00:00Added an answer on May 27, 2026 at 3:34 pm

    What you try to achieve is certainly possible in C#. Here is an example:

    [StructLayout(LayoutKind.Sequential)]
    unsafe struct SockAddr {
        public ushort sa_family;
        public fixed byte sa_data[14]; // Note: sizeof(char) == 2 in C#
    }
    
    [StructLayout(LayoutKind.Sequential)]
    unsafe struct SockAddr_In {
        public short sin_family;
        public ushort sin_port;
        public In_Addr sin_addr;
        public fixed byte sin_zero[8];
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct In_Addr {
        public byte s_b1, s_b2, s_b3, s_b4;
    }
    
    public static unsafe void CStyle() {
        SockAddr s;
        SockAddr* ps = &s;
    
        SockAddr_In* psa = (SockAddr_In*)ps;
    
        var inAddr = new In_Addr();
    
        inAddr.s_b1 = 192;
        inAddr.s_b2 = 168;
        inAddr.s_b3 = 168;
        inAddr.s_b4 = 56;
        psa->sin_addr = inAddr;
        Console.WriteLine("{0}.{1}.{2}.{3}", 
                           psa->sin_addr.s_b1, psa->sin_addr.s_b2, 
                           psa->sin_addr.s_b3, psa->sin_addr.s_b4);
    }
    

    However as others have pointed out, such approaches lead to non-idiomatic C# code and cannot generally be recommended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Converting date From one format to another I have a date, for
Possible Duplicate: Converting ereg expressions to preg I'm trying to fix some old code
Possible Duplicate: Converting ereg expressions to preg I have this ereg() expression: ^[0-9]{8}\T{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}$ How
Possible Duplicate: Byte array in objective-c Am converting some Java code to Objective-C and
Possible Duplicate: Parse C# string to DateTime I am facing a problem while converting
Possible Duplicate: Converting Seconds to HH:MM:SS I have an integer as a number of
Possible Duplicate: Converting unix timestamp string to readable date in Python I have a
Possible Duplicate: Better way of converting a Map[K, Option[V]] to a Map[K,V] I have
Possible Duplicate: Converting XML to JSON using Python? I am importing an XML feed
Possible Duplicate: Converting XML to JSON using Python? I'm doing some work on App

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.