simple p2p comms

This commit is contained in:
Alexander Matson 2019-04-30 18:14:28 -04:00
parent 59c4fc4251
commit 91327b1590
5 changed files with 205 additions and 0 deletions

35
daytime.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"fmt"
"net"
"os"
"time"
)
func main() {
service := ":1200"
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
daytime := "Hahahaha virgin client vs chad server" + time.Now().String()
conn.Write([]byte(daytime))
conn.Close()
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

53
echo.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"net"
"os"
"fmt"
)
func main() {
service := ":1201"
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleClient(conn)
}
}
func handleClient(conn net.Conn) {
// close connection on exit
defer conn.Close()
var buf [512]byte
for {
// read up to 512 bytes
n, err := conn.Read(buf[0:])
if err != nil {
return
}
// write the n bytes read
_, err2 := conn.Write(buf[0:n])
if err2 != nil {
return
}
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

26
ip.go Normal file
View File

@ -0,0 +1,26 @@
/* IP
*/
package main
import (
"net"
"os"
"fmt"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s ip-addr\n", os.Args[0])
os.Exit(1)
}
name := os.Args[1]
addr := net.ParseIP(name)
if addr == nil {
fmt.Println("Invalid address")
} else {
fmt.Println("The address is ", addr.String())
}
os.Exit(0)
}

BIN
p2p Executable file

Binary file not shown.

91
p2p.go Normal file
View File

@ -0,0 +1,91 @@
package main
import (
"net"
"os"
"fmt"
"strings"
)
func main() {
if len(os.Args) <= 1 {
fmt.Fprintf(os.Stderr, "Usage: %s <local node port> <remote node port>\n", os.Args[0])
os.Exit(1)
}
// Configure this local node's listening port.
service := ":" + os.Args[1]
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
// If a remote peer's port is specified, we want to connect and message it
if len(os.Args) == 3 {
fmt.Println("Found a remote peer")
remote := ":" + os.Args[2]
tcpRemote, err := net.ResolveTCPAddr("tcp4", remote)
checkError(err)
peerconn, err := net.DialTCP("tcp4", nil, tcpRemote)
checkError(err)
_, err = peerconn.Write([]byte("Hello from " + os.Args[1]))
checkError(err)
var buf [512]byte
n, err := peerconn.Read(buf[0:])
checkError(err)
result := string(buf[0:n])
fmt.Println(string(result))
}
fmt.Printf("(%s) Waiting for peer connections...\n", os.Args[1])
for {
conn, err := listener.Accept()
if err != nil {
continue
}
handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
// close connection on exit
defer conn.Close()
var buf [512]byte
for {
// read up to 512 bytes
n, err := conn.Read(buf[0:])
if err != nil {
return
}
message := string(buf[0:n])
fmt.Println(message)
tokens := strings.Split(message, " ")
if tokens[0] != "" {
response := "Hi " + tokens[2] + ", from " + os.Args[1]
// write the n bytes read
_, err2 := conn.Write([]byte(response))
if err2 != nil {
return
}
}
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}