Skip to content

A little wrapper to make it easier to get a socket's tcpinfo stats in go

License

Notifications You must be signed in to change notification settings

LinkShen/go-tcpinfo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-tcpinfo

This is a small wrapper around the syscall library, so you don't have to mess around with it whenever you want to get tcp info for a connection

Usage

tcpInfo, err := tcpinfo.GetsockoptTCPInfo(&conn)
if err != nil {
    panic(err)
}

Example

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"sync"

	"github.com/brucespang/go-tcpinfo"
)

func handleConn(conn net.Conn) {
	io.Copy(ioutil.Discard, conn)
}

func server(wg *sync.WaitGroup) {
	ln, err := net.Listen("tcp", ":8000")
	if err != nil {
		panic(err)
	}

	wg.Done()

	// accept connection on port
	for {
		conn, err := ln.Accept()
		if err != nil {
			panic(err)
		}

		go handleConn(conn)
	}
}

func client() {
	conn, err := net.Dial("tcp", "127.0.0.1:8000")
	if err != nil {
		panic(err)
	}

	go io.Copy(ioutil.Discard, conn)

	_, err = conn.Write([]byte("hihihihihihihi"))
	if err != nil {
		panic(err)
	}

	tcpInfo, err := tcpinfo.GetsockoptTCPInfo(conn.(*net.TCPConn))
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", tcpInfo)
}

func main() {
	wg := sync.WaitGroup{}
	wg.Add(1)
	go server(&wg)
	wg.Wait()
	client()
}

About

A little wrapper to make it easier to get a socket's tcpinfo stats in go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%