Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Here is my updated code which is used to established a multiple connection and send data to device/client from server but my server does not receive a data from client, how to solve this issue please help me to solve this issue.

What I have tried:

Below code is my updated code from single connection to multiple connection. When I select a client from the list of connected device my server send the data to client easily but my server does not receive a data from client.
Code:


This section of code is established a multiple connection with device
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";
              }

          }




      }



This code for sending and receiving data. server send data to client successfully but this server does not receive a data from client.
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);
           }

       }
Posted
Updated 15-Apr-21 20:23pm
v3

1 solution

You were given some suggestions over a week ago at How to connect multiple devices with server using TCP?[^].
 
Share this answer
 
Comments
Member 14743579 15-Apr-21 6:06am    
Actually my code established a connection with multiple devices/clients and after when i select a device from a list of connected device my server send a messages to client perfectly but my server is not able to read a data or message from client.
Richard MacCutchan 15-Apr-21 6:07am    
Why not? If you can send data to a client you should be able to receive as well.
Member 14743579 15-Apr-21 6:44am    
I know but every time socket is empty when my server trying to read the messages comes form client.
Greg Utas 15-Apr-21 7:55am    
Maybe the clients aren't sending properly. Or maybe you're not reading the socket that the server allocated when it accepted the connection.
Richard MacCutchan 15-Apr-21 8:50am    
Then you need to use the debugger to find out why.

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