gpt4 book ai didi

以太网传输时字节顺序的变化

转载 作者:行者123 更新时间:2023-12-01 22:50:52 26 4
gpt4 key购买 nike

我使用 zedboard 和 vivado v2017.3。我正在尝试发送存储在以下程序中 baseaddr_p 指向的寄存器(Zynq PL 部分中的 slv_reg0)中的数据。

我使用 lwip echo 服务器应用程序读取这个地址并通过以太网将它发送到 PC。

这部分代码在收到回显请求(telnet)时执行。在这部分代码中,我还在 gtkterm 中打印读取的值,以便我可以验证接收到的数据。

void process_echo_request(void *p)
{
int sd = (int)p;
int RECV_BUF_SIZE = 2048;
char recv_buf[RECV_BUF_SIZE];
int n,i,nwrote;
unsigned long my_buffer[10];
i=0;
init_platform();
while(1){
while(i<11)
{
Xuint32 *baseaddr_p = (Xuint32 *)XPAR_MYIP_0_S00_AXI_BASEADDR;
my_buffer[i] = *(baseaddr_p);
xil_printf("0x%08x \n\r", my_buffer[i]);
i++;}
//xil_printf("0x%08x \n\r", my_buffer);
/* handle request */
if ((nwrote = write(sd, my_buffer, sizeof (my_buffer))) < 0) {
xil_printf("%s: ERROR responding to client echo request. received = %d, written = %d\r\n",
__FUNCTION__, n, nwrote);
xil_printf("Closing socket %d\r\n", sd);
break;

}

while (1) {
/* read a max of RECV_BUF_SIZE bytes from socket */
if ((n = read(sd, recv_buf, RECV_BUF_SIZE)) < 0) {
xil_printf("%s: error reading from socket %d, closing socket\r\n", __FUNCTION__, sd);
break;
}

/* break if the recved message = "quit" */
if (!strncmp(recv_buf, "quit", 4))
break;

/* break if client closed connection */
if (n <= 0)
break;


}
}

/* close connection */
close(sd);
vTaskDelete(NULL);
cleanup_platform();
}

gtkterm 结果:读取值0x000000040x000000050x000000060x000000070x000000080x000000090x0000000A0x0000000B0x0000000C0x0000000D0x0000000E

在接收这些数据时,似乎发生了字节反转。

收到的数据:04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 09 00 00 00 0A 00 00 00 0B 00 00 00 0C 00 00 00 0D 00 00 00

关于它发生的原因和可能的解决方法有什么建议吗?

最佳答案

问题是由于 endianess .当您发送和接收数据时,最好进行主机到网络的转换,反之亦然。

有两个 POSIX API 可以执行这些字节顺序转换:htonlntohl

在接收方尝试进行网络到主机的字节顺序转换,如果您有权访问发送方则在那里执行相反的操作。

关于以太网传输时字节顺序的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50408755/

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