|
请问我这段网络时间服务程序有什么问题?编译通过了,但是在redhat下运行总是提示“段出错”。谢谢
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#define SA struct sockaddr
#define MAXLINE 4096
/* #include "error.c" */
int
main(int argc,char** argv)
{
int sockfd,n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;
if(argc!=2){
printf("%s<IP adress>\n",argv[0]);
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
printf("socket error");
exit(1);
}
memset(&servaddr,0,sizeof(struct sockaddr_in));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13); /* daytime server */
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0){
printf("inet_pton error for %s",argv[1]);
exit(0);
}
if(connect(sockfd,(SA*)(&servaddr),sizeof(struct sockaddr_in))<0){
printf("connection failure");
exit(1);
}
while((n=read(sockfd,recvline,MAXLINE))>0){
recvline[n]=0; /* null terminate */
if(fputs(recvline,stdout)==EOF){
printf("fputs error");
exit(1);
}
}
if(n<0){
printf("read error");
exit(1);
}
exit(0);
} |
|