New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/cohhei/tcp

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/cohhei/tcp

  • v0.0.0-20200320070140-a1cbe5bccea2
  • Source
  • Go
  • Socket score

Version published
Created
Source

Too naive HTTP client and server

cohhei/http is a too simple and naive HTTP client and server library that doesn't depend on net/http. This client has only one method, Do which sends an HTTP request.

Usage

HTTP client

package main

import (
	"fmt"
	"io"
	"log"
	"os"

	"github.com/cohhei/http"
)

func main() {
	// Create a client.
	c := http.NewClient()

	// Create a request.
	req := &http.Request{
		Method: "GET",
		Host:   "example.com",
		Path:   "/index.html",
		Header: Header{},
	}

	// Send the request.
	resp, err := c.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp)
	io.Copy(os.Stdout, resp.Body)
}

HTTP server

package main

import (
	"io"
	"log"
	"os"

	"github.com/cohhei/http"
)

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	http.HandleFunc("/", func(w io.Writer, r io.Reader) {
		io.Copy(os.Stderr, r)
		w.Write([]byte("HTTP/1.1 200 OK\naaaaa: bbbbbb\n\nHello World!"))
	})
	log.Print("http://127.0.0.1:8080/")
	if err := http.ListenAndServe(8080); err != nil {
		log.Fatal(err)
	}
}

Too naive TCP client

This package has a TCP client that depends on only syscall.

TCP client usage

// Create a client
c := http.NewTCPClient()

// Connect to the address
ip := [4]byte{127, 0, 0, 1}
port := 11111
if err := c.Connect(ip, port); err != nil {
  	log.Fatal(err)
}

// Read data
resp, err := c.Read()
if err != nil {
	log.Fatal(err)
}
io.Copy(os.Stdout, resp)

// Write data
n, err := c.Write([]byte("world"))
if err != nil {
	log.Fatal(err)
}

Too naive UDP client

This package has a UDP client that depends on only syscall.

UDP client usage

c := http.NewUDPClient()
ip := [4]byte{8, 8, 8, 8}
port := 53
b := []byte{0x01}
if err := c.SendTo(b, ip, port); err != nil {
	log.Fatal(err)
}
defer c.Close()

resp, err := c.Recvfrom()
if err != nil {
	log.Fatal(err)
}

io.Copy(os.Stdout, resp)

FAQs

Package last updated on 20 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc