# 網路程式作業 HW2 題目連結: https://hackmd.io/@wang1234/Sy6r1wneq ## 1.使用wireshark抓DNS request/reply封包 * request封包標頭: ![](https://i.imgur.com/H3G6ILh.png) * reply封包標頭: ![](https://i.imgur.com/bQm3AQl.png) * 執行的指令: dns,附上圖片![](https://i.imgur.com/2QDkEIi.png) * 程式碼: ```cpp= #include <bits/stdc++.h> #include <stdio.h> #include <winsock.h> int main(void) { WSADATA wsadata; WSAStartup (0x101, (LPWSADATA) &wsadata); LPHOSTENT hp; struct in_addr sAddr; sAddr.s_addr = inet_addr("140.130.41.11"); hp = gethostbyaddr((LPSTR) &sAddr, sizeof(sAddr), AF_INET); printf("140.130.41.11's domain name:\n %s\n", hp->h_name); return 0; } ``` * 執行畫面: ![](https://i.imgur.com/ez1qEiU.png) ## 2. 印出subnet的所有ip其domain name * 程式碼: ```cpp= #include <bits/stdc++.h> #include <stdio.h> #include <winsock.h> #include <charconv> using namespace std; char req_ip[40]; int main() { WSADATA wsadata; WSAStartup (0x101, (LPWSADATA) &wsadata); string s; for(int i=1; i<=255; i++) { stringstream ss; s = "140.130.175." + to_string(i); ss << s; ss >> req_ip; LPHOSTENT hp; struct in_addr sAddr; sAddr.s_addr = inet_addr(req_ip); hp = gethostbyaddr((LPSTR) &sAddr, sizeof(sAddr), AF_INET); cout << "What is " << req_ip << "'s domain name?" << endl; cout << "Host name: " << hp->h_name << endl << endl; } return 0; } ``` * 執行畫面: ![](https://i.imgur.com/a2Qh7q0.png) . . . ![](https://i.imgur.com/YyjEp0p.png) ## 3. 本週心得 很新奇的東西,有影片的課前預習,上起課來覺得比較輕鬆也比較聽得懂。這次有個小發現,就是在家操作,連自己數據的時候程式碼跑不出來,需要連上學校wifi才可以執行出正確答案。 #### Hint💡: 這裡可以切換網路 ![](https://i.imgur.com/JPZT1GK.png) ![](https://i.imgur.com/xrs3SzX.png) ###### tags = `網路程式`