|
|
网卡的静态地址为: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);
} |
|