gpt4 book ai didi

sockets - 如何使用Socket.select

转载 作者:行者123 更新时间:2023-12-03 11:59:57 32 4
gpt4 key购买 nike

如何在标准ML中使用Socket.select

根据docs,我应该向它传递三个套接字列表和一个超时option,当任何一个套接字准备好做某事时,该函数都会返回(而且我假设,但不确定是否知道套接字名单中只有需要注意的人)。然而,

  • 输入似乎既不是元组,也不是四个参数。我该如何构建适当的输入结构?
  • select接收并返回sock_desc列表,似乎没有一种方法可以从socket取回sock_desc。似乎也没有一种构建有效映射的方法,因为似乎不可能对两个sock_desc进行排序,仅比较它们的相等性即可。获得select的返回值后,如何对返回的套接字做任何有用的事情,例如写出响应或在它们上调用accept
  • 最佳答案

  • 输入参数是一个包含四个字段的记录,因此您的代码应如下所示:

  • 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

    关于sockets - 如何使用Socket.select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669207/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com