当前位置: 技术问答>linux和unix
LINUX-SOCK编程基础问题?望斑竹解答1
来源: 互联网 发布时间:2015-02-15
本文导语: /*客户端代码*/ #include #include #include #include #include #include #include #include #include #include #include #define MYPORT 1234 #define BACKLOG 5 int main(int argc,char *argv[]){ int sockfd,nbytes; char buf[1024]; ...
/*客户端代码*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 1234
#define BACKLOG 5
int main(int argc,char *argv[]){
int sockfd,nbytes;
char buf[1024];
struct hostent *he;
struct sockaddr_in srvaddr;
if (argc !=2 ){
perror("host home n");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1){
perror("SOCKET CREATE ERROR");
exit(1);
}
bzero(&srvaddr,sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port =htons(MYPORT) ;
srvaddr.sin_addr = *((struct in_addr *)he->h_addr);
if (connect(sockfd,(struct sockaddr *)&srvaddr,sizeof (struct sockaddr)) == -1) {
perror("connect error");
exit(1);
}
if (nbytes = read(sockfd,buf,1024) == -1){
perror("READ error");
exit(1);
}
buf[nbytes] = '';
printf("read:%s",buf);
close(sockfd);
}
/*服务器端代码*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 1234
#define BACKLOG 5
int main(){
int sockfd,new_fd;
struct sockaddr_in srvaddr;
struct sockaddr_in cliaddr;
int sin_size;
if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1){
perror("SOCKET CREATE ERROR");
exit(1);
}
bzero(&srvaddr,sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port =htons(MYPORT) ;
if (bind(sockfd,(struct sockaddr *)&srvaddr,sizeof (struct sockaddr)) == -1) {
perror("bind error");
exit(1);
}
if (listen(sockfd ,BACKLOG) == -1){
perror("listen error");
exit(1);
}
for(;;){
sin_size = sizeof(struct sockaddr_in);
if (new_fd= accept(sockfd,(struct sockaddr *)&cliaddr,&sin_size) == -1 ){
perror("ACCPET error");
continue;
}
printf("server :get connect from %s n",inet_ntoa(cliaddr.sin_addr));
if (write(new_fd , "hello, network!n",14) == -1)
perror("WRITE error");
close(new_fd);
}
close(sockfd);
}
以上代码在RED HAT8上编译通过!
运行SERVER没有问题!
但是运行CLIENT localhost
出现 Segmenttation fault
问如何解决?
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 1234
#define BACKLOG 5
int main(int argc,char *argv[]){
int sockfd,nbytes;
char buf[1024];
struct hostent *he;
struct sockaddr_in srvaddr;
if (argc !=2 ){
perror("host home n");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1){
perror("SOCKET CREATE ERROR");
exit(1);
}
bzero(&srvaddr,sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port =htons(MYPORT) ;
srvaddr.sin_addr = *((struct in_addr *)he->h_addr);
if (connect(sockfd,(struct sockaddr *)&srvaddr,sizeof (struct sockaddr)) == -1) {
perror("connect error");
exit(1);
}
if (nbytes = read(sockfd,buf,1024) == -1){
perror("READ error");
exit(1);
}
buf[nbytes] = '';
printf("read:%s",buf);
close(sockfd);
}
/*服务器端代码*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 1234
#define BACKLOG 5
int main(){
int sockfd,new_fd;
struct sockaddr_in srvaddr;
struct sockaddr_in cliaddr;
int sin_size;
if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1){
perror("SOCKET CREATE ERROR");
exit(1);
}
bzero(&srvaddr,sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port =htons(MYPORT) ;
if (bind(sockfd,(struct sockaddr *)&srvaddr,sizeof (struct sockaddr)) == -1) {
perror("bind error");
exit(1);
}
if (listen(sockfd ,BACKLOG) == -1){
perror("listen error");
exit(1);
}
for(;;){
sin_size = sizeof(struct sockaddr_in);
if (new_fd= accept(sockfd,(struct sockaddr *)&cliaddr,&sin_size) == -1 ){
perror("ACCPET error");
continue;
}
printf("server :get connect from %s n",inet_ntoa(cliaddr.sin_addr));
if (write(new_fd , "hello, network!n",14) == -1)
perror("WRITE error");
close(new_fd);
}
close(sockfd);
}
以上代码在RED HAT8上编译通过!
运行SERVER没有问题!
但是运行CLIENT localhost
出现 Segmenttation fault
问如何解决?
|
he没有获得实际值(gethostbyname()?)就使用了
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。