Need advices conceiving a Multi-client / Server Application

Just starting out? Need help? Post your questions and find answers here.
FlorenceF
New User
New User
Posts: 3
Joined: Fri Jan 17, 2025 6:58 pm

Need advices conceiving a Multi-client / Server Application

Post by FlorenceF »

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
FlorenceF
New User
New User
Posts: 3
Joined: Fri Jan 17, 2025 6:58 pm

Re: Need advices conceiving a Multi-client / Server Application

Post by FlorenceF »

No one ??
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Need advices conceiving a Multi-client / Server Application

Post by idle »

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!
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Need advices conceiving a Multi-client / Server Application

Post by spikey »

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.
Post Reply