GoLang监听端口范围

I'm trying to make a program listen on a port range in Go (520+ ports;TCP).

However, it doesn't seem to work. It listens on a few and then stops.

I was thinking to make a for loop increasing by one and then calling a listening function like so:

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

    go listener(ipaddrnew)
}

That's what I've been trying to do but it stops after a few go through.

Update:

Here's a test I just ran:

I change the code to the following:

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

and it gave the following response:

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
dongyang448966
dongyang448966 文件描述符为1000000;是的,如果它无法绑定到侦听器,我正在检查并详细检查错误;但是,这在我正在调用的函数中(侦听器),由于某种原因,它并不是在for循环中每次都调用该函数,这就是问题所在。
大约 4 年之前 回复
down00112
down00112 创建侦听器时是否正在检查错误?您的文件描述符用完了吗?
大约 4 年之前 回复

1个回答



main </ code>函数结束时,Go程序结束。 只需无限期地等待main函数。</ p>

  func main(){
go listen()
选择{}
}
</ code> </ pre>

</ div>

展开原文

原文

Go program ends when main function ends. Just wait indefinitely in main function.

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

douyan6742
douyan6742 没有帮助。 我在主要功能的末尾添加了它; 但是,即使这样,主函数也不是调用go例程进行“侦听”的函数
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐