[問題] sendto issue with ipv6/ipv4 on Windows
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Windows, VC++
問題(Question):
由於開發網路程式要在IPv6和IPv4都能運作,
於是程式一開始會先創造一個AF_INET6的socket.
但是在Linux可以運作的程式, 在Windows上無法順利運作,
所以寫了一個簡易程式測試sendto
Linux測試程式
int main(void)
{
int sock_fd;
if((sock_fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
{
perror("socket failed");
return 0;
}
struct sockaddr_in remote;
int sizeremote = sizeof(struct sockaddr_in);
memset(&remote, 0, sizeof(remote));
remote.sin_family = AF_INET;
remote.sin_port = htons(IP_PORT);
remote.sin_addr.s_addr = inet_addr(IP_Address);
char sendbuf[128] = "This is a test";
int rc = sendto(sock_fd, sendbuf, strlen(sendbuf), 0,
(struct sockaddr*)&remote, sizeremote);
if(rc < 0)
{
perror("sendto failed");
return 0;
}
return 1;
}
同樣的程式直接在VC2008上跑, 在sendto會回傳-1,
使用WSAGetLastErrore, 回傳為10014 (WSAEFAULT)
想請教, 能處理IPv6和IPv4的Windows程式, 理應當要如何撰寫?
(是否要先判斷IP version, 再開對應的socket? 不能像Linux?)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.219.195.33
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1445482046.A.111.html
→
10/22 19:30, , 1F
10/22 19:30, 1F
推
10/22 19:32, , 2F
10/22 19:32, 2F
→
10/22 19:33, , 3F
10/22 19:33, 3F
→
10/23 09:37, , 4F
10/23 09:37, 4F
→
10/23 09:37, , 5F
10/23 09:37, 5F
→
10/23 09:38, , 6F
10/23 09:38, 6F