客户端服务器编程缓冲内容不正确

时间:2014-01-31 10:50:35

标签: c++ unix networking client-server

我为客户端键入两个程序,为服务器键入一个。 服务器是带有select调用的 tcp并发echo 服务器,以便只对所有客户端使用一个进程。 它使用明显的并发性。

我开发程序并运行它的工作但是在3/4消息交换下注客户端和服务器之后。 服务器上的缓冲区内容更改它显示当前消息,其中包含上一条消息中的某些字符。

我不明白为什么会这样。 请有人能帮助我......

//Client Program

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;

#define MAXLINE 4096 /*max text line length*/
#define serv_PORT 3000 /*port*/

int main(int argc,char **argv)
{
 int sockfd;
 struct sockaddr_in servaddr;
 char sendline[MAXLINE]; 
 char recvline[MAXLINE];
 /*int sendchars,recvchar;
 char buf[MAXLINE];
*/

if (argc !=2)
 {
  cerr<<"Usage: Femto: <IP address of the serv"<<endl;
  exit(1);
 }

//Create a socket for the client

if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0)
 {
  cerr<<"Problem in creating the socket"<<endl;
  exit(1);
 }

//Creation of the socket

 memset(&servaddr, 0, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr= inet_addr(argv[1]);
 servaddr.sin_port =  htons(serv_PORT); 

//Connection of the client to the socket
 if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0)
 {
  cerr<<"Problem in connecting to the serv"<<endl;
  exit(1);
 }

while (fgets(sendline, MAXLINE, stdin) != NULL)
 {
  send(sockfd, sendline, strlen(sendline), 0);
   if (recv(sockfd, recvline, MAXLINE,0) == 0)
  {
    cerr<<"The serv terminated"<<endl;
    exit(1);
  }

   cout<< "String received from the serv: ";
   fputs(recvline, stdout);
 }

 exit(0);
}


//Server program

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sys/select.h>
#include <sys/time.h>


using namespace std;
#define MAXLINE 4096 /*max text line length*/
#define serv_PORT 3000 /*port*/
#define LISTENQ 65535 

int main (int argc, char **argv)
{
 int msock,ssock;
 fd_set rfds;
 fd_set afds;
 int fd,nfds;
 socklen_t client_len ;


 char buf[MAXLINE];
 struct sockaddr_in clientaddr, servaddr;

 if ((msock = socket (AF_INET, SOCK_STREAM, 0)) <0)
 {
 cerr<<"Problem in creating the socket"<<endl;
 exit(1);
   }

 servaddr.sin_family = AF_INET; 
 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 servaddr.sin_port = htons(serv_PORT);

 bind (msock, (struct sockaddr *) &servaddr, sizeof(servaddr));

 listen (msock, LISTENQ);

 nfds=getdtablesize();
 FD_ZERO(&afds);
 FD_SET(msock,&afds);

 while(1)
 {
 memcpy(&rfds,&afds,sizeof(rfds));
 if(select(nfds,&rfds,(fd_set *)0,(fd_set *)0,(struct timeval * )0)<0)
 {
 cerr<<"Error in select";
// exit(1);
 }


 if(FD_ISSET(msock,&rfds))
 {
 //int ssock;
 ssock= accept(msock,(struct sockaddr *)&clientaddr,&client_len);
  if(ssock<0)
  {
   cerr<<"Accept error";
  }
 FD_SET(ssock,&afds);
 }
 int n;
 for(fd=0;fd<nfds;++fd)
  if(fd!=msock && FD_ISSET (fd,&rfds))
    while ( (n = recv(fd, buf, MAXLINE,0)) > 0)  {
    cout<<"String received from and resent to the client:"<<endl;
    puts(buf);
    send(fd, buf, n, 0);
}
 close(fd);
 FD_CLR(fd,&afds);
  }
 }



output::

client-hi
server-hi

client-bye
server-bye

//after some message exchange
client-wru?
server-wru?

client- i m here
server-i am here u?

1 个答案:

答案 0 :(得分:1)

你犯了忽略recv()返回的计数的错误。缓冲区中的数据仅在该计数内有效。其余部分与之前的价值保持不变。

你也忽略了bind(),listen(),send()和recv()中出错的可能性。