gpt4 book ai didi

c++ - 在套接字编程中发送额外的查询

转载 作者:行者123 更新时间:2023-11-30 17:26:42 25 4
gpt4 key购买 nike

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

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

FF,FD,18 --->

                 <---FF,FE,18

FF,FD,1E --->

                 <---FF,FE,1E

客户端

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


}

对于从服务器发送的每条消息,都应该有来自客户端的响应,但在我的代码中,总是有一条额外的消息从服务器发送到客户端。如输出所示,服务器端发送了一个额外的查询。

服务器端的缓冲区有4个查询。所以基本上它应该发送 4 个查询,但这里它发送一个额外的查询。

输出:服务器端:

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

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

关于c++ - 在套接字编程中发送额外的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26677604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com