C++ 套接字编程中发送的额外查询

C++ 套接字编程中发送的额外查询,c++,c,sockets,C++,C,Sockets,我正在尝试实现一个基本的服务器-客户机套接字程序,其中服务器发送很少的消息,客户机响应这些消息。此处的消息以缓冲区的形式存储。以下是对逻辑的高级解释: 服务器------------------客户端 FF、FD、18---> 客户端: debian:~/sam$ ./single_client 127.0.0.1 67777 Recieving Buffer 3 from Server side FF FD 18 Sent Buffer bytes 3: FF FC 18 from clie

我正在尝试实现一个基本的服务器-客户机套接字程序,其中服务器发送很少的消息,客户机响应这些消息。此处的消息以缓冲区的形式存储。以下是对逻辑的高级解释:

服务器------------------客户端

FF、FD、18--->

客户端:

debian:~/sam$ ./single_client 127.0.0.1 67777
Recieving Buffer 3 from Server side FF FD 18 
Sent Buffer bytes 3: FF FC 18 from client side 

Recieving Buffer 3 from Server side FF FD 1E 
Sent Buffer bytes 3: FF FC 1E from client side 

Recieving Buffer 3 from Server side FF FD 1D 
Sent Buffer bytes 3: FF FC 1D from client side 

Recieving Buffer 3 from Server side FF FD 18 
Sent Buffer bytes 3: FF FC 18 from client side 
我应该如何解决这个问题。提前谢谢

而不是:

 int ctr = sizeof(buff1) ; 
尝试:


这将为您提供“ctr”作为“buff1”中的元素数,其中每个元素都是要在一次迭代中发送的字符数组。目前,您正在获取多维数组中的总字节数。我相信您也会希望将“k”初始化为0,而不是1。

为什么要执行“count=count%4;”如果有3组字符要发送?buff1有4个查询..因此使用计数%4来划分每个查询OK。但是,然后使用%运算符在内部循环中再次发送每个查询?除了发送失败()之外,您打算如何突破内部循环?如果您只想发送4个查询,每个查询一次,是否需要外部循环?我对count部分进行了注释,但它仍在发送一个额外的查询。
#include <stdio.h>
#include <stdlib.h>     
#include <string.h>
#include <string>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <unistd.h>
#include <netdb.h>

int main(int argc, char *argv[])
 {
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server1;

   char buffer1[256];

   if (argc < 3)
    {
        fprintf(stderr,"usage %s hostname port\n", argv[0]);
        return(0);
    }
    portno = atoi(argv[2]);
    /* Create a socket point */

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

   if (sockfd < 0) 
    {
        perror("ERROR opening socket");
        return(1);
    }

    server1 = gethostbyname(argv[1]);
    if (server1 == NULL)
    {
     fprintf(stderr,"ERROR no such host \n");
     exit(0);
    }

    bzero((char *) &serv_addr , sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server1->h_addr, (char*)&serv_addr.sin_addr.s_addr, server1->h_length);
    serv_addr.sin_port = htons( portno );


/*Connect to server*/
 if (connect( sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) ) < 0) 
    {
         perror("ERROR connecting");
         return(1);
    }   
 int l = 1 ;


    bzero(buffer1,256);
int j=1;
int i =0;

while(j <= 4)
{

        n= recv(sockfd,buffer1,3,0);
        if(n<0)
           printf("ERROR reading in socket %d  len %d", n, sizeof(buffer1));


          {          
           printf("Recieving Buffer %d from Server side %hhX %hhX %hhX \n",n, buffer1[0], buffer1[1], buffer1[2]); 
           buffer1[1] = 0xfc;
           n= send(sockfd,buffer1,3,0);
       if(n<0)
           printf("ERROR writing in socket %d  \n", n);        
           printf("Sent Buffer bytes %d: %hhX %hhX %hhX from client side \n\n",n,buffer1[0],buffer1[1],buffer1[2]);
           j++;
           l++;
          }

}
return(0);
}
#include <stdio.h>
#include <stdlib.h>     
#include <string.h>
#include <string>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <unistd.h>

char buff1[][3]= {{0xff , 0xfd, 0x18},{0xff , 0xfd, 0x1e},{0xff , 0xfd, 0x1d},{0xff , 0xfd , 0x18}};

char recbuf[1024];

int main(int argc , char *argv[] )
{
  int sockfd , newsockfd , portno;
  socklen_t clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  int n;

  sockfd = socket(AF_INET , SOCK_STREAM , 0);
  if (sockfd < 0 )
  {
   perror("Error opening socket ");
   exit(1);
  }

  /* Initialize socket structure */
  bzero((char *) &serv_addr , sizeof(serv_addr));
  portno = atoi(argv[1]);
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY ; 
  serv_addr.sin_port = htons(portno);

 /* Now bind the host address using client */
  if(bind(sockfd, (struct sockaddr *) &serv_addr , sizeof(serv_addr)) <0)

  { 
   perror("Error on binding");
   exit(1);
  } 
    if(listen(sockfd,5)<0)
     {
       perror("Error on listen");
       exit(1);
     }
        int k = 1;
       int count = 0;
        clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

    if (newsockfd < 0)
    {
     perror("ERROR on accept");
     exit(1);
    }
         int ctr = sizeof(buff1) ; 

     printf("Server Sending (DO TERMINAL TYPE)\n");
         while(k < ctr)
         {// int i = 0;

         while ((n = send(newsockfd,&buff1[count][0],3,0))> 0)
         { 
                  if(n < 0)
               {
                    close(newsockfd);
                    exit(1);
                   }
                  else if (n > 0)
                  {
           printf("Server Sent query %d to Client:%hhX %hhX %hhX\n", count , buff1[count][0], buff1[count][1], buff1[count][2]);
           n = recv(newsockfd, recbuf , sizeof(recbuf),0);
                   printf("n = %d\n",n);
                   printf("Server received response from Client: %hhX %hhX %hhX\n\n", recbuf[0], recbuf[1], recbuf[2]);

                  }
        }

          k++;
         }


      close(sockfd);
      return(0);


}
debian:~/sam$ ./single_sample 67777
Server Sending (DO TERMINAL TYPE)
Server Sent query 0 to Client:FF FD 18
n = 3
Server received response from Client: FF FC 18

Server Sent query 1 to Client:FF FD 1E
n = 3
Server received response from Client: FF FC 1E

Server Sent query 2 to Client:FF FD 1D
n = 3
Server received response from Client: FF FC 1D

Server Sent query 3 to Client:FF FD 18
n = 3
Server received response from Client: FF FC 18

Server Sent query 0 to Client:FF FD 18
n = -1
Server received response from Client: FF FC 18
debian:~/sam$ ./single_client 127.0.0.1 67777
Recieving Buffer 3 from Server side FF FD 18 
Sent Buffer bytes 3: FF FC 18 from client side 

Recieving Buffer 3 from Server side FF FD 1E 
Sent Buffer bytes 3: FF FC 1E from client side 

Recieving Buffer 3 from Server side FF FD 1D 
Sent Buffer bytes 3: FF FC 1D from client side 

Recieving Buffer 3 from Server side FF FD 18 
Sent Buffer bytes 3: FF FC 18 from client side 
 int ctr = sizeof(buff1) ; 
 int ctr = sizeof(buff1) / sizeof(buff1[0]) ;