Skip to content

Commit

Permalink
docs: added the Inspiration section
Browse files Browse the repository at this point in the history
  • Loading branch information
chefZau committed Oct 21, 2021
1 parent 88fa201 commit 0111a9c
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

Implement in Python a simple client-server chat program. The general purpose of this assignment is to gain network programming experience in:

1. writing networked applications.
2. using the socket API in Python.
3. writing software supporting a simple protocol.
1. Writing networked applications.
2. Using the socket API in Python.
3. Writing software supporting a simple protocol.


## Hardware Required

Expand All @@ -16,6 +17,10 @@ As mentioned by the professor in the email: "Please note that under Windows, at

Detailed instructions have been illustrated on OWL. For your convenience, here is a quick explanation. There are two files in this assignment: **client.py** and **server.py**. As their name suggests, **server.py** is for starting the server and handling incoming requests, whereas **client.py** is responsible for taking client input at the console and communicating with the server.

When the server is up, it constantly runs in the background. Users will then run **client.py** to register connections. When users are successfully registered, they can send and receive messages back and forth using the terminal(console).

Notice that the program allows multiple clients to run concurrently. This is done by using the built-in [selectors](https://docs.python.org/3/library/selectors.html) module.

## Set it up

### Step 1: Get Your IP Address
Expand All @@ -35,7 +40,7 @@ To start a server, use the following script:
python3 server.py
```

When a the server starts, it will output a port number in the console. Record this number, we will use it to register a client.
When the server starts, it will output a port number in the console. Record this number; we will use it to register a client.

```text
Will wait for client messages at port 64700 <---- this one
Expand Down Expand Up @@ -73,3 +78,26 @@ Registration successful. Ready for messageing!
>
```

## Inspiration

When completing this assignment, I noted that there weren't many examples for using the [selectors](https://docs.python.org/3/library/selectors.html) module. On the contrary, there were many implementations related to [threading](https://www.techwithtim.net/tutorials/socket-programming/). Yet, those examples were irrelevant to the assignment. Therefore, I started by reading the [Socket Programming in Python (Guide)](https://realpython.com/python-sockets/) to familiarize myself with the networking fundamentals. Furthermore, I also watched a [Python Socket Programming Tutorial](https://youtu.be/3QiPPX-KeSc) on YouTube. Up to this point, I was able to implement the majority of the program.

Since the server needs to support communicating with multiple clients simultaneously, I started by learning the basics of the selectors module. Here are some inspirational links I encountered:

* [Python 3 Standard Library: selectors I/O Multiplex Abstraction](https://programming.vip/docs/python-3-standard-library-selectors-i-o-multiplex-abstraction.html)
* [Python selectors模块用法:实现非阻塞式编程](https://naoketang.com/p/nozql01vqg01)
* [使用Python实现一个简单的聊天室](https://blog.csdn.net/u011960402/article/details/107503730)

The last bit was the user input. One of the requirements was the following:

* To send a message, a user will type it at a prompt provided by their chat client.

I tried using the built-in `input()` method. However, the way blocks the incoming message from the server, and the incoming message only displays when I press the enter key on the keyboard. This is not ideal. Fortunately, the professor sent a follow-up email regarding this issue. Here is the email:

```text
Several of you have had issues with input() blocking in your client, making it unable to retrieve server messages until input of some kind is provided. That's not ideal and not the way you should be doing things. You don't need to use threads to solve this problem, but instead should be using the selectors package there similar to how you do so in the server. The difference is that in the client, in addition to waiting for input from the server, you can also wait for input from sys.stdin. That way, you only go to retrieve input from the terminal when input is there waiting for you. (And there's a few ways to retrieve things nicely from sys.stdin ...) If you do things this way, we can readily avoid issues in blocking while still only using the selectors package.
...
```

Here is a helpful link related to [Taking input from sys.stdin, non-clocking](https://stackoverflow.com/questions/53045592/python-non-blocking-sockets-using-selectors).

0 comments on commit 0111a9c

Please sign in to comment.