首页 \ 问答 \ PHP UDP socket_recv()超时(PHP UDP socket_ recv() timeout)

PHP UDP socket_recv()超时(PHP UDP socket_ recv() timeout)

我正在研究可以通过UDP套接字进行通信的服务器/客户端php脚本。 我想知道是否有一种很好的方法来实现socket_recv()函数的timeout() 。 目前它在我的客户端阻塞,等待确认。

感谢您的输入!


I am working on server/client php scripts which can communicate over a UDP socket. I was wondering if there is a good way to implement a timeout() for the socket_recv() function. Currently it is blocking in my client, waiting for an ack.

Thanks for your input!


原文:https://stackoverflow.com/questions/16098026
更新时间:2022-07-16 16:07

最满意答案

对此最简单的解决方案是仅定义更改值的数组并使用FactoryGirl步骤中的数组

urls = [nil, "https://www.facebook.com/rihanna/videos/10155221800656676/", ...]
xs = [nil, 500, ...]
ys = [nil, 200, ...]
(0..4).each do |n|
  let!(:"deal1_step#{n}") {
    FactoryGirl.create(:step,
      video_url: urls[n],
      video_x: xs[n],
      video_y: ys[n],
      st_background_asset_filename: "campaigns/042017/assets_for_tests_and_dev/backgroundimage#{n}.jpg",  
      deal: deal1)
  }
end

对于其他任何事情你需要了解let / let! 作品。 let在示例中定义一个方法(由传入的符号命名)(它实际上在混合的模块上,效果相同),它会记忆传递给它的块的响应。 let! 调用let然后定义一个调用方法let的前块,以便在测试示例之前运行它。 这就是为什么你得到错误' deal1_step1在示例组上不可用(例如......),因为该方法是在示例上定义的,而不是在组上。

因此,如果您不想执行上面的数组示例之类的操作,那么您需要在一个在示例上下文中运行的块中进行属性更新,因此beforebefore - 就像

(0..4).each do |n|
    let!(:"deal1_step#{n}") {
      FactoryGirl.create(:step,
        order_nb: n,
        message_content: "this is message #{n}",        
       background_asset_filename: "campaigns/assets_for_tests/backgroundimage#{n}.jpg",  
        deal: deal)
    }
  end
before do 
  deal1_step1.video_url = "https://www.facebook.com/rihanna/videos/10155221800656676/"
  deal1_step1.video_x = 500
  deal1_step1.video_y = 500
  # may need to save deal1_step1 depending on exactly what you're looking for

  deal1_step2.update_attributes(video_url: "https://www.facebook.com/rihanna/videos/10155221800656676/", video_x: 500, video_y: 500 )

  ...
end

The easiest solution to this is to just define arrays of the changing values and use those in your FactoryGirl steps

urls = [nil, "https://www.facebook.com/rihanna/videos/10155221800656676/", ...]
xs = [nil, 500, ...]
ys = [nil, 200, ...]
(0..4).each do |n|
  let!(:"deal1_step#{n}") {
    FactoryGirl.create(:step,
      video_url: urls[n],
      video_x: xs[n],
      video_y: ys[n],
      st_background_asset_filename: "campaigns/042017/assets_for_tests_and_dev/backgroundimage#{n}.jpg",  
      deal: deal1)
  }
end

For anything else you need to understand how let/let! works. let defines a method (named by the symbol passed in) on the example (it's actually on a module that gets mixed in, same effect) which memoizes the response of the block passed to it. let! calls let and then defines a before block that calls the method let defined so it has already been run before the test example is. That's why you're getting the error 'deal1_step1 is not available on an example group (e.g. a ...', because the method is defined on the example, not on the group.

Therefore, if you don't want to do something like the arrays example above then you'd need to do your attribute updating in a block that gets run in the example context, so a before block - like

(0..4).each do |n|
    let!(:"deal1_step#{n}") {
      FactoryGirl.create(:step,
        order_nb: n,
        message_content: "this is message #{n}",        
       background_asset_filename: "campaigns/assets_for_tests/backgroundimage#{n}.jpg",  
        deal: deal)
    }
  end
before do 
  deal1_step1.video_url = "https://www.facebook.com/rihanna/videos/10155221800656676/"
  deal1_step1.video_x = 500
  deal1_step1.video_y = 500
  # may need to save deal1_step1 depending on exactly what you're looking for

  deal1_step2.update_attributes(video_url: "https://www.facebook.com/rihanna/videos/10155221800656676/", video_x: 500, video_y: 500 )

  ...
end

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)