Also the proper use of Go routines allow you to implement async without the messiness of callbacks. 

On Aug 11, 2024, at 1:49 PM, Robert Engels <reng...@ix.netcom.com> wrote:


This is not true. The synchronous requirements are based on higher level protocol requirements. You can just as easily do async with Reader and Writer. 

Eg. The client can send multiple requests and the server could respond to these out of order (I.e the response contains the request id) or not respond at all. 

You might want to look at using gRPC which will help you understand the model better. 

On Aug 11, 2024, at 1:44 PM, 'lijh8' via golang-nuts <golang-nuts@googlegroups.com> wrote:



Hi community,

In my little example:
if server reads first, then writes;
client must writes first, then read;

then client must performs the operations in reverse order as server,
or both server and client will block.

In C++ boost.asio, the order of calls to async_read, async_write 
does not matter in server and client.

Is there this kind of ` async ` read and write ability in golang packages?

Thanks

```

// server.go:

func main() {
logfile := "logfile.log"
logfd, err := log2.InitLog(logfile)
if err == nil {
defer logfd.Close()
}

if len(os.Args) != 2 {
log.Printf("Usage: ./server <port>")
os.Exit(-1)
}

port := os.Args[1]
ln, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Println(err)
os.Exit(-1)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
os.Exit(-1)
}
go handleConnection(conn)
}
}

func handleConnection(conn net.Conn) {
defer conn.Close()

msgID := 0
buffer := make([]byte, 1024)

for {
n, err := conn.Read(buffer)
if err != nil {
log.Println(err, n)
break
}
fmt.Println(string(buffer))

msgID++
msg := fmt.Sprintf("msg from server with msgID: %d", msgID)
n, err = conn.Write([]byte(msg))
if err != nil {
log.Println(err, n)
break
}
}
}


// client.go:

func main() {
logfile := "logfile.log"
logfd, err := log2.InitLog(logfile)
if err == nil {
defer logfd.Close()
}

if len(os.Args) != 4 {
log.Printf("Usage: ./client <serverIP> <port> <tag>")
os.Exit(-1)
}

serverIP := os.Args[1]
port := os.Args[2]
tag := os.Args[3]
conn, err := net.Dial("tcp", serverIP+":"+port)
if err != nil {
log.Println(err)
os.Exit(-1)
}
handleConnection(conn, tag)
}

func handleConnection(conn net.Conn, tag string) {
defer conn.Close()

msgID := 0
buffer := make([]byte, 1024)

for {
msgID++
msg := fmt.Sprintf("msg from client: %s, msgID: %d", tag, msgID)
n, err := conn.Write([]byte(msg))
if err != nil {
log.Println(err, n)
break
}

n, err = conn.Read(buffer)
if err != nil {
log.Println(err, n)
break
}
fmt.Println(string(buffer))
}
}


```

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/tencent_16864CF32F826D31908D052707F2E2621709%40qq.com.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CA2745A0-8C46-4037-A8EF-04A4DDC29BB9%40ix.netcom.com.

Reply via email to