首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 網路程式設計:協議與檔案傳輸 (3) — FileRequestMode 列舉與 FileProtocol 結構定義

     編輯:關於C語言

2.2 強型別協議模型

XML 以字串傳輸,為方便操作建立強型別模型。先定義 FileRequestMode 列舉區分傳送/接收:

public enum FileRequestMode {
    Send = 0,
    Receive
}

再定義 FileProtocol 結構,提供協議屬性的強型別存取,並覆寫 ToString() 自動產生 XML,客戶端只需呼叫 ToString() 即可:

public struct FileProtocol {
    private readonly FileRequestMode mode;
    private readonly int port;
    private readonly string fileName;

    public FileProtocol(FileRequestMode mode, int port, string fileName) {
        this.mode = mode; this.port = port; this.fileName = fileName;
    }

    public FileRequestMode Mode { get { return mode; } }
    public int Port { get { return port; } }
    public string FileName { get { return fileName; } }

    public override string ToString() {
        return String.Format("<protocol><file name=\"{0}\" mode=\"{1}\" port=\"{2}\" /></protocol>",
            fileName, mode, port);
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved