简体   繁体   中英

Can't connect to tcp server more than 140 connections?

I am building a tcp socket server in golang. It works well with less than ~140 client connection. But if i try to set max connection number to 500, after 140th clients do not connect to the server.

I increased file descriptor count to 1048576 but it does not still work.

$ulimit -n
1048576

I think the problem comes from operation system.(server and clients work on same machine) So OS information:

Mac OS 10.12 Sierra 64 bit.

Does anyone have any idea why i can't increase tcp connection number?

github

I am on the same operating system as you, but I could not reproduce your issue. I am using Go version 1.7.4. I have not tested on Go 1.8, but it was released earlier today.

I created two files, server.go and client.go (reproduced below). When I run them, I get way more than 140 connections. Before running, I switched to root and setup the ulimit like so:

$ sudo -s
$ ulimit -n 10000

The client outputs:

Established 1 connections
...
Established 2971 connections
panic: dial tcp 127.0.0.1:1337: socket: too many open files in system

The server outputs something very similar.

Here is client.go:

package main

import (
    "fmt"
    "net"
)

func main() {
    var numConns int
    for {
        _, err := net.Dial("tcp", "127.0.0.1:1337")
        if err != nil {
            panic(err)
        }
        numConns++
        fmt.Println("Established", numConns, "connections")
    }
}

And server.go:

package main

import (
    "fmt"
    "net"
)

func main() {
    listener, err := net.Listen("tcp", ":1337")
    if err != nil {
        panic(err)
    }
    var numConns int
    for {
        _, err := listener.Accept()
        if err != nil {
            panic(err)
        }
        numConns++
        fmt.Println("Got", numConns, "connections")
    }
}

This is not a problem with the operating system, but the hardware. It is likely a problem with your router. Commercial routers do tend to fail when you get into that range. My guess is that Alex is testing at a large company or university where they have commercial grade routers, but you are testing at home.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM