Skip to content
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

listener.Accept() runs only after a byte is received #225

Open
vikulin opened this issue Jun 4, 2022 · 1 comment
Open

listener.Accept() runs only after a byte is received #225

vikulin opened this issue Jun 4, 2022 · 1 comment

Comments

@vikulin
Copy link

vikulin commented Jun 4, 2022

listener.Accept() runs only after a byte is received: but expected to run it after kcp.Dial.

The test code:

package main

import (
	"log"
	"time"
	kcp "github.com/xtaci/kcp-go/v5"
)

func main() {

	// start the server

	ln, err := kcp.Listen("127.0.0.1:48000")
	log.Println("Listening KCP...")
	if err != nil {
		panic(err)
	}
	// run the client
	go client()
	/**
	Listening for a connection
	**/
	_, err = ln.Accept()
	if err != nil {
		panic(err)
	} else {
		log.Println("OK.")
	}
}

func client() {

	// wait for server to become ready
	time.Sleep(time.Second)

	// dial to the echo server
	if sess, err := kcp.Dial("127.0.0.1:48000"); err == nil {
		// wait for data write
		time.Sleep(time.Second)
		data := "."
		log.Println("sent:", data[:1])
		if _, err := sess.Write([]byte(data[:1])); err != nil {
			log.Fatal(err)				
		}
	} else {
		log.Fatal(err)
	}
}

Output:

$ go run examples/main.go
2022/06/04 12:36:33 Listening KCP...
2022/06/04 12:36:35 sent: .
2022/06/04 12:36:35 OK.

Expected "OK." to follow right after "Listening KCP..."

@vikulin
Copy link
Author

vikulin commented Jun 4, 2022

Test code for TCP:

package main

import (
	"net"
	"log"
	"time"
	//kcp "github.com/xtaci/kcp-go/v5"
)

func main() {

	// start the server

	ln, err := net.Listen("tcp", "127.0.0.1:48000")
	log.Println("Listening KCP...")
	if err != nil {
		panic(err)
	}
	// run the client
	go client()
	/**
	Listening for a connection
	**/
	_, err = ln.Accept()
	if err != nil {
		panic(err)
	} else {
		log.Println("OK.")
	}
}

func client() {

	// wait for server to become ready
	time.Sleep(time.Second)

	// dial to the echo server
	if sess, err := net.Dial("tcp", "127.0.0.1:48000"); err == nil {
		// wait for data write
		time.Sleep(time.Second)
		data := "."
		log.Println("sent:", data[:1])
		if _, err := sess.Write([]byte(data[:1])); err != nil {
			log.Fatal(err)				
		}
	} else {
		log.Fatal(err)
	}
}

Output:

$ go run examples/main.go
2022/06/04 12:39:52 Listening KCP...
2022/06/04 12:39:53 OK.

@vikulin vikulin changed the title listener.Accept() runs only after a byte received listener.Accept() runs only after a byte is received Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant