客户端服务器编程 - 缓冲区写

时间:2014-01-31 07:42:25

标签: c++ oop

我正在尝试用c ++开发客户端服务器程序,其中客户端是TCP echo客户端,而服务器是使用单个进程的TCP并发服务器(使用select系统调用)。但是我成功地开发它但是写缓冲区的问题。

服务器在客户端和服务器之间进行了一些消息交换之后,从之前的消息中写入了一些额外的字符,在启动它时可以正常处理某些消息。 我不知道为什么会这样?

//客户端代码

#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 srv_PORT 3000 /*port*/

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


if (argc !=2)
 {
  cerr<<"Usage: Femto: <IP address of the srv"<<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(&srvaddr, 0, sizeof(srvaddr));
 srvaddr.sin_family = AF_INET;
 srvaddr.sin_addr.s_addr= inet_addr(argv[1]);
 srvaddr.sin_port =  htons(srv_PORT); 

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

while (fgets(buf,sizeof(buf), stdin))
 {
  int n;
  buf[MAXLINE]='\0';
  sendchars=strlen(buf);
  write(sockfd,buf,sendchars);

  for(recvchar=0;recvchar<sendchars;recvchar+=n)  
   { 
    n=read(sockfd,&buf[recvchar],sendchars-recvchar);
    if(n<0)
     {
       cerr<<"Read faild"<<endl;
        }
   cout<< "String received from the FGW: ";
   fputs(buf, stdout);
 }
}
}

//服务器代码

#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 srv_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, srvaddr;

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

 srvaddr.sin_family = AF_INET; 
 srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 srvaddr.sin_port = htons(srv_PORT);

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

 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);
 }

 for(fd=0;fd<nfds;++fd)
  if(fd!=msock && FD_ISSET (fd,&rfds))
  {
  int cc;
  char buf[MAXLINE];
  cc=read(fd,buf,sizeof(buf));
  cout<<"String received from and resent to the client:"<<endl;
       puts(buf);
  if(cc<0)
  {
  cerr<<"Read error"<<endl;
  exit(1);
  }
  if(cc && write(fd,buf,cc)<0)
  {
  cerr<<"Write error"<<endl;
  exit(1);
  }  
  }
 close(fd);
 FD_CLR(fd,&afds);
  }
 }

1 个答案:

答案 0 :(得分:0)

 buf[MAXLINE]='\0';

超出范围。这可能会导致任何错误。 你可以声明

 char buf[MAXLINE+1]