New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A server that also acts as a client #345
Comments
Couldn't you just create a UDP socket to server B in server A and bind that socket with the port you want? |
Maybe, but how? Example code: package main
import (
"fmt"
"log"
"github.com/panjf2000/gnet/v2"
)
type udpServer struct {
gnet.BuiltinEventEngine
}
func (es *udpServer) OnTraffic(c gnet.Conn) gnet.Action {
data, _ := c.Next(-1)
log.Printf("Got message: %s", data)
return gnet.None
}
func (es *udpServer) OnBoot(eng gnet.Engine) gnet.Action {
// Here I want to send a UDP message to B from the socket of `udpServer`
return gnet.None
}
func main() {
udp := new(udpServer)
log.Fatal(gnet.Run(udp, fmt.Sprintf("udp://:%d", 9000), gnet.WithMulticore(true), gnet.WithReusePort(false)))
} |
client.Dial() and use the returned gnet.Conn to send data, or you can just use the standard |
I think there is still a little misunderstanding. It is not an option to create a new socket. This would mean that the outgoing UDP packets would have a different sender port than the UDP server A. So I need to send UDP packets from UDP server A to remain addresses. Thus, the receiver can answer the message, and this answer also arrives again at the UDP server A. |
Besides, I don't think you are allowed to reuse an in-use port of an existing server as the source port of sender. |
u want it working like this ? u need to figure out a way send pkts to different peer from one local Conn object. Does one udp Conn support customized peer addr on each asyncWrite ? |
I understand what you want. In fact, by using the builtin net package in Go to directly bind a UDP socket, one can read messages that are being listened to, or send them to another target. That might have allowed you to do what you want. However, the reason we use gnet is probably because it's more convenient than implementing directly with the builtin net package. In gnet, internally, there is a file descriptor for a UDP socket bound to the server's port address in the event loop. In fact, it would be great if the gnet maintainer provided a convenient interface for this. The workaround I found is as follows.
And then it can be used elsewhere.
In fact, because I am in a bit of a hurry, I had to make it work somehow. For the time being, I will use it this way, but as I mentioned before, I hope that gnet can make it more elegantly. Thank you. |
Please fill out the following system information before opening an issue:
What is your question about gnet?
Is there a way in gnet to start a UPD server and send a message to another server from the same FD without the server sending a message before.
An example: There is UDP server
A
and UDP serverB
.A
andB
have never communicated with each other before.A
now wants to send a message toB
.How would I do something like this in gnet? Is it possible at all?
It is important that the port of UDP server
A
does not change when sending the message. So it is not possible to just create a new client, which then sends the message toB
.The text was updated successfully, but these errors were encountered: