gpt4 book ai didi

c++ - UDP 客户端未正确形成数据包

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

我正在尝试在 Linux 中创建一个客户端以允许发送 UDP 数据包。到目前为止,程序似乎正确地形成了所有部分,但是当我尝试发送数据时,程序无法发送数据包。

I've put the code here ,代码输出示例如下:

jeremiah@ubuntu:~/Documents/Networking 2/Lab3/client$ sudo ./a.out 192.168.1.128 22222great successImperial Soviet Comrade: SVN -> sin_family: 2Imperial Soviet Comrade: SVN -> sin_port: 52822Imperial Soviet Comrade: SVN -> sin_addr: -2147374912Attention Comrade: Enter Message: redImperial Soviet Comrade: Data in packet dump ::::::::::::::::::::::::::::::::::::::::: startImperial Soviet Comrade: the size of the UPC is 1576Imperial Soviet Comrade: Socket: 3Imperial Soviet Comrade: UPC -> flags: 1Imperial Soviet Comrade: UPC -> offset: 0Imperial Soviet Comrade: UPC -> length: 8Imperial Soviet Comrade: UPC -> filename: redImperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000Imperial Soviet Comrade: UPC -> data: 0000000000000000000000000000000000000000Imperial Soviet Comrade: sizeof(UPC): 8Imperial Soviet Comrade: sockaddr.sin_family: 2Imperial Soviet Comrade: sockaddr.sin_port: 52822Imperial Soviet Comrade: sockaddr.sin_addr.s_addr: -2147374912Imperial Soviet Comrade: sizeof(sockaddr): 8Imperial Soviet Comrade: Data in packet dump ::::::::::::::::::::::::::::::::::::::::::: endImperial Soviet Comrade: size_sent: -1Alert Comrade: Data not sent: Invalid argument

它可以完全编译,没有错误,但有来自 printf 函数中的强制转换的警告。任何可以提供的帮助将不胜感激。

最佳答案

这确实很可疑:

int size_sent = sendto(SOCKET, UPC, sizeof(UPC)+2*MAX_MESSAGE, 0, 
(struct sockaddr*) &TARGET, sizeof(TARGET)+2*MAX_MESSAGE);

为什么要在消息长度和 sockaddr 结构的大小中添加 2 * MAX_MESSAGE?最后一个很可能是您的 sendto() 调用返回 -1 的原因。

另请记住,sizeof(UPC) 将是 4 或 8,而不是您想要的值。

关于c++ - UDP 客户端未正确形成数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10299413/

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