Click here to Skip to main content
15,613,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to connect multiple devices with server using same port number with the help TCP but i am not able to understand the logic to how to connect multiple devices with server using same port and how to communicate with device after the selection of device from the list of connected device?

What I have tried:

I have tried this code to establish a connection between server and device and after that they communicate to each other and the good news is this code is working perfectly to communicate with a device but the limitation is the server is not connected with the multiple device at a same time using same port number. please help me to solve this problem.

Here is my running code and communicate with a single device a time with same port number..
public void Connnection()

              string Systemip = getlocalip();
              txtinfo.Text = "Server IP:" + Systemip + Environment.NewLine;
              var portno = Int32.Parse("8010");

              String a = "";

              IPAddress ip = IPAddress.Parse(Systemip);

              //TcpListener server = new TcpListener(ip,portno);
              server = new TcpListener(ip, portno);

              //here environment.newline means, display msg to next line.
              txtinfo.AppendText("Server started waiting for client.............." + Environment.NewLine);
              socketforclient = server.AcceptSocket();

              if (socketforclient.Connected)
              //send messsage to client
              txtactivecolor.BackColor = Color.Green;
              comboxoption.Enabled = true;
              rdioch1.Enabled = true;
              checkBoxtemp.Enabled = true;
              ns = new NetworkStream(socketforclient);
                  StreamWriter writer = new StreamWriter(ns);

                  txtinfo.AppendText("Welcome Client you are connected perfectly:" + Environment.NewLine);
                  writer.WriteLine("Welcome Client by StreamWriter" + Environment.NewLine);

                  //get message from client
                  txtinfo.AppendText("BY Client:" + Environment.NewLine);

                  btnsend.Enabled = true;
                  btnstop.Enabled = true;
                  btnstart.Enabled = false;

              //Auto send command to the device on 2 april 2021
              writer.WriteLine("GA 10; SP 1; SR 5120; TL1024" + Environment.NewLine);
              txtinfo.AppendText("Server:" + "GA 10; SP 1; SR 5120; TL1024" + Environment.NewLine);
              writer.WriteLine("AQ" + Environment.NewLine);
              txtinfo.AppendText("Server:" + "AQ" + Environment.NewLine);


              //End auto send command to device...

              richtxtbddata.Text = "";
Updated 5-Apr-21 1:55am

Whenever a client connection request is accepted by the server, the system returns the new file descriptor for that socket. So you need to keep a list of all socketfds as the server accepts them in order to know which client you are communicating with. See Socket Programming in C/C++ - GeeksforGeeks[^] for details.
Share this answer
If you want to look at some C++ code for a thread that handles TCP, on either the server or client side, here[^] is an example. Start in the Enter function on line 332. This has been tested under load on Windows. The comments in its header[^] should also help.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900