套接字编程权限被拒绝 [英] Socket programing Permission denied

查看:96
本文介绍了套接字编程权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继code是TCP服务器程序只是发回HELLO!到客户端。

当我运行服务器端口80,返回的bind()权限被拒绝

12345端口就可以了。

如何使用端口80用于该服务器程序?

 的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; netinet / in.h中>INT
主要(){
    INT sock0;
    结构sockaddr_in的地址;
    结构sockaddr_in的客户端;
    INT LEN;
    INT袜子;
    字符*消息;
    消息=HELLO!
    sock0 =插座(AF_INET,SOCK_STREAM,0);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    inet_pton(AF_INET,127.0.0.1,&安培;地址,的sizeof(地址));
    绑定(sock0,(结构sockaddr *)及地址,的sizeof(地址));
    PERROR(绑定);
    LEN = sizeof的(客户端);
    袜子=接受(sock0,(结构sockaddr *)及客户端,和放大器; LEN);
    PERROR(接受);
    写(袜子,消息的sizeof(消息));
    PERROR(写);
    关闭(袜子);
    返回0;
}


解决方案

低于1024的端口被认为是特权,只能绑定到一个同样的特权用户。(阅读:

任何超过并包括1024是自由使用的人。

OT:你可能已经知道了,但在你的例子中,端口是为HTTP Web服务器。凡是听了这个端口的的说话HTTP,太。一个简单的世界你好并不足够。 ; - )

Following code is TCP server program just send back "HELLO!!" to client.

When I run server with port 80, bind() is returned Permission denied.

Port 12345 is OK.

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;
}

解决方案

Ports below 1024 are considered "privileged" and can only be bound to with an equally privileged user (read: root).

Anything above and including 1024 is "free to use" by anyone.

OT: you may know this already, but the port in your example is that for HTTP web servers. Anything listening to this port should speak HTTP, too. A simple "hello world" does not suffice. ;-)

这篇关于套接字编程权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆