gpt4 book ai didi

c - 从 32 位机器发送数据包并在 64 位机器上接收

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:40 26 4
gpt4 key购买 nike

我正在编写一个在不同位机之间交换消息的程序。我的 Send、Receive 和 readn 函数如下所示。问题是我有一种发送消息的数据包格式。数据包类型决定了消息的类型,如欢迎、数据、用户名等。我从 sd 读取数据包类型,然后读取剩余数据。当我从 32 位发送时,数据包类型将为 x 到 x+4,数据将为 x+4 等等。在接收时,在 64 位机器上,数据包类型从 x 读取到 x+8 和 x+8 作为数据,因此我的读取函数永远等待以获取指定长度的剩余字节。我该如何解决这段代码中的这个问题?

/*----------------------------------------------------------------*/

/* messsges received/sent by server */
#define WELCOME_MSG 0
#define USER_NAME 1
#define EMAIL_MSG_TO_SERVER 2
#define EMAIL_MSG_TO_CLIENT 3
#define CLOSE_CON 4
/* structure of a packet */
typedef struct _packet {

/* packet type */
char type;

/* packet length */
long lent;

/* packet text */
char * text;

} Packet;

int readn(int sd, char *buf, size_t n) {
printf("readn via utils. %d, %s, %d\n", sd, buf, n);
size_t toberead = n;
char * ptr = buf;

while (toberead > 0) {

int errno_save = 0;

fprintf(stderr, "toberead: %zu\n", toberead);

ssize_t byteread = read(sd, ptr, toberead);
errno_save = errno;

fprintf(stderr, "toberead: %zu, byteread: %zd\n", toberead, byteread);

if (byteread <= 0) {
fprintf(stderr, "byteread val: %d",byteread);
if (byteread == -1)
{
perror("read");
errno = errno_save;
}
return (0);
}

toberead -= byteread;
ptr += byteread;
}

if ('\0' != buf[n]) /* This assumes buf is one byte **larger** then n. */
{
buf[n] = '\0';
}

fprintf(stderr, "Finished readn. %s\n", buf);
return (1);
}

/*----------------------------------------------------------------*/



Packet *recvpkt(int sd)
{
printf("Recvpkt via utils.\n");
Packet *pkt;

/* allocate space for the pkt */
pkt = (Packet *) calloc(1, sizeof(Packet));
if (!pkt) {
fprintf(stderr, "error : unable to calloc\n");
return(NULL);
}

/* read the message type */
if (!readn(sd, (char *) &pkt->type, sizeof(pkt->type))) {
free(pkt);
return(NULL);
}

/* read the message length */
if (!readn(sd, (char *) &pkt->lent, sizeof(pkt->lent))) {
free(pkt);
return(NULL);
}
pkt->lent = ntohl(pkt->lent);

/* allocate space for message text */
if (pkt->lent > 0) {
pkt->text = (char *) malloc(pkt->lent);
if (!pkt) {
fprintf(stderr, "error : unable to malloc\n");
return(NULL);
}

/* read the message text */
if (!readn(sd, pkt->text, pkt->lent)) {
freepkt(pkt);
return(NULL);
}
}

fprintf(stderr, "Reading packet complete succesfully.\n");

/* done reading */
return(pkt);
}

int sendpkt(int sd, char typ, long len, char *buf)
{
fprintf(stderr, "Send packet via utils. sd: %d, typ: %c, len: %lu, buf: %s\n", sd, typ, len, buf);
char tmp[8];
long siz;

/* write type and lent */
bcopy(&typ, tmp, sizeof(typ));
siz = htonl(len);
bcopy((char *) &siz, tmp+sizeof(typ), sizeof(len));
write(sd, tmp, sizeof(typ) + sizeof(len));

/* write message text */
if (len > 0)
write(sd, buf, len);
return(1);
}

void freepkt(Packet *pkt)
{
fprintf(stderr, "Freeing packet.\n");
free(pkt->text);
free(pkt);
}

最佳答案

使用 stdint.h header ,它定义的类型的名称告诉您它们的大小。它们在任何平台上都将始终是那个尺寸。

#include <stdint.h>

/* structure of a packet */
typedef struct _packet {
/* packet type */
uint8_t type;
/* packet length */
uint32_t lent;
/* packet text */
char * text;
} Packet;

关于c - 从 32 位机器发送数据包并在 64 位机器上接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20314807/

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