当前位置: 技术问答>linux和unix
段错误
来源: 互联网 发布时间:2016-03-15
本文导语: 编译执行Linux下客户端程序,给出“段错误”。不知道该怎么办。请大家赐教: /* client.c */ #include #include #include #include #include #include #include #include #include #include #include //struct sockaddr_in{ //uint8_t sin_len...
编译执行Linux下客户端程序,给出“段错误”。不知道该怎么办。请大家赐教:
/* client.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//struct sockaddr_in{
//uint8_t sin_len; /* 无符号的8位整数 */
//sa_family_t sin_family; /* 套接口地址结构的地址簇,这里为AF_INET */
//in_port_t sin_port; /* TCP或UDP端口 */
//struct in_addr sin_addr;
//char sin_zero[8];
//}
int main(int argc,char *argv[]) {
int sockfd,numbytes;
char buf[100];
struct hostent *he;
struct sockaddr_in their_addr;
//int i = 0;
//将基本名字和地址转换
he = gethostbyname(argv[1]);
//建立一个TCP套接口
//int socket(int family,int type,int protocol);
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("socket");
exit(1);
}
//初始化结构体,连接到服务器的10001端口
their_addr.sin_family = AF_INET; /* 套接口地址结构的地址簇,这里为AF_INET */
their_addr.sin_port = htons(10001); /* TCP端口 */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero),8);
//和服务器建立连接
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1){
perror("connect");
exit(1);
}
//向服务器发送字符串"hello!"
if(send(sockfd,"hello!",6,0)==-1) {
perror("send");
exit(1);
}
//接受从服务器返回的信息
if((numbytes = recv(sockfd,buf,100,0))==-1) {
perror("recv");
exit(1);
}
buf[numbytes] = ' ';
printf("result:%s",buf);
close(sockfd);
return 0;
}
/* client.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//struct sockaddr_in{
//uint8_t sin_len; /* 无符号的8位整数 */
//sa_family_t sin_family; /* 套接口地址结构的地址簇,这里为AF_INET */
//in_port_t sin_port; /* TCP或UDP端口 */
//struct in_addr sin_addr;
//char sin_zero[8];
//}
int main(int argc,char *argv[]) {
int sockfd,numbytes;
char buf[100];
struct hostent *he;
struct sockaddr_in their_addr;
//int i = 0;
//将基本名字和地址转换
he = gethostbyname(argv[1]);
//建立一个TCP套接口
//int socket(int family,int type,int protocol);
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("socket");
exit(1);
}
//初始化结构体,连接到服务器的10001端口
their_addr.sin_family = AF_INET; /* 套接口地址结构的地址簇,这里为AF_INET */
their_addr.sin_port = htons(10001); /* TCP端口 */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero),8);
//和服务器建立连接
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1){
perror("connect");
exit(1);
}
//向服务器发送字符串"hello!"
if(send(sockfd,"hello!",6,0)==-1) {
perror("send");
exit(1);
}
//接受从服务器返回的信息
if((numbytes = recv(sockfd,buf,100,0))==-1) {
perror("recv");
exit(1);
}
buf[numbytes] = ' ';
printf("result:%s",buf);
close(sockfd);
return 0;
}
|
因为这句 he = gethostbyname(argv[1]);
你应该
if(argc > 1) {
he = gethostbyname(argv[1]);
} else {
printf("Invalid argvn");
return -1;
}
你应该
if(argc > 1) {
he = gethostbyname(argv[1]);
} else {
printf("Invalid argvn");
return -1;
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。