The following code is a TCP server program that simply sends back "HELLO !!" to the customer.
When I start the server with port 80, bind () returns Permission denied .
Port 12345 is fine.
How can I use port 80 for this server program?
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main(){ int sock0; struct sockaddr_in addr; struct sockaddr_in client; int len; int sock; char *message; message = "HELLO !!"; sock0 = socket(AF_INET,SOCK_STREAM,0); addr.sin_family = AF_INET; addr.sin_port = htons(80); inet_pton(AF_INET,"127.0.0.1",&addr,sizeof(addr)); bind(sock0,(struct sockaddr *)&addr,sizeof(addr)); perror("bind"); len = sizeof(client); sock = accept(sock0,(struct sockaddr *)&client,&len); perror("accept"); write(sock,message,sizeof(message)); perror("write"); close(sock); return 0; }
c sockets
user1345414
source share