段错误 linux 请看一下怎么改
//client#include"apue.h"#include<netdb.h>#include<errno.h>#include<sys/socket.h>#defi...
//client
#include"apue.h"
#include<netdb.h>
#include<errno.h>
#include<sys/socket.h>
#define MAXADDRLEN 256
#define BUFLEN 128
extern int connect_retry(int , const struct sockaddr * , socklen_t);
#define MAXSLEEP 128
int connect_retry(int sockfd,const struct sockaddr *addr,socklen_t alen)
{
int nsec;
for(nsec=1;nsec<=MAXSLEEP;nsec<<=1)
{
if (connect(sockfd,addr,alen)==0) return(0);
else if (nsec<=MAXSLEEP/2) sleep(nsec);
}
return(-1)
}
void print_uptime(int sockfd)
{
int n;
char buf[BUFLEN];
while((n=recv(sockfd,buf,BUFLEN,0))>0) write(STDOUT_FILENO,buf,n);
if (n<0) printf("recv error");
}
int main(int argc ,char *argv[])
{
struct addrinfo *ailist ,*aip;
struct addrinfo hint;
int sockfd,err;
if (argc !=2) printf("usage:ruptime hostname");
hint.ai_flags =0;
hint.ai_family=0;
hint.ai_socktype=SOCK_STREAM;
hint.ai_protocol=0;
hint.ai_addrlen=0;
hint.ai_canonname =NULL;
hint.ai_addr=NULL;
hint.ai_next=NULL;
if ((err=getaddrinfo(argv[1],"ruptime",&hint,&ailist))!=0)
printf("getaddrinfo error:%s",gai_strerror(err));
for(aip = ailist;aip!=NULL;aip=aip->ai_next)
{
if ((sockfd=socket(aip->ai_family,SOCK_STREAM,0))<0)
err=errno;
if (connect_retry(sockfd,aip->ai_addr,aip->ai_addrlen)<0)
{
err=errno;
}
else
{
print_uptime(sockfd);
exit(0);
}
}
fprintf(stderr,"can't connect to %s:%s\n",argv[1],strerror(err));
exit(1);
}
我用了gdb结果是这样:
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/nannanwu/network/remote-uptime/a.out
Program received signal SIGSEGV, Segmentation fault.
0x08048b58 in main (argc=1, argv=0xbf91f234) at uptime.c:60
60 if ((sockfd=socket(aip->ai_family,SOCK_STREAM,0))<0)
(gdb) q
请高手指教,我该怎么改 展开
#include"apue.h"
#include<netdb.h>
#include<errno.h>
#include<sys/socket.h>
#define MAXADDRLEN 256
#define BUFLEN 128
extern int connect_retry(int , const struct sockaddr * , socklen_t);
#define MAXSLEEP 128
int connect_retry(int sockfd,const struct sockaddr *addr,socklen_t alen)
{
int nsec;
for(nsec=1;nsec<=MAXSLEEP;nsec<<=1)
{
if (connect(sockfd,addr,alen)==0) return(0);
else if (nsec<=MAXSLEEP/2) sleep(nsec);
}
return(-1)
}
void print_uptime(int sockfd)
{
int n;
char buf[BUFLEN];
while((n=recv(sockfd,buf,BUFLEN,0))>0) write(STDOUT_FILENO,buf,n);
if (n<0) printf("recv error");
}
int main(int argc ,char *argv[])
{
struct addrinfo *ailist ,*aip;
struct addrinfo hint;
int sockfd,err;
if (argc !=2) printf("usage:ruptime hostname");
hint.ai_flags =0;
hint.ai_family=0;
hint.ai_socktype=SOCK_STREAM;
hint.ai_protocol=0;
hint.ai_addrlen=0;
hint.ai_canonname =NULL;
hint.ai_addr=NULL;
hint.ai_next=NULL;
if ((err=getaddrinfo(argv[1],"ruptime",&hint,&ailist))!=0)
printf("getaddrinfo error:%s",gai_strerror(err));
for(aip = ailist;aip!=NULL;aip=aip->ai_next)
{
if ((sockfd=socket(aip->ai_family,SOCK_STREAM,0))<0)
err=errno;
if (connect_retry(sockfd,aip->ai_addr,aip->ai_addrlen)<0)
{
err=errno;
}
else
{
print_uptime(sockfd);
exit(0);
}
}
fprintf(stderr,"can't connect to %s:%s\n",argv[1],strerror(err));
exit(1);
}
我用了gdb结果是这样:
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/nannanwu/network/remote-uptime/a.out
Program received signal SIGSEGV, Segmentation fault.
0x08048b58 in main (argc=1, argv=0xbf91f234) at uptime.c:60
60 if ((sockfd=socket(aip->ai_family,SOCK_STREAM,0))<0)
(gdb) q
请高手指教,我该怎么改 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询