sendfile

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 3 Imported by: 4

README

sendfile

PkgGoDev Build Status codecov Go Report Card LICENSE

Package sendfile wraps the sendfile system call.

Get started

Install
go get github.com/hslam/sendfile
Import
import "github.com/hslam/sendfile"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/sendfile"
	"net"
	"os"
)

func main() {
	srcName := "srcfile"
	srcFile, err := os.Create(srcName)
	if err != nil {
		panic(err)
	}
	defer os.Remove(srcName)
	defer srcFile.Close()
	contents := "Hello world"
	srcFile.Write([]byte(contents))
	lis, err := net.Listen("tcp", ":9999")
	if err != nil {
		panic(err)
	}
	defer lis.Close()
	done := make(chan bool)
	go func() {
		conn, _ := lis.Accept()
		defer conn.Close()
		buf := make([]byte, len(contents))
		n, _ := conn.Read(buf)
		fmt.Println(string(buf[:n]))
		close(done)
	}()
	conn, _ := net.Dial("tcp", "127.0.0.1:9999")
	if _, err = sendfile.SendFile(conn, int(srcFile.Fd()), 0, int64(len(contents))); err != nil {
		fmt.Println(err)
	}
	conn.Close()
	<-done
}
Output
Hello world
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

sendfile was written by Meng Huang.

Documentation

Overview

Package sendfile wraps the sendfile system call.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendFile

func SendFile(conn net.Conn, src int, pos, remain int64) (written int64, err error)

SendFile wraps the sendfile system call.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL