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.

servercreatemain(1880);

// Wait for two clients to connect
Array clients = [0, 0];
clients[1] = serveraccept(0);
clients[2] = serveraccept(0);

// Send a message to both of them
serversend(clients[1], "Hello, you are number 1");
serversend(clients[2], "Hello, you are number 2");


// Close the connections
servercloseconnection(clients[1]);
servercloseconnection(clients[2]);


// Stop listening
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.

Zoe Liu avatar image Zoe Liu commented ·

Hi @Mischa Spelt ,I realized connect two ports at the same time by calling servercreatemain twice last night, but when I disconnected and tried to reconnect, the model would prompt "Server unable to bind port 8888" (8888 is my first port number) , and the second time I can only receive the information sent by the second port.Do you know why?

0 Likes 0 ·