背景:服务器和客户端需要使用同一个端口,进行udp数据包的互传,发送过程都没有问题,抓包软件抓到的包也正常,但是为什么在接收的时候显示了icmp端口不可达问题。代码在下面,DEV_BIND_S过程中向服务器发送了一个udp单播,DEV_DATA_RW_S中接收服务器udp单播失败了。
case DEV_BIND_S:
/* Bind socket (optional) */
addr_local.sin_family = AF_INET;
addr_local.sin_port = FNET_HTONS(50002);
addr_local.sin_addr.s_addr = FNET_HTONL(INADDR_ANY);
//addr_local.sin_scope_id = 0;
if (fnet_socket_bind( update_handle->socket_id, (struct fnet_sockaddr *)&addr_local, sizeof(addr_local)) == FNET_ERR)
{
fnet_printf("bind update_confirm socket fail \r\n");
closesocket(update_handle->socket_id);
update_handle->socket_state = DEV_INIT_S;
} else {
add_remote.sa_family = AF_INET;
add_remote.sa_port = addr_local.sin_port;
update_handle->fapp_client_ack.code=0;
memcpy(add_remote.sa_data, update_handle->update_info.serverIP, VERSION_INFO_MAX_LEN);
if(sendto(update_handle->socket_id, &update_handle->fapp_client_ack,
sizeof(update_handle->fapp_client_ack), 0, (struct fnet_sockaddr *)&add_remote, sizeof(add_remote)) < 0) {
fnet_printf("admin socket sendto error \r\n");
closesocket(update_handle->socket_id);
update_handle->socket_state = DEV_INIT_S;
return;
}
update_handle->socket_state = DEV_DATA_RW_S;
}
break;
case DEV_DATA_RW_S:
recv_size = recvfrom(update_handle->socket_id, recv_buff, sizeof(recv_buff), 0, (struct fnet_sockaddr *)&add_remote, &fromlen);
if(recv_size==0){
update_handle->socket_state = DEV_DATA_RW_S;
}
else if (recv_size != sizeof(fapp_update_confirm_t)) {
fnet_printf("update confirm socket recv error\r\n");
//update_confirm_socket_close();
closesocket(update_handle->socket_id);
update_handle->socket_state = DEV_INIT_S;
}
else {
fapp_handle_update_cmd(recv_buff);
}
break;