Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 1 file changed
  • 0 comments
  • 1 contributor

Showing 1 changed file with 85 additions and 0 deletions. Show diff stats Hide diff stats

  1. 85  share.go
85  share.go
... ...
@@ -0,0 +1,85 @@
  1
+package main
  2
+
  3
+import (
  4
+	"io/ioutil"
  5
+	"log"
  6
+	"net"
  7
+	"os"
  8
+)
  9
+
  10
+const (
  11
+	DEFAULT_ADDRESS   = "127.0.0.1:8080"
  12
+	DEFAULT_MIME_TYPE = "text/plain"
  13
+)
  14
+
  15
+var (
  16
+	src  *os.File
  17
+	size int64
  18
+)
  19
+
  20
+func main() {
  21
+	log.Println("Start")
  22
+
  23
+	var oerr error
  24
+
  25
+	src, oerr = os.Open("test/7000.txt")
  26
+	if oerr != nil {
  27
+		log.Fatal("Error opening payload. ", oerr)
  28
+	}
  29
+
  30
+	fileinfo, serr := src.Stat()
  31
+	if serr != nil {
  32
+		log.Fatal("Error Stat on payload")
  33
+	}
  34
+	size = fileinfo.Size()
  35
+	log.Println("Payload size:", size)
  36
+
  37
+	// Warm up OS page cache
  38
+	ioutil.ReadAll(src)
  39
+	src.Seek(0, os.SEEK_SET)
  40
+
  41
+	sock, lerr := net.Listen("tcp", DEFAULT_ADDRESS)
  42
+	if lerr != nil {
  43
+		log.Fatal("Error listening on ", DEFAULT_ADDRESS, ". ", lerr)
  44
+	}
  45
+
  46
+	for {
  47
+		conn, aerr := sock.Accept()
  48
+
  49
+		if aerr != nil {
  50
+			log.Fatal("Error Accept. ", aerr)
  51
+		}
  52
+
  53
+		go handle(conn)
  54
+	}
  55
+}
  56
+
  57
+func handle(conn net.Conn) {
  58
+
  59
+	var pos int64
  60
+	buf := make([]byte, 32*1024)
  61
+
  62
+	for pos < size {
  63
+
  64
+		read, rerr := src.ReadAt(buf, pos)
  65
+		log.Println("Read bytes:", read)
  66
+		log.Println("Err:", rerr)
  67
+
  68
+		pos += int64(read)
  69
+
  70
+		wrote, werr := conn.Write(buf[:read])
  71
+		log.Println("Wrote bytes:", wrote)
  72
+		log.Println("Err:", werr)
  73
+	}
  74
+
  75
+	/*
  76
+		written, err := io.Copy(conn, src)
  77
+		src.Seek(0, os.SEEK_SET) // Rewind for next connection
  78
+
  79
+		if err != nil {
  80
+			log.Fatal("Error copying to socket. ", err)
  81
+		}
  82
+		log.Println("Copied", written, "bytes to socket")
  83
+	*/
  84
+	conn.Close()
  85
+}

No commit comments for this range

Something went wrong with that request. Please try again.