EchoServer complete
This commit is contained in:
parent
8c17564504
commit
75c4a4c006
49
echo.go
Normal file
49
echo.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type EchoServer struct {
|
||||
port uint16
|
||||
}
|
||||
|
||||
func NewEchoServer(port uint16) *EchoServer {
|
||||
return &EchoServer{port}
|
||||
}
|
||||
|
||||
func (s *EchoServer) Run() {
|
||||
addr := fmt.Sprintf("0.0.0.0:%d", s.port)
|
||||
server, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
fmt.Println("Error listening:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
defer server.Close()
|
||||
fmt.Println("EchoServer waiting for client...")
|
||||
for {
|
||||
connection, err := server.Accept()
|
||||
if err != nil {
|
||||
fmt.Println("Error accepting: ", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("client connected")
|
||||
go s.processClient(connection)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EchoServer) processClient(connection net.Conn) {
|
||||
buffer := make([]byte, 1024)
|
||||
for {
|
||||
mLen, err := connection.Read(buffer)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading:", err.Error())
|
||||
break
|
||||
}
|
||||
fmt.Printf("Received %d bytes\n", mLen)
|
||||
_, err = connection.Write([]byte(buffer[:mLen]))
|
||||
}
|
||||
connection.Close()
|
||||
}
|
29
main.go
29
main.go
@ -1,7 +1,32 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"flag"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Server interface {
|
||||
Run()
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("hello world")
|
||||
var challenge int
|
||||
flag.IntVar(&challenge, "challenge",1, "Challenge number")
|
||||
flag.Parse()
|
||||
|
||||
var port uint16
|
||||
port = 13370
|
||||
var server Server
|
||||
switch challenge {
|
||||
case 0:
|
||||
server = NewEchoServer(port);
|
||||
case 1:
|
||||
server = NewPrimeServer(port);
|
||||
default:
|
||||
fmt.Printf("Unknown challenge\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
server.Run()
|
||||
}
|
49
prime.go
Normal file
49
prime.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type PrimeServer struct {
|
||||
port uint16
|
||||
}
|
||||
|
||||
func NewPrimeServer(port uint16) *PrimeServer {
|
||||
return &PrimeServer{port}
|
||||
}
|
||||
|
||||
func (s *PrimeServer) Run() {
|
||||
addr := fmt.Sprintf("0.0.0.0:%d", s.port)
|
||||
server, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
fmt.Println("Error listening:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
defer server.Close()
|
||||
fmt.Println("PrimeServer waiting for client...")
|
||||
for {
|
||||
connection, err := server.Accept()
|
||||
if err != nil {
|
||||
fmt.Println("Error accepting: ", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("client connected")
|
||||
go s.processClient(connection)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PrimeServer) processClient(connection net.Conn) {
|
||||
buffer := make([]byte, 1024)
|
||||
for {
|
||||
mLen, err := connection.Read(buffer)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading:", err.Error())
|
||||
break
|
||||
}
|
||||
fmt.Printf("Received %d bytes\n", mLen)
|
||||
_, err = connection.Write([]byte(buffer[:mLen]))
|
||||
}
|
||||
connection.Close()
|
||||
}
|
Loading…
Reference in New Issue
Block a user