Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: grahamking/netshare
base: efa80f9fcd
...
head fork: grahamking/netshare
compare: df2111b42e
  • 1 commit
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 17, 2012
@grahamking Started on a Go version df2111b
Showing with 85 additions and 0 deletions.
  1. +85 −0 share.go
View
85 share.go
@@ -0,0 +1,85 @@
+package main
+
+import (
+ "io/ioutil"
+ "log"
+ "net"
+ "os"
+)
+
+const (
+ DEFAULT_ADDRESS = "127.0.0.1:8080"
+ DEFAULT_MIME_TYPE = "text/plain"
+)
+
+var (
+ src *os.File
+ size int64
+)
+
+func main() {
+ log.Println("Start")
+
+ var oerr error
+
+ src, oerr = os.Open("test/7000.txt")
+ if oerr != nil {
+ log.Fatal("Error opening payload. ", oerr)
+ }
+
+ fileinfo, serr := src.Stat()
+ if serr != nil {
+ log.Fatal("Error Stat on payload")
+ }
+ size = fileinfo.Size()
+ log.Println("Payload size:", size)
+
+ // Warm up OS page cache
+ ioutil.ReadAll(src)
+ src.Seek(0, os.SEEK_SET)
+
+ sock, lerr := net.Listen("tcp", DEFAULT_ADDRESS)
+ if lerr != nil {
+ log.Fatal("Error listening on ", DEFAULT_ADDRESS, ". ", lerr)
+ }
+
+ for {
+ conn, aerr := sock.Accept()
+
+ if aerr != nil {
+ log.Fatal("Error Accept. ", aerr)
+ }
+
+ go handle(conn)
+ }
+}
+
+func handle(conn net.Conn) {
+
+ var pos int64
+ buf := make([]byte, 32*1024)
+
+ for pos < size {
+
+ read, rerr := src.ReadAt(buf, pos)
+ log.Println("Read bytes:", read)
+ log.Println("Err:", rerr)
+
+ pos += int64(read)
+
+ wrote, werr := conn.Write(buf[:read])
+ log.Println("Wrote bytes:", wrote)
+ log.Println("Err:", werr)
+ }
+
+ /*
+ written, err := io.Copy(conn, src)
+ src.Seek(0, os.SEEK_SET) // Rewind for next connection
+
+ if err != nil {
+ log.Fatal("Error copying to socket. ", err)
+ }
+ log.Println("Copied", written, "bytes to socket")
+ */
+ conn.Close()
+}

No commit comments for this range

Something went wrong with that request. Please try again.