Skip to content

Commit

Permalink
fix: RX/TX implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed Jul 9, 2021
1 parent 9fa2d72 commit 89c2353
Showing 1 changed file with 22 additions and 35 deletions.
57 changes: 22 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -49,13 +47,27 @@ func printDataOut() {
}
}

func handleRequest(conn net.Conn) {
func handleTX(conn net.Conn, b []byte) error {
defer conn.Close()
n, err := io.Copy(ioutil.Discard, conn)
if err != nil {
return
for {
n, err := conn.Write(b)
if err != nil {
return err
}
atomic.AddUint64(&dataOut, uint64(n))
}
}

func handleRX(conn net.Conn) {
defer conn.Close()
b := make([]byte, oneMB)
for {
n, err := conn.Read(b)
if err != nil {
return
}
atomic.AddUint64(&dataIn, uint64(n))
}
atomic.AddUint64(&dataIn, uint64(n))
}

func runServer() {
Expand All @@ -71,7 +83,7 @@ func runServer() {
log.Fatal(err)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
go handleRX(conn)
}
}

Expand All @@ -85,35 +97,10 @@ func runClient(host string) {
continue
}
fmt.Println(host, ": connected")
for {
n, err := conn.Write(b)
if err != nil {
conn.Close()
fmt.Println(host, ": disconnected")
break
}
atomic.AddUint64(&dataOut, uint64(n))
if err := handleTX(conn, b); err != nil {
fmt.Println(host, ": disconnected")
}
}
for i := 0; i < 16; i++ {
go func() {
for {
conn, err := net.Dial("tcp", host)
if err != nil {
time.Sleep(time.Second)
continue
}
for {
n, err := conn.Write(b)
if err != nil {
conn.Close()
break
}
atomic.AddUint64(&dataOut, uint64(n))
}
}
}()
}
}

func main() {
Expand Down

0 comments on commit 89c2353

Please sign in to comment.