question

Zoe Liu avatar image
1 Like"
Zoe Liu asked Zoe Liu commented

How to connect two clients at the same time when using socket communication?

Hello,

I would like to use the FlexSim model as a server and receive messages from two clients. I tried to add two "client" nodes in the tree, then "servercreatemain" twice (enter two different port numbers). But I can only receive the information sent by the second client. How can I receive messages from two clients at the same time? Thank you!

FlexSim 19.1.0
socket
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Mischa Spelt avatar image
2 Likes"
Mischa Spelt answered Zoe Liu commented

The server should (and probably can) be listening on only one port at the time. So you should call servercreatemain only once, and connect both clients at the same port.

  1. servercreatemain(1880);
  2.  
  3. // Wait for two clients to connect
  4. Array clients = [0, 0];
  5. clients[1] = serveraccept(0);
  6. clients[2] = serveraccept(0);
  7.  
  8. // Send a message to both of them
  9. serversend(clients[1], "Hello, you are number 1");
  10. serversend(clients[2], "Hello, you are number 2");
  11.  
  12.  
  13. // Close the connections
  14. servercloseconnection(clients[1]);
  15. servercloseconnection(clients[2]);
  16.  
  17.  
  18. // Stop listening
  19. serverclosemain();

This is not FlexSim-specific by the way.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.