---
# System prepended metadata

title: 網路程式設計 HW3
tags: [網路程式設計]

---

# 網路程式設計 HW3

## 1.使用wireshark抓封包
* 請使用wireshark抓當telnet echo server時，先送出abc，回傳abc後結束，此程序過程的所有tcp封包
* 提示：會有四類的封包：建立連線，傳送，接放，結束連線
    * 用程式跑
        * code
             ![](https://i.imgur.com/5mAeHrA.png)
        * wireshark
             ![](https://i.imgur.com/yXQBodr.png)
    * 使用 Powershell
        * 指令:
            1. telnet localhost 7
            2. abc(畫面顯示 " abbcc")
            3. ctrl+]
            4. close
        * wireshark
            ![](https://i.imgur.com/jSGeNZP.png)

## 2.5種服務的示範程式
* 請修改範例程式，完成一個示範5種服務的程式。
* 當程式執行時，可選擇何種服務。示範後，結束程式。
    1. 回應服務 (Echo):送出 I love NCYU CSIE
    2. 丟棄服務 (Discard): :送出 I love NCYU CSIE
    3. 日期時間服務 (Daytime)
    4. 每日一句 (QUOTE)
    5. 字元產生器 (CHARGEN): 只要顯示接收1024個字元。
* 請繳交程式碼(並用註解簡要說明)以及執行畫面
* code
```cpp==
#include <iostream>
#include <winsock.h>
using namespace std;

void echo()  //port 7
{
    SOCKET sd;
    struct sockaddr_in serv;
    char str[1024]="I love NCYU CSIE";
    char str_r[1024]="";
    WSADATA wsadata;
    int n=1;

    WSAStartup(0x101,(LPWSADATA) &wsadata);  //呼叫WSAStartup() 註冊WinSock DLL的使用
    sd=socket(AF_INET,SOCK_STREAM,0); //開啟TCP socket

    //為連線做準備，包含填寫sockaddr_in結構
    //內容有:server的IP位址，port number等等
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv.sin_port = htons(7);
    connect(sd,(LPSOCKADDR) &serv,sizeof(serv));//連接至echo sever

    cout<<"++++++++++++++"<<endl
        <<"|  執行結果  |"<<endl
        <<"++++++++++++++"<<endl<<endl;


    send(sd,str,strlen(str)+1,0); //傳送how are you至echo server

    cout<<"[傳送內容]      "<<str<<endl;
    cout<<"[傳送內容長度]  "<<strlen(str)+1<<" bytes"<<endl;

    n=recv(sd,str_r,1024,0); //echo server接收
    cout<<"[回應內容]      "<<str_r<<endl;
    cout<<"[回應內容長度]  "<<n<<" bytes"<<endl;

    closesocket(sd); // 關閉TCP socket
    WSACleanup();   //結束Winsock DLL的使用
    system("pause");
}

void discard() //port 9
{
    SOCKET sd;
    struct sockaddr_in serv;
    char str[1024]="I love NCYU CSIE";
    char str_r[1024]="";
    WSADATA wsadata;
    int n=1;

    WSAStartup(0x101,(LPWSADATA) &wsadata);
    sd=socket(AF_INET,SOCK_STREAM,0);

    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv.sin_port = htons(9);
    connect(sd,(LPSOCKADDR) &serv,sizeof(serv));

    cout<<"++++++++++++++"<<endl
        <<"|  執行結果  |"<<endl
        <<"++++++++++++++"<<endl<<endl;

    send(sd,str,strlen(str)+1,0);
    cout<<"[傳送內容]       "<<str<<endl;
    cout<<"[傳送內容長度]   "<<strlen(str)+1<<" bytes"<<endl;

    cout<<"[回應內容]       "<<endl;
    cout<<"[回應內容長度]   "<<n<<" bytes"<<endl;

    closesocket(sd);
    WSACleanup();
    system("pause");
}
void daytime() //port 13
{
    SOCKET sd;
    struct sockaddr_in serv;
    char str[1024]="";
    char str_r[1024]="";
    WSADATA wsadata;
    int n=1;

    WSAStartup(0x101,(LPWSADATA) &wsadata);
    sd=socket(AF_INET,SOCK_STREAM,0);

    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv.sin_port = htons(13);
    connect(sd,(LPSOCKADDR) &serv,sizeof(serv));

    cout<<"++++++++++++++"<<endl
        <<"|  執行結果  |"<<endl
        <<"++++++++++++++"<<endl<<endl;


    cout<<"[傳送內容]        "<<str<<endl;
    cout<<"[傳送內容長度]    "<<strlen(str)+1<<" bytes"<<endl;

    n=recv(sd,str_r,1024,0);
    cout<<"[回應內容]       "<<str_r<<endl;
    cout<<"[回應內容長度]   "<<n<<" bytes"<<endl;

    closesocket(sd);
    WSACleanup();
    system("pause");
}

void quote() //port 17
{
    SOCKET sd;
    struct sockaddr_in serv;
    char str[1024]="";
    char str_r[1024]="";
    WSADATA wsadata;
    int n=1;

    WSAStartup(0x101,(LPWSADATA) &wsadata);
    sd=socket(AF_INET,SOCK_STREAM,0);

    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv.sin_port = htons(17);
    connect(sd,(LPSOCKADDR) &serv,sizeof(serv));

    cout<<"++++++++++++++"<<endl
        <<"|  執行結果  |"<<endl
        <<"++++++++++++++"<<endl<<endl;


    cout<<"[傳送內容]"<<endl;

    cout<<"[傳送內容長度]   "<<strlen(str)+1<<" bytes"<<endl;

    n=recv(sd,str_r,1024,0);
    cout<<"[回應內容]       "<<str_r<<endl;
    cout<<"[回應內容長度]   "<<n<<" bytes"<<endl;

    closesocket(sd);
    WSACleanup();
    system("pause");
}
void chargen() //port 19
{
    SOCKET sd;
    struct sockaddr_in serv;
    char str[1024]="";
    char str_r[1024]="";
    WSADATA wsadata;
    int n=1;

    WSAStartup(0x101,(LPWSADATA) &wsadata);
    sd=socket(AF_INET,SOCK_STREAM,0);

    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv.sin_port = htons(19);
    connect(sd,(LPSOCKADDR) &serv,sizeof(serv));

    cout<<"++++++++++++++"<<endl
        <<"|  執行結果  |"<<endl
        <<"++++++++++++++"<<endl<<endl;

    send(sd,str,strlen(str)+1,0);
    cout<<"[傳送內容]      "<<str<<endl;
    cout<<"[傳送內容長度]  "<<strlen(str)+1<<" bytes"<<endl;

    n=recv(sd,str_r,1024,0);
    cout<<"[回應內容]     "<<str_r<<endl;
    cout<<"[回應內容長度] "<<n<<" bytes"<<endl<<endl;

    closesocket(sd);
    WSACleanup();
    system("pause");
}
int main()
{
    while(1)
    {
        cout<<"++++++++++++++++++++++++++++++++++++++"<<endl;
        cout<<"|  請輸入服務的代號(1~5)以執行任務   |"<<endl;
        cout<<"|  1.回應服務(Echo)                  |"<<endl;
        cout<<"|  2.丟棄服務(Discard)               |"<<endl;
        cout<<"|  3.日期時間服務(DayTime)           |"<<endl;
        cout<<"|  4.每日一句(Quote）                |"<<endl;
        cout<<"|  5.字元產生器(Chargen)             |"<<endl;
        cout<<"|  6.結束程式                        |"<<endl;
        cout<<"++++++++++++++++++++++++++++++++++++++"<<endl;

        int choise;
        cin>>choise;
        if(choise==1)
            echo();
        else if(choise==2)
            discard();
        else if(choise==3)
            daytime();
        else if(choise==4)
            quote();
        else if(choise==5)
            chargen();
        else
            exit(0);
    }
}


```
* 執行畫面
    1. 回應服務 (Echo)
        ![](https://i.imgur.com/pQySRLF.png)
    2. 丟棄服務 (Discard)
        ![](https://i.imgur.com/wfWBofx.png)
    3. 日期時間服務 (Daytime)
        ![](https://i.imgur.com/5QzIbox.png)
    4. 每日一句 (QUOTE)
        ![](https://i.imgur.com/FBk7tNH.png)
    5. 字元產生器 (CHARGEN)
        ![](https://i.imgur.com/M8u1J8m.png)

## 3.本週心得
這是上課以來第一次提前公布作業，可以在上課前就看完影片，並寫完作業，覺得做起來比較沒壓力。而且可以在做每個步驟的時候，更清楚的思考。
在不了解的時候，有充足的時間可以和同學做討論。

###### tags: `網路程式設計`