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

client server programming buffer content not correct

本文关键字:不正确 缓冲区 服务器 编程 客户端      更新时间:2023-10-16

i键入客户端的两个程序一个,一个用于服务器。服务器IS TCP并发echo 服务器带有SELECT CALL,以便向所有客户端使用一个进程。它使用明显的并发。

我开发程序并运行其工作,但是在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?

您犯了通常的错误,即忽略recv()返回的计数。缓冲区中的数据仅有效到该计数。其余的与以前的值保持不变。

您还忽略了bind(),listing(),send()和recv()的可能性。