2015-06-05 20 views
1

如何在Standard ML中使用Socket.select如何使用Socket.select

根據docs,我應該通過它三個套接字列表和一個超時option,並且當任何套接字準備好做某事時函數返回(我假設,但不知道確定在列表中的插座是只有需要注意的插座)。但是,

  1. 輸入似乎既不是一個元組,也不是四個參數。我如何去構建一個合適的輸入結構?
  2. select需要並返回sock_desc的列表,並且似乎沒有辦法從其sock_desc返回socket。似乎也沒有辦法構建一個有效的地圖,因爲它似乎不可能訂購兩個,只是比較它們的平等性。一旦我得到了返回值select,我該如何對返回的套接字執行任何有用的操作,比如寫出響應,或者致電accept

回答

2
  1. 的輸入參數是用四個字段的記錄,所以你的代碼應該是這個樣子:
Socket.select { 
    rds = readSocketDescs, 
    wrs = writeSocketDescs, 
    exs = exnSocketDescs, 
    timeout = SOME (Time.fromSeconds 10) 
} 
  • 呀,不知道,可能你需要使用列表來保存映射。效率不高,但我看不出你還能做什麼。
  • (** 
    * Produces a list of all the pairs in `listPair`, whose keys are present 
    * in `whiteList`. Example: 
    * 
    * ```sml 
    * - filterListPair op= [(1,"a"), (2,"b"), (3,"c")] [2,3]; 
    * val it = [(2,"b"),(3,"c")] : (int * string) list 
    * ``` 
    *) 
    fun filterListPair eq listPair whiteList = 
        let 
        fun recur listPair whiteList result = 
         case (listPair, whiteList) of 
         ([], _) => result 
         | (_, []) => result 
         | ((x, y) :: xs, k :: ks) => 
          if eq (x, k) 
          then recur xs ks ((x, y) :: result) 
          else recur xs whiteList result 
        in 
        List.rev (recur listPair whiteList []) 
        end 
    
    val sockets = [ (* what have you *) ] 
    val descsToSockets = List.map (fn s => (Socket.sockDesc s, s)) sockets 
    val { rds, wrs, exs } = Socket.select { 
        rds = readSocketDescs, 
        wrs = writeSocketDescs, 
        exs = exnSocketDescs, 
        timeout = SOME (Time.fromSeconds 10) 
    } 
    
    (* 
    * The contract of Socket.select ensures that the order in input lists 
    * is preserved in the output lists, so we can use `filterListPair`. 
    *) 
    val selectedReadSockets = 
        filterListPair Socket.sameDesc descsToSockets rds