Created
November 5, 2014 17:15
-
-
Save squiidz/ccb2e9559605ef68d1b7 to your computer and use it in GitHub Desktop.
Learn Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"strconv" | |
) | |
type Server struct { | |
Addr string | |
Port string | |
Type string | |
Client *Pool | |
} | |
func (s *Server) Work() { | |
list, err := net.Listen(s.Type, s.Addr+":"+s.Port) | |
if err != nil { | |
log.Println(err) | |
} | |
for { | |
newConn, err := list.Accept() | |
if err != nil || s.Client.Conns > MAX_CLIENT { | |
log.Println(err) | |
newConn.Close() | |
} | |
s.Client.Free = append(s.Client.Free, newConn) | |
s.Client.Conns = len(s.Client.Free) | |
log.Println("[+] " + s.Client.Name + " new Client ! => " + strconv.Itoa(s.Client.Conns)) | |
HandleConn(newConn) | |
} | |
} | |
type Pool struct { | |
Name string | |
Conns int | |
Free []net.Conn | |
} | |
func NewPool(name string) *Pool { | |
return &Pool{Name: name} | |
} | |
const ( | |
CONN_HOST = "localhost" | |
CONN_PORT = "9000" | |
CONN_TYPE = "tcp" | |
MAX_CLIENT = 2 | |
) | |
var ( | |
welco = []byte("Your Connected") | |
) | |
func main() { | |
server := Server{ | |
Addr: "localhost", | |
Port: "9000", | |
Type: "tcp", | |
Client: NewPool("Test"), | |
} | |
server.Work() | |
} | |
func HandleConn(conn net.Conn) { | |
_, err := conn.Write(welco) | |
if err != nil { | |
log.Println(err) | |
} | |
data := make([]byte, 1024) | |
_, err = conn.Read(data) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Println("[->] ", conn.RemoteAddr().String(), string(data)) | |
conn.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment