你好,想跟你请教个问题 这个:
#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 #include <time.h> //for time_t and time #define LISTEN_QUEUE 20 #define SERVICE_PORT 1234 int service_sockfd; int ret; int sc; static char buf[1024]=""; 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))==0) { 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("%s:%d\n",inet_ntoa(client_addr.sin_addr),client_addr.sin_port); } bzero(buf,1024); strcpy(buf,"hello world 从服务器上来!!"); if(ret =send(sc,buf,strlen(buf)+1,0)==-1) { perror("send failed\n"); exit(-1); } printf("the service sending!!!\n"); ret =recv(sc,buf,1024,0); if(-1 ==ret) { perror("recv failed\n"); exit(-1); } printf("recv:[%d] - %s\n",ret,buf); if(fopen("file1","w+")==NULL) { printf("the file open error\n"); exit(-1); } else { printf("the file open successful\n"); } printf("Send %s\n",buf); int lengsize = 0; if(lengsize =fread(buf,1,1024,stream)>0) { printf("lengsize = %d\n",lengsize); if(send(sc,buf,lengsize,0)<0) { printf("Send File is Failed\n"); break; } bzero(buf, 1024); } if(fclose(stream)) { printf("The file 'data' was not closed! \n"); exit(1); } close(sc); } close(service_sockfd); return 0; }
客户端:
#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 #include <time.h> //for time_t and time #define CLIENT_PORT 1234 #define MAX 1024 char *IP="127.0.0.1"; int client_socket; int ret; int my; char buf[MAX]="hello 服务器"; FILE *stream; int main() { client_socket =socket(AF_INET,SOCK_STREAM,0); if(-1 == client_socket) { perror("sosck failed\n"); exit(-1); } struct sockaddr_in client_addr; client_addr.sin_family =AF_INET; client_addr.sin_port =htons(CLIENT_PORT); client_addr.sin_addr.s_addr = inet_addr(IP); ret =connect(client_socket, (struct sockaddr *)&client_addr, sizeof(struct sockaddr_in)); if(-1 == ret) { printf("connetct failed\n"); exit(-1); } while(1) { strcat(buf,"发送请求"); if(-1 == send(client_socket,buf,sizeof(buf)+1,0)) { perror("send failed\n"); exit(-1); } ret =recv(client_socket,buf,1024,0); if(-1 ==ret) { perror("recv failed\n"); exit(-1); } if(fopen("file2","w+") == NULL) { printf("the file open error\n"); exit(-1); } else { printf("the file open successful\n"); } printf("Send %s\n",buf); int lengsize = 0; if(lengsize=fwrite(buf,1,1024,stream) == -1) { printf("lengsize = %d\n",lengsize); if(send(client_socket,buf,lengsize,0)<0) { printf("Send File is Failed\n"); break; } } } bzero(buf, 1024); if(fclose(stream)) { printf("The file 'data' was not closed! \n"); exit(1); } close(client_socket); return 0; }
哈,没细看,我把我这边server和client端的代码贴给你,你参考一下,看是不是有什么步骤遗漏了。。。
_Isetup_server(_shostname,_Iport,_SERVER_FUNCprf,_SERVER_FUNCpwf,_Inum){_SOCKETIDsockfd,new_fd;_Ire=0;_Ii;structsockaddr_inclient_addr;socklen_taddr_size=sizeof(structsockaddr_in);_i32chk;structsockaddr_inservaddr;structtimevaltv;fd_setfdsr;_i32lastminute,maxsock,flag;signal(SIGPIPE,sig_send_err);ms_serverid=0;sockfd=socket(AF_INET,SOCK_STREAM,ms_potol);_error(sockfd==-1,_setup_server_ERR,"createsocketerror:%s(errno:%d)\n",strerror(errno),errno);chk=setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&ms_socket_reuse_opt,sizeof(ms_socket_reuse_opt));_error(chk==-1,_setup_server_close_ERR,"setsockoptSO_REUSEADDRerror:%s(errno:%d)\n",strerror(errno),errno);memset(&servaddr,0,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(port);chk=inet_pton(AF_INET,hostname,&servaddr.sin_addr);_error(chk<=0,_setup_server_close_ERR,"inet_ptonerrorfor%s",hostname);chk=bind(sockfd,(structsockaddr*)&servaddr,sizeof(servaddr));_error(chk==-1,_setup_server_close_ERR,"bindsocketerror:%s(errno:%d)",strerror(errno),errno);chk=listen(sockfd,ms_backlog);_error(chk==-1,_setup_server_close_ERR,"listensocketerror:%s(errno:%d)",strerror(errno),errno);lastminute=get_current_min();maxsock=sockfd;_log("maxsock%d",maxsock);printf("======waitingforclient'srequest======\n");ms_conn_num=0;其他的类型定义你不用去理解,都是我自己的类型定义,你跟踪两个变量就ok了。_shostname和_Iport;“_s"是我自己定义字符串类型等于char*,_I可以等于unsignedint类型,上面两端代码是我测试过的。。你对应一下。
另外我可不大神。哈。
你的server代码有明显的错误啊。
第35行修改为==-1不是==0:
if(bind(service_sockfd,(structsockaddr*)&service_addr,sizeof(service_addr))==-1)把第26行去掉:
bzero(&(service_addr),sizeof(service_addr));我测试了一下server,在1234端口已经监听成功了,后面的下载你再继续测试看看~
第26行为什么去掉,你先要想一下这第26行为什么加呢?
bzero函数的用处:Thebzero()functionsetsthefirstnbytesofthebyteareastartingatstozero(bytescontaining’\0’).
你前面对server_addr的各种赋值,一个bzero把它全搞没了,后面还怎么玩?
所以这一行要么去掉,要么放在service_addr定义之后属性赋值之前,要么直接修改为:
bzero(&(service_addr.sin_zero),8);版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。