Emre Kabaoglu
Emre Kabaoglu

Reputation: 13146

C# listening 80 port

I want to listen port 80. For this, I coded a TCP listener and gave it admin rights. But it doesn't work (it fails).

This is the error:

An attempt was made to access a socket in a way forbidden by its
access permissions

My code:

static void Main(string[] args)
{
    WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
    if (hasAdministrativeRight == true)
    {
        TcpListener server;
        Int32 port = 80;
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");
        server = new TcpListener(localAddr, port);
        server.Start();
        Byte[] bytes = new Byte[256];
        String data = null;
        while (true)
        {
            Console.Write("Waiting for a connection... ");
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("Connected!");
            data = null;
            NetworkStream stream = client.GetStream();
            int i;
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                Console.WriteLine("Received: {0}", data);
                data = data.ToUpper();

                byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                stream.Write(msg, 0, msg.Length);
                Console.WriteLine("Sent: {0}", data);
            }

            client.Close();
        }
    }
}

Upvotes: 2

Views: 9390

Answers (3)

Davide Cannizzo
Davide Cannizzo

Reputation: 3134

As far I know, to bind a TCP connection through the port 80, you need to get the administrator privileges. So, you must run your program as administrator, and the HttpListener you're using should work correctly. Try adding this to your manifest file:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Upvotes: 0

Esen
Esen

Reputation: 973

Port 80 is used by IIS (default) if you have IIS in your machine then follow this link https://stackoverflow.com/a/108397/1221319 to stop IIS from listening port 80.

Upvotes: 0

slolife
slolife

Reputation: 19870

I suspect port 80 is already in use by either IIS or maybe Skype. You will need to turn them off or change the port they use.

Run this and figure out which process (PID) is using port 80:

C:\> netstat -ano

Active Connections
  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:80             0.0.0.0:0              LISTENING       4

If the PID points to the System process (4 in my case), then that is IIS I believe.

MSDN Socket Error Codes

For even more detail, wrap your server.Start() call in a try/catch and catch SocketException and examine the SocketException.ErrorCode.

try
{
    server.Start();
}
catch (SocketException exception)
{
    Console.Write(exception.ErrorCode);
}

MSDN TcpListener.Start()

Upvotes: 3

Related Questions