作者KuoZheChiou (LALALA)
看板C_and_CPP
标题[问题] sendto issue with ipv6/ipv4 on Windows
时间Thu Oct 22 10:47:24 2015
开发平台(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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1445482046.A.111.html
2F:推 longlongint: vs2008我不会 但是wiki IPv6 底下有 转换机制的介绍 10/22 19:32
3F:→ longlongint: 可以看 10/22 19:33
4F:→ KuoZheChiou: 谢谢longlongint, 也看过了您提供的连结~ 10/23 09:37
5F:→ KuoZheChiou: 网路上好像也有转换的程式实作, 10/23 09:37
6F:→ KuoZheChiou: 所以结论是, 要自己转IP version的资料结构就是了? 10/23 09:38