你好:
我现在使用cc3200的simplink的udp例程,代码中我使用的是sock的接口,我想实现通过getsockname来获取local ip,我不清楚sdk是否支持getsockname,如果不支持的话,可以通过其他方式实现吗?以下是我的代码:
int getLocal() {
struct sockaddr_storage random_addr;
memset(&random_addr, 0, sizeof(random_addr));
random_addr.ss_family = AF_INET;
struct sockaddr_in* v4_addr = (struct sockaddr_in*)&random_addr;
inet_pton(AF_INET, "8.8.8.8", &(v4_addr->sin_addr));
v4_addr->sin_port = 8888;
int localSkt = cc3200_socket(random_addr.ss_family, SOCK_DGRAM, IPPROTO_UDP);
if (localSkt < 0) return -1;
int nErr;
nErr = cc3200_connect(localSkt, (struct sockaddr *)&random_addr, sizeof(random_addr));
if (nErr < 0) return -2;
struct sockaddr_in localAddr;
memset(&localAddr, 0, sizeof(struct sockaddr_in));
socklen_t len = sizeof(struct sockaddr);
nErr = cc3200_getsockname(localSkt,(struct sockaddr*) &localAddr, &len);
if (nErr < 0) return -3;
FiPrint("get local ip:port = %s:%d\n", inet_ntoa(localAddr.sin_addr), ntohs(localAddr.sin_port));
}
Alvin Chen:
不支持的。
hex wu:
回复 Alvin Chen:
hi Alvin:我现在有一个这样的应用场景,udp通讯,创建一个socket后,我直接向一个指定的udp服务器sendto数据,这个时候,这个socket_fd对应的端口号是协议栈内核随机分配的,请问我可以通过哪种方式得到这个端口号(除了在udp服务器端的recvfrom得到以外,还有其他方法吗)。
int getLocal() {struct sockaddr_storage random_addr;memset(&random_addr, 0, sizeof(random_addr));random_addr.ss_family = AF_INET;struct sockaddr_in* v4_addr = (struct sockaddr_in*)&random_addr;inet_pton(AF_INET, "8.8.8.8", &(v4_addr->sin_addr));v4_addr->sin_port = htonl(8888);
int localSkt = cc3200_socket(random_addr.ss_family, SOCK_DGRAM, IPPROTO_UDP);if (localSkt < 0) return -1;
int nErr;nErr = sendto(localSkt, "12345", 5, 0, (struct sockaddr *)&random_addr, sizeof(random_addr));if (nErr < 0) return -2;
}