TCP server and simultaneously communicate with multiple clients

A PAC R1, used as a TCP server, can use one port to listen. Does the two PC act as clients and communicate with the server at the same time as tcp/ip?

Could you please explain what you are trying to do?

I am not sure I follow your question.

The PAC R1 can listen and transmit on many different TCP and UDP port numbers at the same time.
Just make sure you program each communication handle in your charts.

SoftPAC running on a PC can do the same, listen and transmit on many different TCP and UDP ports at the same time.

If using the R1 as a server with multiple clients communicating to the same R1 TCP port, you can check which client is communicating with you by using the SendCommunicationHandleCommand with “get.src” and “get.srcport” in the Command parameter.

I want the server to use only one port to listen for multiple client and server communications. Instead of multiple ports to listen. In the c# development platform, you can do this by threading. What should I do in Pac control? Could you draw a flow chart for me? For example, a R1 server and 3 PC customer service terminals.
Thank you very much for your help. Thank you

Here is an outline of just one of the many ways you might go about it.

The block names are the commands you would use.

1 Like

excuse me
1、Do the “listen for incoming”and “accept incoming” statements use the same communication handle?

2、Which communication handle is used for each PC communication?

3、Do each PC use a communication handle, or do they share one?

Thank you

  1. Yes. The same.
  2. The same one.
  3. They share the same one.

Because all three share the same comm handle, you need to use the get.src to know which PC you are talking/listening to.

Please re-read the post in this thread by Philip.

Thank you. I see.
They share the same communication handle.
That is, only one PC can communicate with PAC through this communication handle at the same time. At the end of this PC communication, other PC can communicate with PAC through this communication handle.
Am I correct?

The comm handle will buffer some of the data, so you can work through that in the chart.

Take a moment to read the command reference manual, it will help you a lot.
Here is what the get.src mentions in the manual;

When accepting incoming connections, it is sometimes necessary to determine the IP address from which the connection originated. To accomplish this you would do the following in OptoScript:

Result = SendCommunicationHandleCommand(chComHandle, “get.src” );

Result would be set to the address of the peer. For instance, if the connection originated from, the value of Result, in hex, would be:


(C0 = 192, A8 = 168, 01 = 1, 0C = 12)

Are you trying to maintain some sort of state for the clients that are connecting? If not, then it really doesn’t matter who the connection is coming from, just serve out what was requested - for the Opto controller it doesn’t matter if it is coming from one computer or a hundred different ones. The single comm handle can handle multiple TCP connections at the same time, but you write your strategy as if you are handling a single request at a time (since you are) - the TCP/IP stack in the controller will buffer the other requests so it will appear to your strategy like they are coming in sequentially.

The only time you need to know the source IP and port is if you are maintaining state for your clients. That is a lot more difficult to do (on Opto and c#), but it can be done.

I see. Thank you!
In c#, multithreading is used to implement this functionality
A port in the TCP server only listens continuously. When a connection request is made with a new client, a new thread is created, communicating with the client using a new Socekt ().

In order to complete a connection, the server must issue the accept () function specifying the socket number that was specified in the prior listen () call (passive connection establishment). A new socket is created with the same properties as the initial one. This new socket is connected to the client’s socket, and its number is returned to the server. The initial socket is thereby free for other clients that might want to connect with the server.