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

net.listen_tcp return connected port selected by os when port number was 0 #4237

Closed
gco-bmx opened this issue Sep 14, 2024 · 0 comments · Fixed by #4261
Closed

net.listen_tcp return connected port selected by os when port number was 0 #4237

gco-bmx opened this issue Sep 14, 2024 · 0 comments · Fixed by #4261

Comments

@gco-bmx
Copy link

gco-bmx commented Sep 14, 2024

As discussed on discord

I think this can be done doing something like this to get the port number

port := 0
ep_server : net.Endpoint = {net.IP4_Address{127,0,0,1}, port}
fmt.printf("ep_server %v\n", ep_server)
listen_socket, err_listen := net.listen_tcp(ep_server)
if err_listen != nil {
  fmt.printf("err_listen %v\n", err_listen)
  return
}
sock := win32.SOCKET(listen_socket)
s : win32.SOCKADDR_STORAGE_LH
c : win32.c_int = size_of(s)
r := win32.getsockname(sock, &s, &c)
fmt.printf("socket_listen %v %v s:%v r:%v e:%v\n", ep_server, listen_socket, s, r, win32.WSAGetLastError())

but an easier none os specific way would be nice

Is your feature request related to a problem? Please describe.
startup a server without relying on a fixed port number that may already be taken.

say you have something that spins up a listener on localhost
and forks/execs a child helper
you can pass the port
say you run a game client that does p2p, and have an out-of-band mechanism that does matchmaking
it doesn't matter which port you pick becaue you handle doing uPNP or whatever, and send the ip/port combination to the matchmaking server
etc etc etc
you can hardcode the port, but everyone picks like... 8888, 8080, 6969, 42069 etc
so having the kernel just assign one is more robust

Describe the solution you'd like
add an proc to core:net to be able to do this or something to listen_tcp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants