init
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
all: clean build
|
||||
|
||||
clean:
|
||||
rm speak reply
|
||||
|
||||
build:
|
||||
go build speak.go
|
||||
go build reply.go
|
||||
@@ -0,0 +1,23 @@
|
||||
# ==============================================================================
|
||||
# Auth: Alex Celani
|
||||
# File: convo.txt
|
||||
# Revn: 05-10-2022 0.1
|
||||
# Func: elaborate on how the convo Golang networks programs should run
|
||||
#
|
||||
# TODO: create
|
||||
# ==============================================================================
|
||||
# CHANGE LOG
|
||||
# ------------------------------------------------------------------------------
|
||||
# 05-10-2022: init
|
||||
#
|
||||
# ==============================================================================
|
||||
|
||||
|
||||
uh
|
||||
|
||||
just send a bunch of information back and forth
|
||||
|
||||
A sends data to B, B receives, processes, and responds
|
||||
A receives, processes, and responds
|
||||
|
||||
Repeat until some cutoff condition
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,84 @@
|
||||
// =============================================================================
|
||||
// Auth: Alex Celani
|
||||
// File: reply.go
|
||||
// Revn: 05-13-2022 0.1
|
||||
// Func: host connection, reply to speak.go
|
||||
//
|
||||
// TODO: create
|
||||
// =============================================================================
|
||||
// CHANGE LOG
|
||||
// -----------------------------------------------------------------------------
|
||||
// 05-13-2022: init
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
func check( err error ) {
|
||||
if err != nil {
|
||||
fmt.Println( "Fatal: ", err.Error() )
|
||||
os.Exit( 1 )
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func handle( conn net.Conn ) {
|
||||
|
||||
table := map[string]string{ "hello":"hello",
|
||||
"how are you":"fine and yourself",
|
||||
"well enough for the weather":"aye it scalds",
|
||||
"i lost my begonias":"exit" }
|
||||
var buffer [512]byte
|
||||
for {
|
||||
n, err := conn.Read( buffer[:] )
|
||||
check( err )
|
||||
|
||||
fmt.Println( "\t\t\t", string( buffer[:n] ) )
|
||||
|
||||
time.Sleep( 2 * time.Second )
|
||||
|
||||
response, present := table[string( buffer[:n] )]
|
||||
if present {
|
||||
_, err = conn.Write( []byte( response ) )
|
||||
check( err )
|
||||
|
||||
|
||||
if response == "exit" {
|
||||
break
|
||||
} else {
|
||||
fmt.Println( response )
|
||||
}
|
||||
} else {
|
||||
fmt.Println( "excuse me good sir" )
|
||||
os.Exit( 2 )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
service := ":1200"
|
||||
|
||||
addr, err := net.ResolveTCPAddr( "tcp", service )
|
||||
check( err )
|
||||
|
||||
listener, err := net.ListenTCP( "tcp", addr )
|
||||
|
||||
conn, err := listener.Accept()
|
||||
check( err )
|
||||
|
||||
handle( conn )
|
||||
|
||||
os.Exit( 0 )
|
||||
|
||||
}
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,85 @@
|
||||
// =============================================================================
|
||||
// Auth: Alex Celani
|
||||
// File: speak.go
|
||||
// Revn: 05-11-2022 0.1
|
||||
// Func: initiate conversation
|
||||
//
|
||||
// TODO: create
|
||||
// =============================================================================
|
||||
// CHANGE LOG
|
||||
// -----------------------------------------------------------------------------
|
||||
// 05-11-2022: init
|
||||
// 05-17-2022: changed an entry in the response table
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
func check( err error ) {
|
||||
if err != nil {
|
||||
fmt.Println( "fatal: ", err.Error() )
|
||||
os.Exit( 1 )
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func handle( conn net.Conn ) {
|
||||
|
||||
table := map[string]string{ "hello":"how are you",
|
||||
"fine and yourself":"well enough for the weather",
|
||||
"aye it scalds":"i lost my begonias" }
|
||||
var buffer [512]byte
|
||||
for {
|
||||
n, err := conn.Read( buffer[:] )
|
||||
check( err )
|
||||
|
||||
if string( buffer[:n] ) == "exit" {
|
||||
break
|
||||
}
|
||||
|
||||
fmt.Println( "\t\t\t", string( buffer[:n] ) )
|
||||
|
||||
time.Sleep( 2 * time.Second )
|
||||
|
||||
response, present := table[string( buffer[:n] )]
|
||||
if present {
|
||||
_, err = conn.Write( []byte( response ) )
|
||||
check( err )
|
||||
|
||||
fmt.Println( response )
|
||||
} else {
|
||||
fmt.Println( "excuse me good sir" )
|
||||
os.Exit( 2 )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
service := ":1200"
|
||||
|
||||
addr, err := net.ResolveTCPAddr( "tcp", service )
|
||||
check( err )
|
||||
|
||||
conn, err := net.DialTCP( "tcp", nil, addr )
|
||||
check( err )
|
||||
|
||||
_, err = conn.Write( []byte( "hello" ) )
|
||||
check( err )
|
||||
fmt.Println( "hello" )
|
||||
|
||||
handle( conn )
|
||||
|
||||
os.Exit( 0 )
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user