Advertisement
Ivanezko

Untitled

Dec 5th, 2019
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.67 KB | None | 0 0
  1. // This is a trivial TCP server leaking sockets.
  2.  
  3. package main
  4.  
  5. import (
  6.     "fmt"
  7.     "net"
  8.     "time"
  9.     //"io"
  10. )
  11.  
  12. func handle(conn net.Conn, i int) {
  13.     defer conn.Close()
  14.     //io.Copy(conn, conn)
  15.     for {
  16.         fmt.Printf("[%d]",i)
  17.         time.Sleep(time.Second)
  18.     }
  19. }
  20.  
  21. func main() {
  22.     IP := ""
  23.     Port := 5000
  24.     listener, err := net.Listen("tcp4", fmt.Sprintf("%s:%d", IP, Port))
  25.     if err != nil {
  26.         panic(err)
  27.     }
  28.  
  29.     i := 0
  30.  
  31.     for {
  32.         fmt.Println("\nstart accepting")
  33.         if conn, err := listener.Accept(); err == nil {
  34.             fmt.Printf("\naccepted %d\n", i)
  35.             i += 1
  36.             if i < 800 {
  37.                 go handle(conn, i)
  38.             } else {
  39.                 conn.Close()
  40.             }
  41.         } else {
  42.             panic(err)
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement