# 網路程式設計 HW2 ## 1.使用wireshark抓DNS request/reply封包 印出封包標頭的截圖,以及所執行的指令 * code ```cpp== #include <iostream> #include <winsock.h> #include <sstream> using namespace std; int main() { WSADATA windata; WSAStartup(0X101,(LPWSADATA) & windata); char ipaddress[32]; cout<<"the ip is : "; cin>>ipaddress; cout<<endl; LPHOSTENT hp; struct in_addr sAddr; sAddr.s_addr=inet_addr(ipaddress); hp=gethostbyaddr((LPSTR) &sAddr,sizeof(sAddr),AF_INET); if(hp==NULL)cout<<"NULL"<<endl; else cout<<"host name : "<<hp->h_name<<endl; } ``` * 輸出結果 ![](https://i.imgur.com/2Vv7PNe.png) * 指令 ip.addr ==10.1.200.77 &&dns ![](https://i.imgur.com/tjqdnua.png) * request ![](https://i.imgur.com/UGBVqqM.png) * reply ![](https://i.imgur.com/K6e8EHW.png) ## 2.印出subnet的所有ip其domain name 請寫一個程式,當給定一個subnet address,可印出其範圍的所有ip的domain name。 例如:輸入:140.130.175 ,則印出140.130.175.1-255所有主機的domain name。若無則跳過。 請繳交程式碼(並用註解簡要說明)以及執行畫面 * code ```cpp== #include <iostream> #include <winsock.h> #include <sstream> using namespace std; int main() { WSADATA windata; WSAStartup(0X101,(LPWSADATA) & windata); char ipaddress[32]; string IP; cout<<"the ip is : "; cin>>IP; //輸入subnet address cout<<endl; for(int i=1;i<=255;i++) { string str; stringstream s1; s1<<i; s1>>str; string IPAddress; IPAddress=IP+'.'+str; //完整IP stringstream s2; s2<<IPAddress; s2>>ipaddress; LPHOSTENT hp; struct in_addr sAddr; sAddr.s_addr=inet_addr(ipaddress); hp=gethostbyaddr((LPSTR) &sAddr,sizeof(sAddr),AF_INET); if(hp==NULL)cout<<"NULL"<<endl; //IP不存在 else cout<<"host name : "<<hp->h_name<<endl; } } ``` * 輸出結果 ![](https://i.imgur.com/6Fip4PK.png) . .(中間太多,省略) . ![](https://i.imgur.com/rsmlia7.png) ## 3.本週心得 請寫下對於本週影片教學和程式作業的適應程度與喜惡。 這是我第一次寫網路程式,我發現它要的知識量遠超乎我的想像,真的需要一定基礎學起來比較輕鬆一些。還挺好玩的。 ###### tags: `網路程式設計`