views:

159

answers:

2

I'm taking a class right now where some of the examples are in C#. Since my laptop runs Linux, I'm using Mono 2.6.7 on Ubuntu.

I'm trying to compile the following code:

using System.Net.Sockets;
using System.Net;
using System;

/// <summary>
/// Example program showing simple TCP socket connections in C#.NET.
/// Rather than reading and writing byte arrays, this example show
/// how to use a stream reader in the client.
/// TCPSocketServer is the socket server.
/// <author>Tim Lindquist ASU Polytechnic Department of Engineering</author>
/// <version>September, 2009</version>
/// </summary>
public class TCPSocketServer {

  public static void Main (string [] args) {
    IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
    TcpListener tcpl = new TcpListener(ipAddress, 9090);
    tcpl.Start();
    Console.WriteLine("TCPSocketServer waiting for connections on 9090");
    Socket sock = tcpl.AcceptSocket();
    string msg = "Hello Client";
    Byte[] msgBytes = System.Text.Encoding.ASCII.GetBytes(msg);
    sock.Send(msgBytes, msgBytes.Length, SocketFlags.DontRoute);
    Console.WriteLine("Message-Hello Client-sent to client.");
    tcpl.Stop();
    sock.Close();
  }
}

When I compile the code, I get:

/home/vivin/Projects/cst420/CSSockets/src/TCPSocketServer.cs(16,31): error CS0122: `System.Net.Dns.GetHostEntry(string)' is inaccessible due to its protection level
/usr/lib/mono/gac/System/1.0.5000.0__b77a5c561934e089/System.dll (Location of the symbol related to previous error)
ompilation failed: 1 error(s), 0 warnings

I'm a C# novice; this is the first C# program I've ever compiled. I tried searching on Google, but I didn't get very many hits for this problem. Is this a problem with Mono?

+7  A: 

System.Net.Dns.GetHostEntry was introduced in the .NET Framework 2.0. In Mono's implementation, it's also present in the pre-2.0 versions, but marked as internal instead of public. You seem to be compiling against the .NET Framework 1.0.

From /mcs/class/System/System.Net/Dns.cs:

#if NET_2_0
    public
#else
    internal
#endif
    static IPHostEntry GetHostEntry (string hostNameOrAddress)
    {
        // ...
dtb
In retrospect, the "System/1.0.5000.0__b77a5c561934e089" should have been a give-away :)
Jon Skeet
That was it! I was using `mcs`. When I used `gmcs` the problem went away. Thanks!
Vivin Paliath
+5  A: 

Are you using the gmcs compiler?

Gabriel Burt