Hello
I have to create a Client / Server application over the Internet on Windows with potentially hundreds of clients and one server .
Before rushing headlong into programming, I would like some theorical and practical advices from you guys.
Especially :
- What packet structure should I use (headers maybe ?)
- Reliably detect disconnections both on clients and server.
- Ensuring messages transmission and handling transmission errors.
- Is the Purebasic Network Library enough or should I use Windows socket library ?
- Should I use threads on the server side for handling connections ? If yes, what are the programming errors i must avoid.
Any other advice is welcome.
Thank you
Need advices conceiving a Multi-client / Server Application
Re: Need advices conceiving a Multi-client / Server Application
It's a bit of an open question that no one can answer without knowing what your specifically wanting to do
what packet structure: That's arbitrary depends on what you want to send and receive, header fields may include packet type, sequence number, payload length, time stamp, crc...
disconnections: use a heartbeat where the client periodically sends a small packet to the server to indicate it's still connected. If the server doesn't receive a heartbeat within a specified time, it can assume the client has disconnected.
or use timeouts to detect inactivity. If a client doesn't send any data within a certain period, the server can close the connection.
Ensuring messages transmission and handling transmission errors. Use TCP that's what its for
Is the Purebasic Network Library enough. yes
Should I use threads on the server side for handling connections ? If yes, what are the programming errors i must avoid.
if your asking what errors you need to avoid using threads then I'd suggest that you don't use threads.
ps you can also try asking copilot!
what packet structure: That's arbitrary depends on what you want to send and receive, header fields may include packet type, sequence number, payload length, time stamp, crc...
disconnections: use a heartbeat where the client periodically sends a small packet to the server to indicate it's still connected. If the server doesn't receive a heartbeat within a specified time, it can assume the client has disconnected.
or use timeouts to detect inactivity. If a client doesn't send any data within a certain period, the server can close the connection.
Ensuring messages transmission and handling transmission errors. Use TCP that's what its for
Is the Purebasic Network Library enough. yes
Should I use threads on the server side for handling connections ? If yes, what are the programming errors i must avoid.
if your asking what errors you need to avoid using threads then I'd suggest that you don't use threads.
ps you can also try asking copilot!
Re: Need advices conceiving a Multi-client / Server Application
Please be patient and don't re-post the same question multiple times in quick succession. It's Friday, people have social lives! Many will be in a different time-zone to you. In this case the answer wasn't quick to compose either!
It really depends exactly what you are going to be doing. Read all the network library command help articles carefully before you start.
It may not actually work the way you assume it will if you don't. See this thread too https://www.purebasic.fr/english/viewtopic.php?t=80475
If there is an established protocol for your application already, follow suit. There will be example code for you to look at as a reference. You may need
headers, it depends how large each message might be.
If a client calls CloseNetworkConnection() the server will receive #PB_NetworkEvent_Disconnect. However a client will not automatically receive this event if the server calls CloseNetworkConnection() first. You will need to build some kind of "I'm shutting down now" messaging into your application to do this properly.FlorenceF wrote: Fri Jan 17, 2025 7:10 pm - Reliably detect disconnections both on clients and server.
Unexpected disconnections may occur. You will need to build in tolerance of this because they will occur without warning. For example, "keep alive" messages and connection idle timers.
TCP/IP contains packet level transmission control for errors detected at the network protocol level but if your application's individual messages will be larger than about 1500 bytes (TCP) or 2048 bytes (UDP) you will need application level sequencing control too.FlorenceF wrote: Fri Jan 17, 2025 7:10 pm - Ensuring messages transmission and handling transmission errors.
If you will need to transfer more than 64kb you may be interested in mk-soft's module, Module NetworkTCP - Send and Receive Data over 64kB.
It depends how complex your messaging will be. The PB library should be fine for simple messaging but more complex messaging might need features from the OS API.FlorenceF wrote: Fri Jan 17, 2025 7:10 pm - Is the Purebasic Network Library enough or should I use Windows socket library ?
It might be possible to construct a synchronous program to handle small workloads but I'd be willing to bet that you would come to regret that decision sooner or later. A synchronous program definitely will not be able to handle "hundreds" of clients.FlorenceF wrote: Fri Jan 17, 2025 7:10 pm - Should I use threads on the server side for handling connections ?
- Concurrent uncontrolled access to shared data.
- Failing to lock mutexes at the right point.
- Failing to unlock mutexes at the right point.
Challenges and Best Practices in Multithreaded Programming.
Also this is specific to PureBasic: What does Threadsafe do?.
The one thread per client model might seem simple to implement but it has a few drawbacks:
- It requires a lot of resources for a lot of clients.
- It is potentially very vulnerable to a DDOS attack.
- It can be very inefficient if thread workloads are low.