golang代码服务器:
package main
import (
//"io"
"log"
"net"
"time"
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(4)//设置cpu个数
// Listen on TCP port 2000 on all interfaces.
l, err := net.Listen("tcp", ":2000")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
defer c.Close()
var buf = make([]byte, 512)
for {
cnt, err := c.Read(buf)
if err != nil {
fmt.Printf("recv failed : %s", err)
return
}
//fmt.Println(string(buf[:cnt]))
time.Sleep(10)
_, err = c.Write(buf[:cnt])
if err != nil {
fmt.Printf("write failed : %s&