reader := bufio.NewReader(conn)
ILOOP:
for {
text, err := reader.ReadString('\n')
switch err {
case io.EOF:
pool.DeleteClient(client)
log.Printf("Перезапуск цикла")
break ILOOP
case nil:
incomingClientMsg <- text
default:
log.Printf("Ошибка при получении:%s", err)
//return
}
}
package main
import (
"fmt"
"log"
"net"
"strconv"
"strings"
"sync"
"time"
)
var commandCounter int
var commandCounterEnter int
var command map[int]string
var mutex *sync.Mutex
func SocketClient(ip string, port int) {
addr := strings.Join([]string{ip, strconv.Itoa(port)}, ":")
conn, err := net.Dial("tcp", addr)
defer conn.Close()
if err != nil {
log.Fatalln(err)
}
//conn.Write([]byte(message))
//log.Printf("Send: %s", message)
buff := make([]byte, 1024)
n, _ := conn.Read(buff)
//добавляем команду в очередь
mutex.Lock()
commandCounter = commandCounter + 1
mutex.Unlock()
mutex.Lock()
command[commandCounter] = fmt.Sprintf("%s", buff[:n])
mutex.Unlock()
SocketClient(ip, port)
}
func main() {
mutex = &sync.Mutex{}
log.Printf("Started")
command = make(map[int]string)
go func() {
i := 1
for {
if i <= commandCounter {
if commandCounterEnter < i {
commandCounterEnter = i
mutex.Lock()
log.Print(command[i])
mutex.Unlock()
i = i + 1
} else {
time.Sleep(1 * time.Minute)
}
} else {
time.Sleep(1 * time.Minute)
}
}
}()
var (
ip = "46.63.122.27"
port = 3333
)
SocketClient(ip, port)
}
package main
import (
"log"
"net"
"strconv"
"strings"
)
func main() {
Client("46.63.122.27", 6666)
}
func Client(ip string, port int) {
addr := strings.Join([]string{ip, strconv.Itoa(port)}, ":")
conn, err := net.Dial("tcp", addr)
defer conn.Close()
if err != nil {
log.Fatalln(err)
}
conn.Write([]byte("test"))
log.Printf("Отправил: %s", "test")
buff := make([]byte, 1024)
n, _ := conn.Read(buff)
log.Printf("Ответ: %s", string(buff[:n]))
}