gpt4 book ai didi

c - 未在服务器端客户端-服务器 TCP 上接收单独的值

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:13 25 4
gpt4 key购买 nike

我在客户端-服务器程序的服务器端接收正确值时遇到问题。

服务器端和客户端都包含的头文件:

#define CHUNK_SIZE 1024
#define ARR_LEN 3

客户:

int uids[ARR_LEN] = {1994, 2423, 1222};
unsigned int uidlen = 0;
char uidbuffer[CHUNK_SIZE] = {0};

for(int i = 0; i < ARLL; i++)
{
uidlen = strlen(uids[i])+1;
snprintf(uidbuffer, uidlen, "%s", uids[i]);

if(send(socket, uidbuffer, strlen(uidbuffer), 0) < 0)
DIE("Write Error");
if(recv(socket, uidbuffer, sizeof(uidbuffer), 0) < 0)
DIE("Acknowledge Error");

memset(uidbuffer, 0, sizeof(uidbuffer));
}

服务器:

char uid_buff[CHUNK_SIZE];
for(int i = 0; i < ARR_LEN; i++)
{
memset(uid_buff, 0, sizeof(uid_buff));
// receiving the UID and storing it directly
if(recv(client_sock, uid_buff, sizeof(uid_buff), 0) < 0)
DIE("Receive Error");

printf("buffer content: %s\n", uid_buff);
uid_str = uid_buff;

uids[i] = (uid_t)strtol(uid_str, (char **)NULL, 10);

if(send(client_sock, uid_buff, sizeof(uid_buff), 0) < 0)
DIE("Acknowledge Error");
}

这些只是我程序的一部分。我试图只包括相关部分。输出是这样的:

buffer content: 1994
buffer content: 24231222
buffer content:

虽然我希望它是:

buffer content: 1994
buffer content: 2423
buffer content: 1222

可能是什么问题?我知道这不是那么容易,而且服务器-客户端通信是在字节流而不是消息中执行的,但我想通过确认收到的每条“消息”来模拟该功能。你能告诉我该怎么做吗?我越来越绝望了。

最佳答案

您将需要一个协议(protocol)。例如,您定义应用程序中的每条消息都具有以下格式:

xx | message

这意味着您收到的前两个字节(注意字节序)表示后面消息的长度。现在您应该首先接收前两个字节 - 检查长度 - 然后接收准确的字节数。之后您就知道您已成功收到该消息。然后您可以继续处理其他消息(可能/应该具有类似的格式:长度 + 消息本身)。

例子:假设您想发送三个消息:

char s1[]="message1";
char s2[]="message2";
char s3[]="message3";

//You do this(client side):

int x1 = strlen(s1); // length of message1
int intsize = 4; // just size of integer -we'll need in next call

sendall(socket, &x1, &intsize); // send length of first message
sendall(socket, s1, &x1); // Now send the message

//On server:
int x = 0;
int y = 4; //size of integer most probably
receiveall(socket,&x,&y);//get length first; y=4 because that is size of integer
receiveall(socket, buffer, &x); // now we know how many bytes to expect - x - so request that number of bytes only

您也可以对其他消息重复此逻辑。

最后,您想使用这样的函数 ( here ) 而不是 sendreceive (因为发送和接收可能不会发送/接收您的字节数告诉它):

int sendall(int s, char *buf, int *len)
{
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;

while(total < *len) {
n = send(s, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}

*len = total; // return number actually sent here

return n==-1?-1:0; // return -1 on failure, 0 on success
}

您将需要一个类似的receiveall 函数。

关于c - 未在服务器端客户端-服务器 TCP 上接收单独的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29457602/

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