3

I have to test a TCP connection timeout in golang. Right now this is what I'm doing to simulate a connection timeout so far,

    socket, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
    _ = syscall.Bind(socket, &syscall.SockaddrInet4{
    Port: 62433,
    })
    _ = syscall.Listen(socket, 1)

    // Block new connection by running out the backlog
    _, _ = net.Dial("tcp", "127.0.0.1:62433")

The idea is, only accept one connection in the backlog so after one connection when other clients try to connect with it will get a timeout. This approach works in macOS but it's not working in the Linux environment. Seems like in Linux the backlog value is not respected and allows multiple clients to connect.

I’m trying to find out a way to get more control to modify the TCP handshake. So I will open and listen to a port but added a custom delay or logic when doing the handshake with the client.

rakib
  • 131
  • 3
  • 10
  • 1
    Listen backlog in Linux is discussed [here](https://stackoverflow.com/questions/5111040/listen-ignores-the-backlog-argument) – Pak Uula Feb 06 '20 at 15:37
  • one way to trigger timeout on linux, although it requires running the test as root is setting an ip table drop rule for the port, as discussed [here](https://unix.stackexchange.com/questions/136642/how-to-make-a-tcp-socket-time-out) – hullarb Feb 07 '20 at 09:53

0 Answers0