question

kamleshjha-5590 avatar image
0 Votes"
kamleshjha-5590 asked TimonYang-MSFT commented

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

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-generaldotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@kamleshjha-5590
Is Multipleconnect your custom class?
Could you please provide more complete information so that we can reproduce your problem?

0 Votes 0 ·

0 Answers