Repository: encode/uvicorn

Issue:
OSError when --port=0
### Checklist

<!-- Please make sure you check all these items before submitting your bug report. -->

- [x] The bug is reproducible against the latest release and/or `master`.
- [x] There are no similar issues or pull requests to fix it yet.

### Describe the bug

Since version 0.13.4 (latest version at the time of this writing), running `uvicorn` with `--port=0` results in the following (or similar, depending on OS):
```
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
```

### To reproduce

- Use the example application from the docs:
```python
# example.py
async def app(scope, receive, send):
    assert scope['type'] == 'http'

    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            [b'content-type', b'text/plain'],
        ],
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })
```
- Run `uvicorn`:
```
$ uvicorn example:app --port=0
```

### Expected behavior

As in `uvicorn==0.13.3`, I would expect the following output (the port may vary of course)

```
[12:03:58] D:\sand\uvicorn-port-error
λ venv\win32-3.8\Scripts\uvicorn example:app --port=0
INFO:     Started server process [18916]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:56518 (Press CTRL+C to quit)
```

### Actual behavior

On Windows:
```
[12:03:58] D:\sand\uvicorn-port-error
λ venv\win32-3.8\Scripts\uvicorn example:app --port=0
INFO:     Started server process [31268]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\sand\uvicorn-port-error\venv\win32-3.8\Scripts\uvicorn.exe\__main__.py", line 7, in <module>
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\main.py", line 362, in main
    run(**kwargs)
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\main.py", line 386, in run
    server.run()
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 49, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 66, in serve
    await self.startup(sockets=sockets)
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 158, in startup
    self._log_started_message(listeners)
  File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 190, in _log_started_message
    port = listeners[0].getpeername()[1]
  File "C:\Program Files\Python38\lib\asyncio\trsock.py", line 85, in getpeername
    return self._sock.getpeername()
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
```

On Linux:
```
[12:26:04] /mnt/d/sand/uvicorn-port-error
$ venv/linux-3.8/bin/uvicorn example:app --port=0
INFO:     Started server process [14487]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
Traceback (most recent call last):
  File "venv/linux-3.8/bin/uvicorn", line 8, in <module>
    sys.exit(main())
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/uvicorn/main.py", line 362, in main
    run(**kwargs)
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/uvicorn/main.py", line 386, in run
    server.run()
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/uvicorn/server.py", line 49, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/uvicorn/server.py", line 66, in serve
    await self.startup(sockets=sockets)
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/uvicorn/server.py", line 158, in startup
    self._log_started_message(listeners)
  File "/mnt/d/sand/uvicorn-port-error/venv/linux-3.8/lib/python3.8/site-packages/uvicorn/server.py", line 190, in _log_started_message
    port = listeners[0].getpeername()[1]
  File "/usr/lib/python3.8/asyncio/trsock.py", line 85, in getpeername
    return self._sock.getpeername()
OSError: [Errno 107] Transport endpoint is not connected
```

### Environment

Windows:
```
[12:06:28] D:\sand\uvicorn-port-error
λ venv\win32-3.8\Scripts\uvicorn --version
Running uvicorn 0.13.4 with CPython 3.8.5 on Windows
```

I also tried with the latest python 3.9, which gave the same error
```
[12:17:58] D:\sand\uvicorn-port-error
λ venv\win32-3.9\Scripts\uvicorn --version
Running uvicorn 0.13.4 with CPython 3.9.2 on Windows
```

Linux (through WSL2):
```
[12:26:15] /mnt/d/sand/uvicorn-port-error
$ venv/linux-3.8/bin/uvicorn --version
Running uvicorn 0.13.4 with CPython 3.8.5 on Linux
```


Discussion:
correct, seems like a typo from https://github.com/encode/uvicorn/pull/930/files#diff-716652db9a2feab62038753117d5e4fd4294f213f4c7f2edcb47b5788e5209f6L158-R190

Resolving patch:
diff --git a/uvicorn/server.py b/uvicorn/server.py
--- a/uvicorn/server.py
+++ b/uvicorn/server.py
@@ -187,7 +187,7 @@ def _log_started_message(self, listeners: List[socket.SocketType]) -> None:
 
             port = config.port
             if port == 0:
-                port = listeners[0].getpeername()[1]
+                port = listeners[0].getsockname()[1]
 
             protocol_name = "https" if config.ssl else "http"
             message = f"Uvicorn running on {addr_format} (Press CTRL+C to quit)"


Test patch: