Browse Source

server sending messages to multiple clients works fine

master
arnaucode 7 years ago
parent
commit
c92299f831
2 changed files with 110 additions and 30 deletions
  1. +9
    -2
      client/main.go
  2. +101
    -28
      server/main.go

+ 9
- 2
client/main.go

@ -5,12 +5,19 @@ import (
"fmt"
"net"
"os"
"time"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
func main() { //client https://systembash.com/a-simple-go-tcp-server-and-tcp-client/
// connect to this socket
conn, _ := net.Dial("tcp", "127.0.0.1:3333")
conn, _ := net.Dial(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err)
@ -20,7 +27,7 @@ func main() { //client https://systembash.com/a-simple-go-tcp-server-and-tcp-cli
ip := getIp()
fmt.Println("local ip:", ip)
// send to socket
fmt.Fprintf(conn, hostname+", ip: "+ip.String()+"\n")
fmt.Fprintf(conn, "[client connecting] - [date]: "+time.Now().Local().Format(time.UnixDate)+" - [hostname]: "+hostname+", [ip]: "+ip.String()+"\n")
for {
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {

+ 101
- 28
server/main.go

@ -5,6 +5,8 @@ import (
"fmt"
"net"
"os"
"strconv"
"strings"
)
const (
@ -13,43 +15,114 @@ const (
CONN_TYPE = "tcp"
)
func main() { //server
var allClients map[*Client]int
fmt.Println("Launching server...")
type Client struct {
outgoing chan string
reader *bufio.Reader
writer *bufio.Writer
conn net.Conn
connection *Client
}
func (client *Client) Read() {
for {
line, err := client.reader.ReadString('\n')
if err == nil {
if client.connection != nil {
client.connection.outgoing <- line
}
fmt.Println("")
fmt.Println(line)
} else {
break
}
// listen on all interfaces
ln, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer ln.Close()
defer fmt.Println("ended")
client.conn.Close()
delete(allClients, client)
if client.connection != nil {
client.connection.connection = nil
}
client = nil
}
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
func (client *Client) Write() {
for data := range client.outgoing {
client.writer.WriteString(data)
client.writer.Flush()
}
}
func (client *Client) Listen() {
go client.Read()
go client.Write()
}
func NewClient(connection net.Conn) *Client {
writer := bufio.NewWriter(connection)
reader := bufio.NewReader(connection)
client := &Client{
// incoming: make(chan string),
outgoing: make(chan string),
conn: connection,
reader: reader,
writer: writer,
}
client.Listen()
return client
}
func watchConsoleInput(conn net.Conn) {
newcommand := bufio.NewReader(os.Stdin)
for {
text, _ := newcommand.ReadString('\n')
text = strings.TrimSpace(text)
// send newcommand back to client
if text != "" {
fmt.Println("console input command: " + text)
fmt.Println("sending command to " + strconv.Itoa(len(allClients)) + " connected clients")
for clientList, _ := range allClients {
//if clientList.connection == nil {
clientList.conn.Write([]byte(text + "\n"))
//}
}
fmt.Print("enter new command: ")
}
}
}
func watchConnectionInput(client *Client) {
for clientList, _ := range allClients {
if clientList.connection == nil {
client.connection = clientList
clientList.connection = client
fmt.Println("Connected")
}
}
// accept connection on port
c, err := ln.Accept()
allClients[client] = 1
fmt.Println(strconv.Itoa(len(allClients)) + " connected clients")
fmt.Print("command to send to the clients: ")
}
func main() {
allClients = make(map[*Client]int)
listener, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error accepting: ", err.Error())
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
// will listen for message to process ending in newline (\n)
message, _ := bufio.NewReader(c).ReadString('\n')
// output message received
fmt.Print("client connected:", string(message), "\n")
for {
/*// will listen for message to process ending in newline (\n)
message, _ := bufio.NewReader(c).ReadString('\n')
// output message received
fmt.Print("client connected:", string(message), "\n")*/
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
newcommand := bufio.NewReader(os.Stdin)
fmt.Print("command to send to the clients: ")
text, _ := newcommand.ReadString('\n')
// send new string back to client
c.Write([]byte(text + "\n"))
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println(err.Error())
}
client := NewClient(conn)
go watchConnectionInput(client)
go watchConsoleInput(client.conn)
}
}

Loading…
Cancel
Save