How to connect multiple devices/client with my TCP server and how to receive messages from client?

kamlesh jha 1 Reputation point
2021-04-16T10:13:46.95+00:00

Here is my Code:
Below code established a multiple client/devices connection with server

private void btnstart_Click(object sender, EventArgs e)
{

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

              String a = "";

              IPAddress ip = IPAddress.Parse(Systemip);
              server = new TcpListener(ip, portno);
              server.Start();

             //here environment.newline means, display msg to next line.
              txtinfo.AppendText("Server started waiting for client.............." + Environment.NewLine);
          counter = 0;
          f = new Form1();
          Thread newone = new Thread(loop);
          newone.Start();


      }
      public delegate void messageone();
      public void mess()
      {

          richtxtbddata.AppendText(counter + "clien connected" + Environment.NewLine);
          richtxtbddata.AppendText("The client is from IP address: " + ((IPEndPoint)socketforclient.RemoteEndPoint).Address.ToString() + Environment.NewLine);
      //    iplist.Items.Add(((IPEndPoint)socketforclient.RemoteEndPoint).Address.ToString());
          listBox1.Items.Add(counter);         //add this (((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString()); on the place of counter
      }

      public void loop(object obj)
      {
          connectobj = new List<Multipleconnect>();

          while (true)
          {
              counter++;

              socketforclient = server.AcceptSocket();
             // ns = new NetworkStream(socketforclient);
              connectobj.Add(new Multipleconnect
              {
                  objectno = counter,
                  Skt = socketforclient,
                  nstream = new NetworkStream(socketforclient),
                  ip = ((IPEndPoint)socketforclient.RemoteEndPoint).Address.ToString()
              });

              //Box.AppendText(counter + "Client connected");
              richtxtbddata.Invoke(new messageone(mess));
              Thread UserThread = new Thread(new ThreadStart(() => f.User(socketforclient)));
              UserThread.Start();

          }
      }
      public void User(Socket client)
      {
          while (true)
          {
              try
              {
                  byte[] msg = new byte[1024];
                  int size = client.Receive(msg);
                  client.Send(msg, 0, size, SocketFlags.None);
              }
              catch (Exception ex)
              {
                  txtinfo.Text = "Divice Disconnected";
              }

          }

      }

Below code is for sending and receiving a messages but In this code messages sent client/device successfully but this code is not receive a messages from client/devices.

private void btnsend_Click(object sender, EventArgs e)
{

   isNew = true;

       if (servermsg.Text != "") {

           ns = new NetworkStream(socketforclient);
           StreamWriter writer = new StreamWriter(ns);

           writer.WriteLine(servermsg.Text + Environment.NewLine);
           txtinfo.AppendText("Server:" + servermsg.Text + Environment.NewLine);
           writer.Flush();
           writer.Close();
       }


       ns = new NetworkStream(socketforclient);
       StreamReader sr = new StreamReader(ns);
       string myCompleteMessage = string.Empty;

       if (ns.DataAvailable)
       {
           myReadBuffer = new byte[2048];
           datafinal = new double[1];

           myCompleteMessage = Encoding.ASCII.GetString(myReadBuffer, 0, readbytes);

       }

       //runn();



       if (myCompleteMessage != "")
       {
          txtinfo.AppendText("Client:" + myCompleteMessage + Environment.NewLine + Environment.NewLine);
       }

   }
SQL Server | Other
Developer technologies | C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.