我正在构建一个与多个发起者一起使用的回声算法,本质上我有三个函数“talk”、“listen”和“run algorithm”。 “talk”向邻居发送数据,“listen”从邻居接收数据,这两者都使用 channel 。这些 channel 在“运行算法”函数中被监控,该函数决定发送什么以及发送给谁。
本质上是这样的:
func server(s Node, myChannel chan string){
fmt.Printf("SERVER Launching server... %s:%s \n", s.ip, s.port)
// Start listening
// We do not stop listening until the surrounding function returns (finishes)
ln, _ := net.Listen("tcp", s.ip+":"+s.port)
for {
// Accept connections
fmt.Println("SERVER waiting at ln.Accept()")
conn, err := ln.Accept()
if err != nil {
panic(err)
}
//fmt.Println("SERVER connection accepted")
// Read received message
fmt.Println("SERVER awaiting message")
message, err := bufio.NewReader(conn).ReadString('\n')
fmt.Println("SERVER message received = " + message)
if err != nil {
fmt.Println("the server is broken")
panic(err)
}
if err == nil {
// If the mmessage is not empty, then print it in the shell
if string(message) != "" {
fmt.Println("SERVER message put in channel")
myChannel <- message
}
}
}
说话的功能是这样的:
func client(myNode Node, nbour Node, myChannel chan string) {
for {
// this loop controls the sending of algorithm messages
conn, err = net.Dial("tcp", nbour.ip+":"+nbour.port)
// useful info for the user
//fmt.Println("CLIENT awaiting message to send")
// the the message to send out of the channel
messageToSend := <-myChannel
// send the message
conn.Write([]byte(messageToSend))
fmt.Println("CLIENT SENT ALG MESSAGE = " + messageToSend + "to " + nbour.ip + ":" + nbour.port)
// useful information for the user
//fmt.Printf("CLIENT Message text: " + messageToSend. + "\n")
}
问题是这些连接会在前几条消息中找到,然后在某个任意点一个节点发送一条消息,而邻居没有收到它,一切都停止了。
因此我的问题是为什么连接会任意停止,是否有设置告诉他们保持打开状态多长时间,或者要发送多少消息?或者上面的代码在实现这个目标方面是错误的(尽管就像我说的那样有时它工作得很好,事实上有时算法完美地完成,然后我再次运行完全相同的代码并且它卡住了等待消息到达)。
最佳答案
因此,问题似乎是建立连接然后等待将某些内容放入 channel 。如果您改为等待将某些内容放入 channel ,那么(一旦从 channel 中删除了某些内容)建立连接(并按照@leaf bebop 的建议关闭连接)代码每次都运行良好。 (见下文)
func client(myNode Node, nbour Node, myChannel chan string) {
for {
// this loop controls the sending of algorithm messages
// useful info for the user
//fmt.Println("CLIENT awaiting message to send")
// the message to send out of the channel
messageToSend := <-myChannel
conn, err = net.Dial("tcp", nbour.ip+":"+nbour.port)
// send the message
conn.Write([]byte(messageToSend))
conn.Close()
fmt.Println("CLIENT SENT ALG MESSAGE = " + messageToSend + "to " + nbour.ip + ":" + nbour.port)
// useful information for the user
//fmt.Printf("CLIENT Message text: " + messageToSend. + "\n")
关于go - Conn 在 Go 中随机关闭且没有警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54885313/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und
下面的代码在我第一次运行它时就可以正常工作:require'rubygems'require'spreadsheet'book=Spreadsheet.open'/Users/me/myruby/Mywks.xls'sheet=book.worksheet0row=sheet.row(1)putsrow[1]book.write'/Users/me/myruby/Mywks.xls'当我再次运行它时,我会收到更多消息,例如:/Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:11
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle