Skip to content

Instantly share code, notes, and snippets.

@hiyosi
Last active February 12, 2023 23:56
Show Gist options
  • Save hiyosi/e4bd0cb5d741109989babf3bbe02095a to your computer and use it in GitHub Desktop.
Save hiyosi/e4bd0cb5d741109989babf3bbe02095a to your computer and use it in GitHub Desktop.
conn err memo

server.go

package main

import (
   "fmt"
   "log"
   "net"
   "time"
)

func main() {
   s, err := net.Listen("tcp", "127.0.0.1:44444")
   if err != nil {
      log.Fatal(err)
   }
   defer func() {
      fmt.Println("CLOSING LISTENER...")
      s.Close()
   }()
   fmt.Println("LISTENING...")

   c, err := s.Accept()
   if err != nil {
      log.Fatal(err)
   }
   fmt.Println("ACCEPTED")

   fmt.Println("WAITING...")
   time.Sleep(10 * time.Second)

   fmt.Println("CLOSING CONNECTION...")
   c.Close()
}

client.go

package main

import (
   "fmt"
   "log"
   "net"
   "time"
)

func main() {
   c, err := net.Dial("tcp", "127.0.0.1:44444")
   if err != nil {
      log.Fatal(err)
   }
   fmt.Println("CONNECTED")

   fmt.Println("SENDING MESSAGES...")
   _, err = c.Write([]byte("Hello"))
   if err != nil {
      log.Fatal(err)
   }

   fmt.Println("WAITING...")
   time.Sleep(30 * time.Second)

   fmt.Println("RECEIVING MESSAGES...")
   buffer := make([]byte, 1024)
   n, err := c.Read(buffer)
   if err != nil {
      log.Fatal(err)
   }
   fmt.Println(string(buffer[:n]))
   fmt.Println("RECEIVED")

   fmt.Println("CLOSING CONNECTION...")
   c.Close()
}
$ go run server.go
$ go run client.go
CONNECTED
SENDING MESSAGES...
WAITING...
RECEIVING MESSAGES...
2023/02/10 11:11:12 read tcp 127.0.0.1:53071->127.0.0.1:44444: read: connection reset by peer
exit status 1

tcpdump

$ sudo tcpdump -i lo0 port 44444
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo0, link-type NULL (BSD loopback), capture size 262144 bytes
11:04:52.674363 IP localhost.52852 > localhost.44444: Flags [S], seq 2513011824, win 65535, options [mss 16344,nop,wscale 6,nop,nop,TS val 2905899125 ecr 0,sackOK,eol], length 0
11:04:52.674634 IP localhost.44444 > localhost.52852: Flags [S.], seq 605264231, ack 2513011825, win 65535, options [mss 16344,nop,wscale 6,nop,nop,TS val 2862457234 ecr 2905899125,sackOK,eol], length 0
11:04:52.674664 IP localhost.52852 > localhost.44444: Flags [.], ack 1, win 6379, options [nop,nop,TS val 2905899125 ecr 2862457234], length 0
11:04:52.674672 IP localhost.44444 > localhost.52852: Flags [.], ack 1, win 6379, options [nop,nop,TS val 2862457234 ecr 2905899125], length 0
11:04:52.674745 IP localhost.52852 > localhost.44444: Flags [P.], seq 1:6, ack 1, win 6379, options [nop,nop,TS val 2905899125 ecr 2862457234], length 5
11:04:52.674764 IP localhost.44444 > localhost.52852: Flags [.], ack 6, win 6379, options [nop,nop,TS val 2862457234 ecr 2905899125], length 0
11:05:07.675240 IP localhost.44444 > localhost.52852: Flags [R.], seq 1, ack 6, win 6379, length 0

if the server read data before closing connection. tcpdump

11:21:01.147502 IP localhost.53219 > localhost.44444: Flags [S], seq 1985085055, win 65535, options [mss 16344,nop,wscale 6,nop,nop,TS val 369424974 ecr 0,sackOK,eol], length 0
11:21:01.147737 IP localhost.44444 > localhost.53219: Flags [S.], seq 1028179937, ack 1985085056, win 65535, options [mss 16344,nop,wscale 6,nop,nop,TS val 2507778407 ecr 369424974,sackOK,eol], length 0
11:21:01.147750 IP localhost.53219 > localhost.44444: Flags [.], ack 1, win 6379, options [nop,nop,TS val 369424974 ecr 2507778407], length 0
11:21:01.147760 IP localhost.44444 > localhost.53219: Flags [.], ack 1, win 6379, options [nop,nop,TS val 2507778407 ecr 369424974], length 0
11:21:01.147826 IP localhost.53219 > localhost.44444: Flags [P.], seq 1:6, ack 1, win 6379, options [nop,nop,TS val 369424974 ecr 2507778407], length 5
11:21:01.147844 IP localhost.44444 > localhost.53219: Flags [.], ack 6, win 6379, options [nop,nop,TS val 2507778407 ecr 369424974], length 0
11:21:11.149287 IP localhost.44444 > localhost.53219: Flags [F.], seq 1, ack 6, win 6379, options [nop,nop,TS val 2507788409 ecr 369424974], length 0
11:21:11.149364 IP localhost.53219 > localhost.44444: Flags [.], ack 2, win 6379, options [nop,nop,TS val 369434976 ecr 2507788409], length 0
11:21:26.176629 IP localhost.44444 > localhost.53219: Flags [.], ack 6, win 6379, length 0
11:21:26.176639 IP localhost.53219 > localhost.44444: Flags [.], ack 2, win 6379, length 0
11:21:26.176665 IP localhost.53219 > localhost.44444: Flags [.], ack 2, win 6379, options [nop,nop,TS val 369450003 ecr 2507788409], length 0
11:21:26.176673 IP localhost.44444 > localhost.53219: Flags [.], ack 6, win 6379, options [nop,nop,TS val 2507803436 ecr 369434976], length 0
11:21:31.150202 IP localhost.53219 > localhost.44444: Flags [F.], seq 6, ack 2, win 6379, options [nop,nop,TS val 369454977 ecr 2507803436], length 0
11:21:31.150322 IP localhost.44444 > localhost.53219: Flags [.], ack 7, win 6379, options [nop,nop,TS val 2507808410 ecr 369454977], length 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment