前提・実現したいこと
WinSock2によるHTTPクライアントの作成を作成しようとしています.gethostname
等では基本的にエラーが出ますし
- IPv6
- UNICODE
で書いていきたいと思い,GetAddInfo
,ADDRINFOW
を使っていますがconnect
が失敗しWSA Error(WSAECONNREFUSED:10061)が返ってきます.
どのような原因が考えられるでしょうか.GetAddrInfo
の引数はbig endianではないですよね?
入力するGetAddrInfo
へのhostnameにhost以上のパスが含まれる場合や"http"が含まれる場合にもエラーになりますか?
該当のソースコード
スレッドのソケット系の箇所のコードになります.2行目でメインスレッドからパスを取得しています.
WSAStartup等の大きなエラーチェックは省略しています.
C
1#define UNICODE 2#include <windowsx.h> 3#include <WinSock2.h> 4#include <ws2tcpip.h> 5#include <process.h> 6 7{ 8 LPCWSTR turl = tph->lpURL;//url from main thread 9 LPCWSTR pnum = L"80";//http 10 11 /* Initialization of WinSocket */ 12 WSADATA wsad; 13 int err_wsas = WSAStartup(MAKEWORD(2, 0), &wsad); 14 if (err_wsas != 0) MYLOCALERROR 0x00010000; 15 16 /* END Initialization of WinSocket */ 17 18 /* DNS Name Resolution from Host */ 19 ADDRINFOW* ai; 20 ADDRINFOW hint = { 0 }; 21 22 ZeroMemory(&hint, sizeof(ADDRINFOW)); 23 hint.ai_socktype = SOCK_STREAM; 24 hint.ai_family = AF_INET; 25 26 int err_gai = GetAddrInfo(turl, pnum, &hint, &ai); 27 if (err_gai != 0) MYLOCALERROR 0x00020000; 28 29 /* END DNS Name Resolution from Host */ 30 31 wchar_t art[1024]; 32 wsprintf(art, L"ADDRIN\r\nSOCKADDR : %p\r\nsize : %d\r\n", ai->ai_addr, ai->ai_addrlen); 33 SetWindowText(tph->harticle, art); 34 35 /* Open Socket */ 36 SOCKET s; 37 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 38 if (s == INVALID_SOCKET) { 39 MessageBox(thwnd, L"INVALID_SOCKET", L"socket", MB_OK); 40 WSACleanup(); 41 MYLOCALERROR = 0x00110001; 42 break; 43 } 44 /* END Open Socket */ 45 46 /* Connecting */ 47 48 int c = connect(s, ai->ai_addr, ai->ai_addrlen); 49 if (c != 0) 50 { 51 int wer = WSAGetLastError(); 52 wchar_t wsagerr[128]; 53 wsprintf(wsagerr, L"ERR CODE : %d\n",wer); 54 MessageBox(thwnd, wsagerr, L"connect", MB_OK); 55 MYLOCALERROR = 0x00040001; 56 break; 57 } 58 /* END Connecting */ 59 60 /* Send */ 61 int sndstatus = send(s, "GET /index.html HTTP/1.0\r\n\r\n", strlen("GET /index.html HTTP/1.0\r\n\r\n"), 0); 62 63 if (sndstatus > 0) 64 { 65 MessageBox(thwnd, L"Success", L"send", MB_OK); 66 MYLOCALERROR = 0x00050001; 67 break; 68 } 69 70 /* END Send */ 71 72 FreeAddrInfo(ai); 73 74 75 WSACleanup(); 76 EnableWindow(tph->hurl, TRUE); 77 EnableWindow(tph->hbrowse, TRUE); 78 }
補足情報
Windows10 Pro
VisualStudio2019 Community
回答1件
あなたの回答
tips
プレビュー