When I try to bind port 80 to a socket in c, i always get the error, that I don't have permission to use this port. is there an easy way to get this permission?
Usually only the superuser (root) can bind to 'privileged' ports (i.e. those port numbers below 1024).
This means that you either have to run your program as root or make your executable 'suid root'.
Both of these have security consequences so you may want to consider using the suid approach and relinquishing superuser privileges once the bind call has been made.
If you are on a shared system (like a university computer) and not root then there is no 'easy' way to get that permission, by design.
You will find this tutorial very helpful on network programming with C/C++.
And, by the way, ANSI C has no way to access the network. It is the OS supplied libraries (the BSD socket API, also ported to Windows as winsock
) that provide this capability.
It's just as @Charles Bailey puts it...and I would like to add that this is why one used to see http server addresses on 8080 by port specification in the URL as http://some.url:8080/
Traditionally only root can bind sockets to ports under 1024.
Ports 1024 and below are called Privileged Ports, binding to these ports requires elevated permission.
Ports above 1024 are called Emphemeral Ports. Binding to these requires no special permissions.
The easiest way to gain access to privilged ports is to be the root user.
Yes, you can easily bind to port 80. Use Apache. Write a web application. Apache binds to port 80 and runs your web application.
Are you trying to write the next Apache? If so, you'll need to learn about the setuid API call in your operating system.
If you're not writing a new version of Apache, most people use a non-privileged port. 8000 is popular, so is 8080.
S.Lott's reply may have triggered very negative reactions but his idea is far from stupid: if the original question is for a real program (not a school assignment), developing it as an application behind an HTTP server is often a reasonable choice. That way, you can leave a lot of low-level details to a good and well debugged program, Apache.
The application does not have to be a CGI, it can be an Apache module. Apache, from version 2, is no longer just a HTTP server. It is now a platform to develop network programs. Writing an Apache module may be the correct answer to the original question (see Apache documentation)
Normal programs can't bind "privileged" ports - those below 1024. This is a mostly obsolete security feature of UNIX-like operating systems.
Running as a superuser, although suggested by many others here, is a bad solution to this problem. If you are running on a Debian or Ubuntu system, I suggest installing the authbind package, which will allow you to grant your program permission to open privileged ports without actually having to give your program any other special permissions.
If you're running on any other system, I suggest installing debian or ubuntu ;-).