Skip to content

Commit

Permalink
Chapter.10 미션 추가
Browse files Browse the repository at this point in the history
- Mission B: Busy-waiting non-blocking server

Co-authored-by: famo1245 <kandallee38@gmail.com>
  • Loading branch information
csct3434 and famo1245 committed Dec 28, 2024
1 parent a4444e4 commit bc7f057
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions chapter10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,105 @@ Wrong number of pizzas, please try again
12
Thank you for ordering 12 pizzas!
```

# Mission B: Busy-waiting non-blocking server
### Description
Implement the Python code below in Java

### Python Code
```python
#!/usr/bin/env python3.9

"""Busy-waiting non-blocking server implementation"""

import typing as T
from socket import socket, create_server

# the maximum amount of data to be received at once
BUFFER_SIZE = 1024
ADDRESS = ("127.0.0.1", 12345) # address and port of the host machine


class Server:
clients: T.Set[socket] = set()

def __init__(self) -> None:
try:
print(f"Starting up at: {ADDRESS}")
self.server_socket = create_server(ADDRESS)
# set socket to non-blocking mode
self.server_socket.setblocking(False)
except OSError:
self.server_socket.close()
print("\nServer stopped.")

def accept(self) -> None:
try:
conn, address = self.server_socket.accept()
print(f"Connected to {address}")
# making this connection non-blocking
conn.setblocking(False)
self.clients.add(conn)
except BlockingIOError:
# [Errno 35] Resource temporarily unavailable
# indicates that "accept" returned without results
pass

def serve(self, conn: socket) -> None:
"""Serve the incoming connection by sending and receiving data."""
try:
while True:
data = conn.recv(BUFFER_SIZE)
if not data:
break
try:
order = int(data.decode())
response = f"Thank you for ordering {order} pizzas!\n"
except ValueError:
response = "Wrong number of pizzas, please try again\n"
print(f"Sending message to {conn.getpeername()}")
# send a response
conn.send(response.encode())
except BlockingIOError:
# recv/send returns without data
pass

def start(self) -> None:
"""Start the server by continuously accepting and serving incoming
connections."""
print("Server listening for incoming connections")
try:
while True:
self.accept()
for conn in self.clients.copy():
self.serve(conn)
finally:
self.server_socket.close()
print("\nServer stopped.")


if __name__ == "__main__":
server = Server()
server.start()
```

### Sample Output
#### Server
```
Starting up at: ('127.0.0.1', 12345)
Server listening for incoming connections
Connected to ('127.0.0.1', 57283)
Sending message to ('127.0.0.1', 57283)
Sending message to ('127.0.0.1', 57283)
Sending message to ('127.0.0.1', 57283)
```
### Terminal
```
nc 127.0.0.1 12345
a
Wrong number of pizzas, please try again
123
Thank you for ordering 123 pizzas!
11
Thank you for ordering 11 pizzas!
```

0 comments on commit bc7f057

Please sign in to comment.