LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 623|回复: 2

求助:调试服务器程序时bind出错

[复制链接]
发表于 2006-3-19 18:50:35 | 显示全部楼层 |阅读模式
网卡的静态地址为:210.40.41.1
掩码:255.255.255.0
主机ip:210.40.41.1

当我把网卡和主机的ip都改为127.0.0.1后调试正确???
不知道为什么,请诸位大虾指教.

调试信息:
Starting program: /home/wangne/works
socket success...
socket set success...
port=20480
receiver:error in bind...
,errno=13
error type: Permission denied

Program exited with code 01.

原程序:
#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <string.h>

#include <unistd.h>

#include <stdio.h>

#include <errno.h>

#include <netdb.h>

#include <arpa/inet.h>



#define port 80

#define addr_ip INADDR_ANY

#define backlog 20

#define name "lineminwangne.com"



extern int errno;



/*error*/

report_error(char *s)

{

  printf("receiver:error in %s,errno=%d\n",s,errno);

  perror("error type");

  exit(1);

}



/*send*/

send_s(char *s,int newfd)

{

int len,bytes_sent;

len=strlen(s);

bytes_sent=send(newfd,s,len,0);

if(bytes_sent==-1)

   report_error("send");

printf("send success...\n");

}



main()

{

pid_t pid;               

int sockfd,newfd;

struct sockaddr_in my_addr,their_addr;

struct hostent *hp;

int sin_size;

volatile int true=1;



/*socket*/

if((sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)

   report_error("socket");

printf("socket success...\n");

/*socket set*/

if((setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(void *)&true,sizeof(true)))==-1)

   report_error("socket set");

printf("socket set success...\n");



/*bind*/

my_addr.sin_family=AF_INET;

#ifdef port

my_addr.sin_port=htons(port);

#else

my_addr.sin_port=htons(IPPORT_USERRESERVED);

#endif      

my_addr.sin_addr.s_addr=htonl(addr_ip);

  printf("port=%d\n",my_addr.sin_port);

bzero(&(my_addr.sin_zero),8);

if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(my_addr))==-1)

   report_error("bind...\n");

printf("bind success...\n");



/*listen*/

if(listen(sockfd,backlog)==-1)

   report_error("listen");

printf("listen success...\n");



while(1)

{

  sin_size=sizeof(struct sockaddr_in);

  printf("server is run");

/*new cling*/

  newfd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);

  if(newfd<0&&errno==EINTR)

    continue;

/*a signal might interrupt our accept() call*/

  else if(newfd<0)

/*something quite amiss -- kill the server*/

    report_error("accept");

  printf("accept success...\n");

/*fork*/

  if ((pid=fork())<0)

    report_error("fork");

  printf("fork success...\n");

/*child*/

  if (pid==0)

   {

    close(sockfd);                    /*close listen socket*/

    send_s("Content_TYPE:text/html",newfd);

    send_s(".",newfd);

    send_s("<HTML><HEAD><TITLE>Test Page </TITLE></HEAD>",newfd);

    send_s("<BODY>hollo everybody</BODY></HTML>",newfd);

    close(newfd);                    /*close client socket*/

    exit(0);

   }

/*father*/

  else close(newfd);                /*father close client socket*/

}

close(sockfd);

exit(0);

}
发表于 2006-3-19 22:00:18 | 显示全部楼层
permission denied?你用什么用户执行程序的?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-20 10:30:52 | 显示全部楼层
楼上的大大,实在太感谢你了,我今天用root用户一调试~通了,眼泪都流出来了,为了这个我搞了3天啊555555555555谢谢谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表