-1

I'm using Windows 10 and Visual Studio 2016. I can't bind to port 80. I can bind to all other ports. The error printed is: "Bind of IP address 0.0.0.0 returned an error, port 80: No error"

Here is my code:

/*  bind this socket to the server's Internet address */
if( bind(fd,(struct sockaddr *)server_addr,sizeof(struct sockaddr_in))<0 )
{
    printf("Bind of IP address %s returned an error, port %d: %s\n",
        inet_ntoa(server_addr->sin_addr), ntohs(server_addr->sin_port),
        strerror(errno));
    //close(fd);
    return -1;
}
eddyq
  • 879
  • 1
  • 13
  • 25
  • Please explain why you gave me a -1 here. – eddyq Sep 19 '16 at 12:38
  • You need to call `strerror()` before calling any other system calls. You're not reporting the error correcty here. – user207421 Sep 19 '16 at 23:09
  • Wrong ... calling strerror() convertsf an error number to a string. If it is done before the system call then it will convert the wrong error number. – Eddy Sep 22 '16 at 11:16
  • @Eddy Please read what I wrote. I said 'before any *other* system calls'. It is obvious from the displayed text that `errno` has been corrupted prior to calling `strerror()` here, presumably by `inet_ntoa()`. You don't get 'No error' after `bind()` returns -1. – user207421 Sep 23 '16 at 04:02
  • Oops, sorry you are correct. What happens here is that this code first calls bind, then it calls inet_ntoa, then ntohs and finally strerror. Since the error was due to bind then strerror needs to be called before inet_ntoa and ntohs because those will reset the error to 0. – Eddy Feb 01 '17 at 01:33
  • https://superuser.com/a/1504618/45643 – Brandon Boone Apr 02 '20 at 19:58

2 Answers2

2

Well, you can use netstat to see if anyone else is listening, see this article:

https://technet.microsoft.com/en-us/library/bb490947.aspx

Find which process is already using port 80 and stop it.

You also need to be an admin or explicitly grant access to the user you're running as if you're binding port < 1024. See here

HttpListener Access Denied

Community
  • 1
  • 1
mikeb
  • 10,578
  • 7
  • 62
  • 120
2

Use "netstat -o -q -a -n". Then use task manager and look at the Details tab. Click to sort the PID as low to high. Find the PID and notice the name of the program that has the port open. In my case System is listening on port 80 and since you can't kill System then you basically can't bind to port 80.

eddyq
  • 879
  • 1
  • 13
  • 25