socket program:why server cann't receive
I wrote a simple socket program in C, but met an question:my server program cann't receive data from the client program, though the client program can receive data from the server program. Why? I went back to many books and many webpages of the Internet, but cann't find the reason.
Who can tell me the reason,thanks:) Below is the program. |
/******* server.c ************/
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber, recv_len; char buffer[1024]; if(argc!=2) { fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); exit(1); } if((portnumber=atoi(argv[1]))<0) { fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); exit(1); } /* build socke */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); exit(1); } /* fill sockaddr struct */ bzero(&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(portnumber); /* bind sockfd */ if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))== -1) { fprintf(stderr,"Bind error:%s\n\a",strerror(errno)); exit(1); } /*listen socket */ if(listen(sockfd,5)==-1) { fprintf(stderr,"Listen error:%s\n\a",strerror(errno)); exit(1); } while(1) { /*wait for connect*/ sin_size=sizeof(struct sockaddr_in); if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),(socklen_t *)&sin_size ))==-1) { fprintf(stderr,"Accept error:%s\n\a",strerror(errno)); exit(1); } fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr)); recv_len = -1; while(recv_len == -1) { recv_len=recv(sockfd,buffer,1024, 0); if(recv_len != -1) { buffer[recv_len]='\0'; printf("received:%s\n",buffer); } } /* the communication is end */ shutdown(new_fd, SHUT_RD); /* for the next */ } shutdown(sockfd, SHUT_RD); exit(0); } |
Can you use the code tag ? Indentation would ease reading your program.
|
/******* client.c ************/
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> int main(int argc, char *argv[]) { int sockfd; char hello[]="I'm ljqu_happy.\n"; char buffer[1024]; struct sockaddr_in server_addr; struct hostent *host; int portnumber,nbytes, recv_len; if(argc!=3) { fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]); exit(1); } if((host=gethostbyname(argv[1]))==NULL) { fprintf(stderr,"Gethostname error\n"); exit(1); } if((portnumber=atoi(argv[2]))<0) { fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]); exit(1); } /* build socket */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } /* fill socket */ bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(portnumber); server_addr.sin_addr=*((struct in_addr *)host->h_addr); /* connect to server */ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr) )==-1) { fprintf(stderr,"Connect Error:%s\a\n",strerror(errno)); exit(1); } /* connect successful */ if((send(sockfd, hello, sizeof(hello), 0))==-1) { fprintf(stderr,"send name Error:%s\n",strerror(errno)); exit(1); } printf("send data %s OK!\n", hello); /*communication is end */ shutdown(sockfd, SHUT_RD); exit(0); } |
No improvement in your code readability, but anyway you use the wrong file descriptor in your recv call, it should be new_fd.
|
why this forum doesn't have file attachment function?
Is there special reason about it? -- |
Jlliagre is wellcome:) You gave the right answer.Thank you very much.But I'm very sorry for asking such a simple question,I thought things too complicated, and didn't just sit down to look carefully at my code.I should remember this lesson.
BTW, I have indented the code, but the web don't support the space at the head of the line, there only left space line. |
As I asked you before, put your code between code tags, like this
Code:
this is: |
All times are GMT -5. The time now is 06:31 AM. |