#include <arpa/inet.h> #include <netdb.h> #include <netinet/in.h> // for sockaddr_in #include <sys/types.h> // for socket #include <sys/socket.h> // for socket #include <stdio.h> // for printf #include <stdlib.h> // for exit #include <string.h> // for bzero #define LISTEN_QUEUE 20 #define SERVICE_PORT 1234 int service_sockfd; int ret; int sc; int ret_cv; static char buf[]=""; FILE *stream; int main(int argc,char *argv[]) { struct sockaddr_in service_addr; service_addr.sin_family =AF_INET; service_addr.sin_port =htons(SERVICE_PORT); service_addr.sin_addr.s_addr =htonl(INADDR_ANY); //bzero(&(service_addr),sizeof(service_addr)); service_sockfd =socket(AF_INET,SOCK_STREAM,0); if(-1 == service_sockfd) { perror("sosck failed\n"); exit(-1); } if(bind(service_sockfd,(struct sockaddr*)&service_addr,sizeof(service_addr))==-1) { printf("bind failed\n"); exit(-1); } if( -1 ==listen(service_sockfd,10)) { printf("listen: %s\n",hstrerror(h_errno)); exit(-1); } printf("listening...................\n"); while(1) { socklen_t leng = sizeof(service_addr); struct sockaddr_in client_addr; /* sc =accept(service_sockfd,(struct sockaddr*)&client_addr,&(leng)); if(-1 == sc) { printf("accept failed\n"); break; } else { printf("Service IP:%s::Service Port%d\n",inet_ntoa(client_addr.sin_addr),client_addr.sin_port); } */ sc = accept(service_sockfd,(struct sockaddr*)&client_addr,&(leng)); if(sc == -1) { printf("accept failed\n"); break; } else { printf("Service IP:%s::Service Port%d\n",inet_ntoa(client_addr.sin_addr),client_addr.sin_port); } printf("sc =[%d]\n",sc); strcpy(buf,"hello 客户端!!"); if((ret =send(sc,buf,strlen(buf)+1,0))==-1) { perror("send failed\n"); } printf("ret =[%d]\n",ret); printf("the service sending!!!\n"); ret_cv =recv(sc,buf,1024,0); if(-1 ==ret_cv) { perror("recv failed\n"); break; } printf("service ::recv: - %s\n",buf); /* if(fopen("file1","w+")==NULL) { printf("the file open error\n"); break; } else { printf("the file open successful\n"); } int lengsize = 0; printf("lengsize = %d\n",lengsize); if(recv(sc,buf,1024,0)<0) { printf("Send File is Failed\n"); break; } printf("Service recv:: %s\n",buf); //} if(fclose(stream)) { printf("The file 'data' was not closed! \n"); exit(1); } */ close(sc); } close(service_sockfd); return 0; }
问怎么没把错误贴出来呢?<imgsrc="http://www.oschina.net/js/ke/plugins/emoticons/images/10.gif"alt=""/><imgsrc="http://static.oschina.net/uploads/space/2014/0915/180807_oI7V_1581898.png"alt=""/>
static<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:15.397727012634277px;background-color:#FFFFFF;">char<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:15.397727012634277px;background-color:#FFFFFF;">buf[]="";
这行有问题,估计你接受数据的时候,把一些变量给覆盖了。
charbuf[]="";
这行,buf的长度为1,接收的时候就会内存越界,就出错。
你要么用charbuf[1024]="";或者用malloc动态分配内存。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。