草庐IT

networking - GoLang 监听端口范围

coder 2024-07-10 原文

我正在尝试让一个程序在 Go 中的端口范围(520 多个端口;TCP)上进行监听。

不过,好像不行。它会听一些然后停止。

我想做一个递增 1 的 for 循环,然后像这样调用一个监听函数:

for i := <beginning port>; i <= <ending port>; i++ {
    ipaddr := fmt.Sprintf("8.8.8.8:%d", i)
    ipaddrnew, _ := parseAddress(ipaddr)

    go listener(ipaddrnew)
}

这就是我一直在尝试做的,但在尝试了几次之后就停止了。

更新:

这是我刚刚运行的测试:

我将代码更改为以下内容:

for i := 14480; i <= 15000; i++ {
    ipaddr := fmt.Sprintf(":%d", i)
    fmt.Println(i)
    ipaddrnew, _ := parseAddress(ipaddr)
    go listener(ipaddrnew)
}

它给出了以下响应:

14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
[TCP] Listening for connections on :14483.
14496
[TCP] Listening for connections on :14481.
14497
14498
14499
14500
14501
14502
14503
14504
14505
[TCP] Listening for connections on :14482.
14506
14507
14508
14509
14510
14511
14512
[TCP] Listening for connections on :14480.
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
[TCP] Listening for connections on :14501.
14524
14525
14526
14527
14528
14529
14530
[TCP] Listening for connections on :14484.
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
[TCP] Listening for connections on :14485.
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
[TCP] Listening for connections on :14486.
14555
14556
14557
14558
14559
14560
[TCP] Listening for connections on :14487.
14561
14562
14563
14564
14565
14566
14567
14568
14569
[TCP] Listening for connections on :14488.
14570
14571
14572
14573
14574
14575
14576
[TCP] Listening for connections on :14489.
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
[TCP] Listening for connections on :14490.
14587
14588
14589
14590
14591
14592
[TCP] Listening for connections on :14491.
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
[TCP] Listening for connections on :14492.
14606
14607
14608
14609
14610
14611
14612
[TCP] Listening for connections on :14493.
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
[TCP] Listening for connections on :14494.
[TCP] Listening for connections on :14562.
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
[TCP] Listening for connections on :14502.
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
[TCP] Listening for connections on :14503.
14673
14674
[TCP] Listening for connections on :14495.
14675
14676
14677
14678
14679
14680
14681
14682
[TCP] Listening for connections on :14504.
14683
14684
14685
14686
14687
14688
[TCP] Listening for connections on :14505.
14689
14690
[TCP] Listening for connections on :14496.
14691
14692
14693
14694
14695
[TCP] Listening for connections on :14506.
14696
14697
14698
14699
14700
14701
14702
[TCP] Listening for connections on :14507.
14703
[TCP] Listening for connections on :14497.
14704
14705
14706
14707
14708
14709
14710
14711
14712
[TCP] Listening for connections on :14508.
14713
14714
14715
14716
14717
14718
14719
14720
[TCP] Listening for connections on :14509.
14721
14722
14723
14724
14725
14726
[TCP] Listening for connections on :14510.
14727
14728
14729
14730
14731
14732
14733
14734
[TCP] Listening for connections on :14511.
14735
14736
14737
14738
14739
[TCP] Listening for connections on :14512.
14740
14741
14742
14743
14744
14745
14746
14747
[TCP] Listening for connections on :14513.
14748
14749
14750
14751
14752
[TCP] Listening for connections on :14514.
14753
14754
14755
14756
14757
[TCP] Listening for connections on :14515.
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
[TCP] Listening for connections on :14516.
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
[TCP] Listening for connections on :14517.
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
[TCP] Listening for connections on :14518.
14843
14844
14845
14846
14847
14848
[TCP] Listening for connections on :14519.
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
[TCP] Listening for connections on :14520.
14860
14861
14862
14863
14864
[TCP] Listening for connections on :14521.
[TCP] Listening for connections on :14522.
14865
14866

最佳答案

Go 程序在 main 函数结束时结束。在 main 函数中无限期地等待。

func main() {
    go listen()
    select {}
}

关于networking - GoLang 监听端口范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38514058/

有关networking - GoLang 监听端口范围的更多相关文章

  1. ruby - 触发器 ruby​​ 中 3 点范围运算符和 2 点范围运算符的区别 - 2

    请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是

  2. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  3. ruby - 当使用::指定模块时,为什么 Ruby 不在更高范围内查找类? - 2

    我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or

  4. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

  5. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  6. Ruby 日期参数超出范围 - 2

    我正在尝试使用在我的代码中是动态的Time.local来安排时间。在每个月的第一天,我传递的值是Time.local(2009,9,-1,0)。在PHP中,这会将时间设置为上个月的最后一天。在ruby​​中,我只是得到“ArgumentError:参数超出范围”。是我用错了方法还是什么?谢谢。 最佳答案 您应该使用DateTime类而不是Time。(您可能需要先require'date'并安装activesupportgem。)它比Time更通用,并且可以用DateTime.civil(2009,9-1,-1,0)做你想做的事。为天

  7. ruby-on-rails - ruby 范围 : operators in case statement - 2

    我想检查my_number是否在某个范围内,包括较高的值。在IF语句中我会简单地使用“x>100&&x但是我应该在Ruby案例中做什么(开关)?使用:casemy_numberwhenmy_number不起作用。备注:标准范围不包括my_number恰好为500的情况,并且我不想添加第二个“when”,因为我必须编写双重内容casemy_number#between100and500when100..500puts"Correct,dosomething"when500puts"Correct,dosomethingagain"end 最佳答案

  8. Ruby - 从变量中获取位范围 - 2

    我有一个变量,想从该变量中获取一定范围的位。我想要最干净的方式来做到这一点。如果x=19767并且我想要bit3-bit8(从右边开始):100110100110111在二进制中是19767。我想要括号100110(100110)111中的部分,所以答案是38。用Ruby实现以下功能的最简单/最干净/最优雅的方法是什么?bit_range(orig_num,first_bit,last_bit)附言。计算强度较低的答案可加分。 最佳答案 19767.to_s(2)[-9..-4].to_i(2)或19767>>3&0x3f更新:从头

  9. ruby - 使用 Class.new 时访问外部范围 - 2

    是否有可能以某种方式访问​​Class.new范围内的a?a=5Class.new{defb;aend}.new.b#NameError:undefinedlocalvariableormethod`a'for#:0x007fa8b15e9af0>#:in`b' 最佳答案 即使@MarekLipka的回答是正确的——改变变量范围总是有风险的。这是可行的,因为每个block都带有创建它的上下文,因此您的局部变量a突然变得不那么局部了——它变成了一个“隐藏的”全局变量:a=5object=Class.new{define_method(

  10. ruby - private、protected 和 public 的范围 - 2

    在Ruby类定义中,private关键字在以下场景中的作用域是什么:classFoodefbar_publicputs"public"endprivatedefbar_privateputs"private"enddefbar_public_2puts"anotherpublic"endendprivate是否只作用于bar_private?还是在bar_public_2上? 最佳答案 在您的例子中,bar_private和bar_public_2都是私有(private)的。那是因为这两种方法都在private关键字的“范围内”。

随机推荐