when the first datagram is received, recvfrom() doesn't fill the structur `sockaddr`
c, network-programming
Solution
Before calling `recvfrom` you must properly initialize the `len` argument.
E.g.
len = sizeof(struct sockaddr_in);
n = recvfrom(..., &len);
The `recvfrom` function uses the length to help determine what kind of structure the `sockaddr` pointer actually points to.
Problem
I have two machines A and B in LAN I have a UDP client on A and a UDP server on B, like ``` for(;;){ n = recvfrom(sockfd, mesg, 10000, 0, (struct sockaddr *)&cliaddr, &len); ....} ``` I notice when the UDP client sends the first datagram I can get the datagram data payload through `mesg` correctly but the structure `cliaddr` is not filled, it is with the original value e.g, if I use `bzero(&cliaddr, sizeof(cliaddr));`, in `gdb`, I got ``` $1 = {sin_family = 0, sin_port = 0, sin_addr = {s_addr = 0}, sin_zero = "\000\000\000\000\000\000\000"} ``` what is the reason when the first datagram is received, recvfrom() doesn't fill the structure `cliaddr`? for the sebsequent datagram, the valid info can be obtained.