#digipen/cs260 Lecture Notes for 09/23/2025
### Overview of non blocking socket IO strategies:
#### Blocking sockets (one thread)
- "Just live with it"
Pros:
- Simple
Cons:
- Non-interactive
Good for simple debug tools / console apps.
#### Mutliple Threads
- One thread locks, the other runs free
Pros:
- Efficient
- The socket code is easy to write
Cons:
- Threadin is hard to get right
- Threading can be expensive
#### `Select()`
More familier with older UNIX days.
A function that asks "which of these sockets is awaitng action"
Pros:
- handles large numbers of sockets well
Cons:
- .
#### Polling Sockets
"Are we there yet?"
Pros:
- Simple
Cons:
- Polling isn't very efficient unless your program is already structured that way.
#### Event Driven I/O
"Handle mouse, handle keywrod, handle network"
Pros:
- Simple
- Efficient
Cons:
- Platform-specific
#### Completion ports and overalpped IO (Advanced)
Pros:
- maximally efficient for large numbers of sockets
Cons:
- maximally complex
## Polling sockets
- Create the socket as usual
- Tell the OS to set the non-blcoking IO flag
- Watch for `WSAEWOULDBLOCK` error, which are NOT fatal
```c
int mode = 1;
int err = ioctlsocket(socket, FIONBIO, &mode);
int (res == SOCKET_ERROR) {
}
```
>[!tip] Helper method makes things easier because `ioctlsocket` is weird on windows
#### `recv()` and `send()`
- Call as normal
- Positive result = sucess
- Negaitve result = error
- If `WSAEWOULDBLOCK`, call again
- For [[../02 Areas/Computer Science/Networking/TCP-IP Model|TCP]] `recv` only, 0 result = socket shutdown.
#### `accept()`
- Cll as normal
- Valid socket result = sucess
- INVALID_SOCKET result = error
- if WSAEWOULDBLOCK, call again.
#### `connect()`
- Call as normal
- non-error result = success
- But that wont happen
- ou will generally get `WSAEWouldblock` the first time
- You will generaly get `WSALREADY` subsequent attempts until its ready
- `WSAEISCONN` means you're ready
- Errors
- `WSAWEWOUDLBOCK` = try again
- `WSAEINVAL` = try again
- `WSAEALREADY` = try again
- `WSAEISCONN` = ready tou se