UDP客户端无法正确形成数据包

时间:2012-04-24 13:55:06

标签: c++ linux udp

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

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

jeremiah@ubuntu:~/Documents/Networking 2/Lab3/client$ sudo ./a.out 192.168.1.128 22222
great success
Imperial Soviet Comrade: SVN -> sin_family: 2
Imperial Soviet Comrade: SVN -> sin_port: 52822
Imperial Soviet Comrade: SVN -> sin_addr: -2147374912
Attention Comrade: Enter Message: red


Imperial Soviet Comrade: Data in packet dump ::::::::::::::::::::::::::::::::::::::::: start
Imperial Soviet Comrade: the size of the UPC is 1576
Imperial Soviet Comrade: Socket: 3
Imperial Soviet Comrade: UPC -> flags: 1
Imperial Soviet Comrade: UPC -> offset: 0
Imperial Soviet Comrade: UPC -> length: 8
Imperial Soviet Comrade: UPC -> filename: red
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 00000000000000000000000000000000000000000000000000000000000000000000000000000000
Imperial Soviet Comrade: UPC -> data: 0000000000000000000000000000000000000000
Imperial Soviet Comrade: sizeof(UPC): 8
Imperial Soviet Comrade: sockaddr.sin_family: 2
Imperial Soviet Comrade: sockaddr.sin_port: 52822
Imperial Soviet Comrade: sockaddr.sin_addr.s_addr: -2147374912
Imperial Soviet Comrade: sizeof(sockaddr): 8
Imperial Soviet Comrade: Data in packet dump ::::::::::::::::::::::::::::::::::::::::::: end


Imperial Soviet Comrade: size_sent: -1
Alert Comrade: Data not sent: Invalid argument

它完全编译没有错误,但是printf函数中的casting会有警告。我们将非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

这真的很可疑:

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,而不是你想要的。