[問題] sendto issue with ipv6/ipv4 on Windows

看板C_and_CPP作者 (LALALA)時間8年前 (2015/10/22 10:47), 編輯推噓1(105)
留言6則, 2人參與, 最新討論串1/1
開發平台(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:32, , 2F
vs2008我不會 但是wiki IPv6 底下有 轉換機制的介紹
10/22 19:32, 2F

10/22 19:33, , 3F
可以看
10/22 19:33, 3F

10/23 09:37, , 4F
謝謝longlongint, 也看過了您提供的連結~
10/23 09:37, 4F

10/23 09:37, , 5F
網路上好像也有轉換的程式實作,
10/23 09:37, 5F

10/23 09:38, , 6F
所以結論是, 要自己轉IP version的資料結構就是了?
10/23 09:38, 6F
文章代碼(AID): #1MA4u-4H (C_and_CPP)