-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
socket: created
connect
, accept
, recv
, send
, shutdown
, bind
,
`listen`, `getsockname`, `setsockopt`, `getsockopt` and test and example
- Loading branch information
Showing
5 changed files
with
360 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from shakti import SOL_SOCKET, SO_REUSEADDR, run, socket, bind, listen, accept, \ | ||
connect, recv, send, shutdown, close, sleep, setsockopt # task, | ||
|
||
|
||
async def echo_server(host, port): | ||
server_fd = await socket() | ||
try: | ||
print('Starting Server') | ||
await setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, True) | ||
await bind(server_fd, host, port) | ||
await listen(server_fd, 1) | ||
while client_fd := await accept(server_fd): | ||
await client_handler(client_fd) # temp solution | ||
# await task(client_handler(client_fd)) # TODO | ||
break | ||
finally: | ||
await close(server_fd) | ||
print('Closed Server') | ||
|
||
|
||
async def client_handler(client_fd): | ||
try: | ||
print('server recv:', await recv(client_fd, 1024)) | ||
print('server sent:', await send(client_fd, b'hi from server')) | ||
await shutdown(client_fd) | ||
finally: | ||
await close(client_fd) | ||
|
||
|
||
async def echo_client(host, port): | ||
await sleep(.001) # wait for `echo_server` to start up. | ||
client_fd = await socket() | ||
await connect(client_fd, host, port) | ||
print('client sent:', await send(client_fd, b'hi from client')) | ||
print('client recv:', await recv(client_fd, 1024)) | ||
await close(client_fd) | ||
|
||
|
||
if __name__ == '__main__': | ||
host = '127.0.0.1' | ||
port = 12345 | ||
run(echo_server(host, port), echo_client(host, port)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.