Skip to content

Instantly share code, notes, and snippets.

@freekman
Created March 23, 2016 15:09
Show Gist options
  • Save freekman/8784ecfed338e4b99b2d to your computer and use it in GitHub Desktop.
Save freekman/8784ecfed338e4b99b2d to your computer and use it in GitHub Desktop.
TCP(client-server)
package client
import (
"fmt"
"net"
"time"
)
type Client struct {
Host string
Port int
}
func NewMultiClient(host string, port int) *Client {
return &Client{host, port}
}
func (c *Client) OpenConnections(count int) {
addr := fmt.Sprintf("%v:%v", c.Host, c.Port)
for i := 0; i < count; i++ {
con, err := net.Dial("tcp", addr)
if err != nil {
fmt.Println(err.Error())
return
}
go handleConnection(con)
}
}
func handleConnection(con net.Conn) {
con.SetDeadline(time.Now().Add(5*time.Second))
buff := make([]byte, 1024)
for i := 0; i < 1000; i++ {
time.Sleep(20*time.Millisecond)
_, err := con.Write([]byte(fmt.Sprintf("message%v", i)))
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
_, err = con.Read(buff)
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
fmt.Printf(" -> %v %v \n", con.RemoteAddr(), string(buff))
}
// go write(con, "message")
// go read(con)
}
func write(con net.Conn, message string) {
buff := make([]byte, 32)
for i := 0; i < 1000; i++ {
time.Sleep(20*time.Millisecond)
_, err := con.Write([]byte(fmt.Sprintf("message%v", i)))
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
_, err = con.Read(buff)
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
fmt.Printf(" -> %v %v \n", con.RemoteAddr(), string(buff))
}
fmt.Println(con.LocalAddr(), " all messages are send.")
}
func read(con net.Conn) {
for {
buff := make([]byte, 32)
_, err := con.Read(buff)
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
fmt.Printf(" -> %v %v \n", con.RemoteAddr(), string(buff))
}
}
package client
import (
"fmt"
"net"
"time"
)
type Client struct {
Host string
Port int
}
func NewMultiClient(host string, port int) *Client {
return &Client{host, port}
}
func (c *Client) OpenConnections(count int) {
addr := fmt.Sprintf("%v:%v", c.Host, c.Port)
for i := 0; i < count; i++ {
con, err := net.Dial("tcp", addr)
if err != nil {
fmt.Println(err.Error())
return
}
go handleConnection(con)
}
}
func handleConnection(con net.Conn) {
con.SetDeadline(time.Now().Add(5*time.Second))
buff := make([]byte, 1024)
for i := 0; i < 1000; i++ {
time.Sleep(20*time.Millisecond)
_, err := con.Write([]byte(fmt.Sprintf("message%v", i)))
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
_, err = con.Read(buff)
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
fmt.Printf(" -> %v %v \n", con.RemoteAddr(), string(buff))
}
// go write(con, "message")
// go read(con)
}
func write(con net.Conn, message string) {
buff := make([]byte, 32)
for i := 0; i < 1000; i++ {
time.Sleep(20*time.Millisecond)
_, err := con.Write([]byte(fmt.Sprintf("message%v", i)))
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
_, err = con.Read(buff)
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
fmt.Printf(" -> %v %v \n", con.RemoteAddr(), string(buff))
}
fmt.Println(con.LocalAddr(), " all messages are send.")
}
func read(con net.Conn) {
for {
buff := make([]byte, 32)
_, err := con.Read(buff)
if err != nil {
fmt.Println("Connection is clesed.", err.Error())
con.Close()
}
fmt.Printf(" -> %v %v \n", con.RemoteAddr(), string(buff))
}
}
package main
import (
"github.com/clouway/cloudplatform/dellMe/client"
"github.com/clouway/cloudplatform/dellMe/server"
)
func main() {
go func() {
server.NewTCPServer(5896).Start()
}()
go func() {
client.NewMultiClient("127.0.0.1", 5896).OpenConnections(40)
}()
for {
} //Circle for ever :)
}
package server
import (
"fmt"
"net"
"time"
)
type TCP struct {
Port int
}
func NewTCPServer(port int) *TCP {
return &TCP{Port: port}
}
func (s *TCP) Start() {
conNumber := 0
fmt.Printf("Starting TCP Server On Port %v \n", s.Port)
addr := fmt.Sprintf(":%d", s.Port)
listener, err := net.Listen("tcp", addr)
if err != nil {
fmt.Println(err.Error())
return
}
defer listener.Close()
for {
c, err := listener.Accept()
if err != nil {
fmt.Println(err.Error())
}
conNumber++
go func(c net.Conn, num int) {
defer c.Close()
fmt.Printf("[Server] Client Connected with Remote addr:%v number:%v \n", c.RemoteAddr(), num)
c.SetDeadline(time.Now().Add(5*time.Second))
buff := make([]byte, 1024)
for {
n, err := c.Read(buff)
if err != nil {
fmt.Println("[Server] Cant read from", c.RemoteAddr())
break
}
_, err = c.Write(buff[:n])
if err != nil {
fmt.Println("[Server] Cant write to", c.RemoteAddr())
break
}
}
}(c, conNumber)
}
fmt.Printf("TCP Server On Port %v is STOPING \n", s.Port)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment