yunhasnawa
yunhasnawa

Reputation: 965

C++ Gain Sufficient Privilege to Bind to Port 80

I am writing a C++ application that accepts HTTP request from browser and sending back the response.

int port = 2127; //80;
int bind_res = ::bind(port, p_ref->ai_addr, p_ref->ai_addrlen);

The application works as expected on OSX when I set it to listen at port 2127. But when I set it to listen on port 80, the application stopped working with exception:

server: bind: Permission denied

My application is a command line application, and it opened by other GUI application, also written in C++ but with Qt.

How can I gain enough privilege to bind to port 80? Is there any API to show user and password prompt - like in XAMPP Control Panel, for example - to get those privilege?

EDIT: I write the app for running on OSX 10.8 Mountain Lion.

Upvotes: 0

Views: 627

Answers (1)

Öö Tiib
Öö Tiib

Reputation: 11011

Usually you cannot bind because of two issues:

  1. something (Apache server, Skype) is already bound to that port.
  2. you run as user and try to bind to a port below 1024.

It feels you have problem with that second case. For running with super user privileges there is sudo command on all Unixes (OSX is Unix).

If you do not like sudo then you can permanently redirect the port to other port that can be bound by user.

Upvotes: 1

Related Questions