2

I made a simple client-server program in c using sockets and now i want to test it ,by simulating many clients connecting to the server at the same time!I wrote a script to execute the client: ./client 20 times but it didn't work for me since it waited for each client to finish.

Also i wrote another program in c ,this time with threads so it could execute each client with system(./client) and then detach the thread ,but again i had the same problem!

So what is the correct way to implement this?

kotseman
  • 57
  • 6
  • Possible duplicate of [multithread server/client implementation in C](https://stackoverflow.com/questions/21405204/multithread-server-client-implementation-in-c) – Tal Sokolinsky Jun 09 '18 at 16:17
  • I think your question may be answered here: [multithread server/client implementation in C](https://stackoverflow.com/questions/21405204/multithread-server-client-implementation-in-c) – Tal Sokolinsky Jun 09 '18 at 16:18
  • 1
    You probably need `./client &` (or, to run 20, consider `for i in $(seq 1 20); do ./client &; done`) to run the client in background. To write a C program to do the job, it will (probably) take arguments to say how many copies to run, and what to run, and will fork and the child will exec the designated program. – Jonathan Leffler Jun 09 '18 at 16:20
  • 1
    To service multiple clients, you need to avoid blocking I/O... in your server that implements multiple threads, use `fork()` to handle the communication asynchronously. – Yahya Jun 09 '18 at 16:21
  • 1
    You've indicated two different approaches you have tried that don't work, but you haven't shown exactly what you tried. – lurker Jun 09 '18 at 16:33
  • this is what i tried: – kotseman Jun 09 '18 at 16:54
  • `#!/bin/bash # Basic while loop counter=1 while [ $counter -le 20 ] do echo "./clientk" ./clientk ((counter++)) done` – kotseman Jun 09 '18 at 16:54

2 Answers2

2

The easiest solution is to do your shell script, but put an & after the ./clientk call, which will put it in the background and run the next command immediately

One Guy Hacking
  • 1,176
  • 11
  • 16
1

Here's a really simple way to launch a number of clients without waiting for each to complete:

#!/bin/bash

for i in $(seq 0 20)
do
    ./client &
done

wait
Brian Cain
  • 14,403
  • 3
  • 50
  • 88