Comments (9)
麻烦贴一下完整代码。
from gnet.
package server
import (
"fmt"
"github.com/panjf2000/gnet"
"github.com/panjf2000/gnet/pool"
"log"
)
type IcoServer struct {
*gnet.EventServer
pool *pool.WorkerPool
}
func (is *IcoServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
log.Printf("Server is running under multi-core: %t, loops: %d\n", srv.Multicore, srv.NumLoops)
return
}
func (is *IcoServer) OnOpened(c gnet.Conn) (out []byte, action gnet.Action) {
fmt.Println("new client connected!")
c.ResetBuffer()
return
}
func (is *IcoServer) React(c gnet.Conn) (out []byte, action gnet.Action) {
data := append([]byte{}, c.Read()...)
c.ResetBuffer()
fmt.Println("read data...", data)
// Use ants pool to unblock the event-loop.
_ = is.pool.Submit(func() {
fmt.Println("Async Writing...")
c.AsyncWrite([]byte("hello world"))
})
//out = data
//
//action = gnet.Close
return
}
func (is *IcoServer) PreWrite() {
fmt.Println("writing data....")
}
func NewIcoServer(p *pool.WorkerPool) *IcoServer {
return &IcoServer{pool: p}
}
from gnet.
package main
import (
"github.com/panjf2000/gnet"
"github.com/panjf2000/gnet/pool"
"gnet-learning/server"
"log"
)
func main() {
p := pool.NewWorkerPool()
defer p.Release()
echo := server.NewIcoServer(p)
log.Fatal(gnet.Serve(echo, "tcp://:9000", gnet.WithMulticore(true)))
}
from gnet.
client 的代码也给一下,还有你在 go modules 里用的 gnet
是哪个版本?
from gnet.
go version: v1.12.8
gnet version: v1.0.0-beta.3
client.go
package main
import (
"fmt"
"io/ioutil"
"net"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s host:port", os.Args[0])
}
//获取命令行参数 socket地址
//server := os.Args[1]
addr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:9000")
checkError(err)
//建立tcp连接
conn, err := net.DialTCP("tcp4", nil, addr)
checkError(err)
//向服务端发送数据
_, err = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n"))
checkError(err)
//接收响应
response, _ := ioutil.ReadAll(conn)
fmt.Println(string(response))
os.Exit(0)
}
func checkError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
from gnet.
AsyncWrite() 会唤醒 epoll/kqueue 处理写操作,只要AsyncWrite()调用之后就会触发内部事件循环,执行写操作
from gnet.
我有调试了一下代码,写操作执行成功了,但是不知为何,数据没有发送到客户端,在我点击停止调试的时候,客户端才收到了数据
from gnet.
@hongbaotong
刚才开会去了,回来之后大概扫了一下你的代码,问题出在你的client,你用了ioutil.ReadAll去读数据当然会阻塞住,它本来就是一个阻塞方法,会一直阻塞直到有error或者eof,所以其实server已经给你把数据写回来了,但是client一直阻塞在这个方法上,等待error或者eof,所以只有server退出了之后关闭了连接这个方法才能返回。
不要用这个方法,我改了一下你的client,可以成功运行了:
package main
import (
"fmt"
"net"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s host:port\n", os.Args[0])
}
//获取命令行参数 socket地址
//server := os.Args[1]
addr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:9000")
checkError(err)
//建立tcp连接
conn, err := net.DialTCP("tcp4", nil, addr)
checkError(err)
//向服务端发送数据
_, err = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n"))
checkError(err)
//接收响应
//response, err := ioutil.ReadAll(conn)
//if err != nil {
// fmt.Printf("reading error: %v\n", err)
//}
response := make([]byte, 20)
_, _ = conn.Read(response)
fmt.Println(string(response))
os.Exit(0)
}
func checkError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
from gnet.
已解决,多谢指点
from gnet.
Related Issues (20)
- [Question]: gnet engine is stopping with error: kevent add|clear:function not implemented HOT 5
- [Question]: 您好大佬,这个库的epoll的底层实现是用纯go实现的吗?没有用到cgo是吧? HOT 1
- [Feature]: how to disable logging? replace uber zap with zerolog too pls HOT 3
- [Question]: The TechEmpower Benchmark is misleading, but can we make it even faster? HOT 1
- [Feature]: add synchronization for Conn to make it concurrency-safe HOT 1
- [Question]: What is the maximum number of cores tested by anyone? I just tested 192 cores and it's better than evio etc but... HOT 7
- [Question]: Will the OnTraffic function be called concurrently? HOT 1
- [Question]: Validate the approach of implementing persistent TCP connections. HOT 1
- [Bug]: Calling Dial() in version 2.4 of Windows 10 will block HOT 4
- [Question]: I would like to verify whether this problem in the screenshot will cause the memory of the service to slowly increase. HOT 1
- [Feature]: support edge-triggered I/O
- [Question]: Is the netpoll v1 version defaulting to horizontal triggering, and the V2 version defaulting to edge triggering? HOT 15
- [Bug]: fatal error: concurrent map read and map write HOT 1
- Inconsistent behaviors with SO_REUSEPORT on Linux and *BSD HOT 7
- Don't use SO_REUSEPORT on Unix domain sockets HOT 1
- Don't disable SO_REUSEPORT on DragonFlyBSD HOT 1
- [Question]: gnet client works not as expected even tho i did it according to the api ref HOT 1
- [Bug]: GNET client not working as expected HOT 4
- [Bug]: example gnet for echo tcp uses too much memory HOT 7
- [Bug]: Read returns io.ErrShortBuffer on closed connection HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gnet.