FlorenceF wrote: Fri Jan 17, 2025 9:07 pmNo one ??
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!
FlorenceF wrote: Fri Jan 17, 2025 7:10 pm
Any other advice is welcome.
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
FlorenceF wrote: Fri Jan 17, 2025 7:10 pm
- What packet structure should I use (headers maybe ?)
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.
FlorenceF wrote: Fri Jan 17, 2025 7:10 pm
- Reliably detect disconnections both on clients and server.
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.
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.
FlorenceF wrote: Fri Jan 17, 2025 7:10 pm
- Ensuring messages transmission and handling transmission errors.
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.
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.
FlorenceF wrote: Fri Jan 17, 2025 7:10 pm
- Is the Purebasic Network Library enough or should I use Windows socket library ?
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
- Should I use threads on the server side for handling connections ?
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
If yes, what are the programming errors i must avoid.
- Concurrent uncontrolled access to shared data.
- Failing to lock mutexes at the right point.
- Failing to unlock mutexes at the right point.
You should do some background reading. This is a reasonable introduction:
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.
You might wish to consider a thread pooling implementation. This might be useful:
Load-Balanced Worker Threads.