- 论坛徽章:
- 1
|
小弟最近写了一个tcp通信的小程序,希望在客户端输入一段字符串给服务器,server收到后,再会送给client
但是当我在client端,第一次输入一段字符串后,服务器端能够响应,并且将字串回送给client,但是当第一次通信完成后,我在客户端再次输入一段字串后,server端就没有反应了,当第三次输入后,client端直接就推出去了。请那位高手帮忙看一下
下面贴上我的代码,请大神指点一下- /******* 服务器程序 (server.c) ************/
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.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,readbyte;
- char readbuf[1000];
- /*
- char hello[]="Hello! Are You Fine?\n";
- 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);
- }
- /* 服务器端开始建立socket描述符 */
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
- exit(1);
- }
- /* 服务器端填充 sockaddr结构 */
- 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);
- /* 捆绑sockfd描述符 */
- if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
- {
- fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
- exit(1);
- }
- /* 监听sockfd描述符 */
- if(listen(sockfd,5)==-1)
- {
- fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
- exit(1);
- }
- /* 服务器阻塞,直到客户程序建立连接 */
- sin_size=sizeof(struct sockaddr_in);
- while(1)
- {
- if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&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));
-
- if( (readbyte = read(new_fd,readbuf,1024)) == -1 )
- {
- fprintf(stderr,"Read Error:%s\n",strerror(errno));
- exit(1);
-
- }
- else
- {
- readbuf[readbyte] = '\0';
- fprintf(stderr,"server read: %s\n",readbuf);
- printf("read sucess!\n");
- }
- //printf("read sucess!\n");
-
- if((readbyte = write(new_fd, readbuf, strlen(readbuf))) == -1)
- {
- fprintf(stderr,"Read Error:%s\n",strerror(errno));
- exit(1);
- }
- else
- printf("write sucess! %d chars have send.",readbyte);
-
- close(new_fd);
- }
- exit(0);
- }
复制代码- /******* 客户端程序 client.c ************/
- //参数:ip 端口
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <arpa/inet.h>
- int main(int argc, char *argv[])
- {
- int sockfd;
- char buffer_write[200];
- char buffer_read[200];
- struct sockaddr_in server_addr;
- struct hostent *host;
- int portnumber,nbytes;
- char *ip_ptr;
- 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);
- }
- ip_ptr = argv[1];
- /* 客户程序开始建立 sockfd描述符 */
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
- exit(1);
- }
- /* 客户程序填充服务端的资料 */
- 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);
- server_addr.sin_addr.s_addr = inet_addr(ip_ptr);
- /* 客户程序发起连接请求 */
-
- if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
- {
- fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
- exit(1);
- }
- /* 连接成功了 */
- while(1)
- {
- scanf("%s",buffer_write);
- printf("client write: %s\n",buffer_write);
- if((nbytes = write(sockfd, buffer_write, strlen(buffer_write))) == -1)
- {
- fprintf(stderr,"write Error:%s\n",strerror(errno));
- exit(1);
- }
- printf("client write success, %d chars have send\n",nbytes);
-
- //memset(buffer_write,0,sizeof(buffer_write));
-
- if((nbytes=read(sockfd,buffer_read,1024))==-1)
- {
- fprintf(stderr,"Read Error:%s\n",strerror(errno));
- exit(1);
- }
- buffer_read[nbytes]='\0';
- printf("client have received:" "%d""char!\n",nbytes);
- printf("client have received:%s\n\n",buffer_read);
- //close(sockfd);
- }
- //exit(0);
- }
复制代码 |
|