net: Listen always uses nextPort() instead of the desired port on JS #31294
Comments
net.Listen
always uses nextPort()
instead of the desired port
@neelance, I think you have the most context here. Let me know if there's any way I can help! |
Here's another example of a program that actually uses the listener instead of just printing out package main
import (
"fmt"
"io/ioutil"
"net"
)
func main() {
ln, err := net.Listen("tcp", "127.0.0.1:8080")
if err != nil {
panic(err)
}
defer ln.Close()
fmt.Println(ln.Addr())
go func() {
conn, err := net.Dial("tcp", "127.0.0.1:8080")
if err != nil {
panic(err)
}
defer conn.Close()
if _, err := conn.Write([]byte("Hello!")); err != nil {
panic(err)
}
}()
fmt.Println("Waiting for new connections...")
conn, err := ln.Accept()
if err != nil {
panic(err)
}
bytes, err := ioutil.ReadAll(conn)
if err != nil {
panic(err)
}
fmt.Println("Received: ", string(bytes))
} Run with
Run with
Interestingly, it works if you change the code to |
See #30324. The package net on JS is fake and incomplete. I guess http://golang.org/cl/120958 fixes this issue. |
Yes, I understand the implementation is fake. Still, it seems that there was some effort to make it sort of work, and correcting the port numbers would make it a lot more useful. Specifically, I'm writing some tests for Go code and I want to make sure it works when compiled to WebAssembly. There's a lot of code to test, but under the hood I am trying to dial a If http://golang.org/cl/120958 fixes this I'm okay with waiting. Any idea when that might land? |
Then, I will try to catch the Go 1.13 release train. Please be informed that it's not rare that CLs from alien contributors like me will take a bit long time for landing, sometimes a year or two, as CL 120958 was written during the last year's football world cup games and still has no review yet. |
What version of Go are you using (
go version
)?Since this bug has to do with compiling to WebAssembly and running in Node.js, here is my Node.js version too:
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputHowever, it's worth noting that the bug only occurs when you set
GOOS=js GOARCH=wasm
.What did you do?
Consider the following Go program:
Run the program with the following command (From the Go WebAssembly Wiki):
What did you expect to see?
The program should output
127.0.0.1:8080
since that is the address I passed tonet.Listen
.If you run the program with
go run main.go
(not compiling to WebAssembly) you see the output that I would expect.What did you see instead?
I believe the root of the problem is the
socket
function in net_fake.go. It always usesnextPort()
instead of using the port in theladdr
argument.The text was updated successfully, but these errors were encountered: