当前位置: 技术问答>linux和unix
bind函数,运行的时候出现bind:permission denied 怎么解决?
来源: 互联网 发布时间:2016-06-09
本文导语: 提示错误如下: [root@localhost~]# ./yping 5000 192.168.0.3 bind:permission denied [root@localhost~]# ./yping 80 192.168.0.3 bind:permission denied 已经在root权限下运行了 但还是不行 请教各位高手怎么解决呢?十分感谢 代码为: #inc...
提示错误如下:
[root@localhost~]# ./yping 5000 192.168.0.3
bind:permission denied
[root@localhost~]# ./yping 80 192.168.0.3
bind:permission denied
已经在root权限下运行了 但还是不行 请教各位高手怎么解决呢?十分感谢
代码为:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define WAIT_TIME 3
#define BUFFER_SIZE 1024
u_short cal_cksum(const u_short *addr, register int len, u_short csum)
{
register int nleft = len;
const u_short *w = addr;
register u_short answer;
register int sum = csum;
/** 使用32位累加器实现校验码的计算
* 联系对16位数进行累加,最后将高16位与低16位进行累加
*/
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* 如果最后剩余8位,补齐到16位(补零,同时注意网络字节与
主机字节间的转换) */
if (nleft == 1)
sum += htons(*(u_char *)w 16) + (sum & 0xffff); /* 高低16位相加 */
sum += (sum >> 16); /* 消除高低位相加的进位问题 */
answer = ~sum; /* 取反 */
return (answer);
}
//ICMP报头解析代码
int unpack_packet_header(char *buf,int len,struct sockaddr_in *from)
{ int i,iphdrlen;
struct ip *ip;
struct icmp *icmp;
struct timeval *tvsend;
struct timeval tvrecv;
double rtt;
ip=(struct ip *)buf;
//ip报头长度= ip_hl*4
iphdrlen=ip->ip_hl*4;
//得到ICMP报文信息
icmp=(struct icmp *)(buf+iphdrlen);
//icmp报文总长度
len-=iphdrlen;
//如果长度小于要求,退出程序
if( lenicmp_type==ICMP_ECHOREPLY)
{ tvsend=(struct timeval *)icmp->icmp_data;
gettimeofday(&tvrecv,NULL);
//得到接收时间,并计算两者时间差
if((tvrecv.tv_usec-=tvsend->tv_usec)tv_sec;
rtt=tvrecv.tv_sec*1000+tvrecv.tv_usec/1000;
//显示时间信息
printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f msn",
len,
inet_ntoa(from->sin_addr),
icmp->icmp_seq,
ip->ip_ttl,
rtt);
} else
return -1;
}
int
main(int argc, char * argv[])
{
int sock;
struct sockaddr_in sin;
unsigned short local_port;
unsigned char protocol;
char * buffer, * buffer2, * dnsdata;
struct iphdr * ip_header;
struct icmphdr * icmp_header;
char * remote_ip_str;
unsigned short buffer_size, buffer_size2;
int tmp, len;
short randomseq;
//判断参数是否符合程序运行要求
if (argc != 3) {
fprintf(stderr, "USAGE: %s port destinationn", argv[0]);
return 1;
}
protocol = IPPROTO_ICMP;
local_port = atoi(argv[1]);
remote_ip_str = argv[2];
//创建通信所需套接字,注意套接字类型为原始套接字
if ((sock = socket(PF_INET, SOCK_RAW, protocol)) version = 4;
ip_header->tos = 0;
ip_header->tot_len = htons(buffer_size);
ip_header->id = rand();
ip_header->ttl = 64;
ip_header->frag_off = 0x40;
ip_header->protocol = protocol;
ip_header->check = 0;
ip_header->daddr = inet_addr(remote_ip_str);
ip_header->saddr = 0;
icmp_header = (struct icmphdr *) (ip_header + 1);
icmp_header->type = ICMP_ECHO;
icmp_header->code = 0;
icmp_header->un.echo.id = htons(local_port);
icmp_header->un.echo.sequence = 0;
struct timeval *tp = (struct timeval *)&buffer[28];
gettimeofday(tp, NULL);
icmp_header->checksum = cal_cksum((const u_short *) icmp_header, sizeof(struct icmphdr) + sizeof(struct timeval), 0);
//发送该IP数据包
if (sendto(sock, buffer, buffer_size, 0, (struct sockaddr *) &sin,
sizeof(struct sockaddr_in))
[root@localhost~]# ./yping 5000 192.168.0.3
bind:permission denied
[root@localhost~]# ./yping 80 192.168.0.3
bind:permission denied
已经在root权限下运行了 但还是不行 请教各位高手怎么解决呢?十分感谢
代码为:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define WAIT_TIME 3
#define BUFFER_SIZE 1024
u_short cal_cksum(const u_short *addr, register int len, u_short csum)
{
register int nleft = len;
const u_short *w = addr;
register u_short answer;
register int sum = csum;
/** 使用32位累加器实现校验码的计算
* 联系对16位数进行累加,最后将高16位与低16位进行累加
*/
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* 如果最后剩余8位,补齐到16位(补零,同时注意网络字节与
主机字节间的转换) */
if (nleft == 1)
sum += htons(*(u_char *)w 16) + (sum & 0xffff); /* 高低16位相加 */
sum += (sum >> 16); /* 消除高低位相加的进位问题 */
answer = ~sum; /* 取反 */
return (answer);
}
//ICMP报头解析代码
int unpack_packet_header(char *buf,int len,struct sockaddr_in *from)
{ int i,iphdrlen;
struct ip *ip;
struct icmp *icmp;
struct timeval *tvsend;
struct timeval tvrecv;
double rtt;
ip=(struct ip *)buf;
//ip报头长度= ip_hl*4
iphdrlen=ip->ip_hl*4;
//得到ICMP报文信息
icmp=(struct icmp *)(buf+iphdrlen);
//icmp报文总长度
len-=iphdrlen;
//如果长度小于要求,退出程序
if( lenicmp_type==ICMP_ECHOREPLY)
{ tvsend=(struct timeval *)icmp->icmp_data;
gettimeofday(&tvrecv,NULL);
//得到接收时间,并计算两者时间差
if((tvrecv.tv_usec-=tvsend->tv_usec)tv_sec;
rtt=tvrecv.tv_sec*1000+tvrecv.tv_usec/1000;
//显示时间信息
printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f msn",
len,
inet_ntoa(from->sin_addr),
icmp->icmp_seq,
ip->ip_ttl,
rtt);
} else
return -1;
}
int
main(int argc, char * argv[])
{
int sock;
struct sockaddr_in sin;
unsigned short local_port;
unsigned char protocol;
char * buffer, * buffer2, * dnsdata;
struct iphdr * ip_header;
struct icmphdr * icmp_header;
char * remote_ip_str;
unsigned short buffer_size, buffer_size2;
int tmp, len;
short randomseq;
//判断参数是否符合程序运行要求
if (argc != 3) {
fprintf(stderr, "USAGE: %s port destinationn", argv[0]);
return 1;
}
protocol = IPPROTO_ICMP;
local_port = atoi(argv[1]);
remote_ip_str = argv[2];
//创建通信所需套接字,注意套接字类型为原始套接字
if ((sock = socket(PF_INET, SOCK_RAW, protocol)) version = 4;
ip_header->tos = 0;
ip_header->tot_len = htons(buffer_size);
ip_header->id = rand();
ip_header->ttl = 64;
ip_header->frag_off = 0x40;
ip_header->protocol = protocol;
ip_header->check = 0;
ip_header->daddr = inet_addr(remote_ip_str);
ip_header->saddr = 0;
icmp_header = (struct icmphdr *) (ip_header + 1);
icmp_header->type = ICMP_ECHO;
icmp_header->code = 0;
icmp_header->un.echo.id = htons(local_port);
icmp_header->un.echo.sequence = 0;
struct timeval *tp = (struct timeval *)&buffer[28];
gettimeofday(tp, NULL);
icmp_header->checksum = cal_cksum((const u_short *) icmp_header, sizeof(struct icmphdr) + sizeof(struct timeval), 0);
//发送该IP数据包
if (sendto(sock, buffer, buffer_size, 0, (struct sockaddr *) &sin,
sizeof(struct sockaddr_in))