Click here to Skip to main content
15,112,662 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.

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);

             //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);

      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)

              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)));

      public void User(Socket client)
          while (true)
                  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);

           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);



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

Updated 15-Apr-21 21:23pm

1 solution

You were given some suggestions over a week ago at How to connect multiple devices with server using TCP?[^].
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.
Member 14743579 16-Apr-21 1:54am
I tried the the debugger also but it is not working for me.
Member 14743579 16-Apr-21 2:21am
I have updated my code from single connection to multiple connection. This code established a multiple connection with device and shows a list of connected devices. When I select a device from the list of connected device and try to send message to client it's working fine but When I trying to receive a message from client it does not work. In the above code (ns.available) is always shows me false, It means data is not available in networkstream. Anyone have an idea about it, please help me to resolve this issue.
Richard MacCutchan 16-Apr-21 5:02am
Your code only checks a single socket, and if no data is available it does nothing. You should have a loop that checks each socket in turn, and processes any data waiting to be read, while ignoring sockets that have none waiting.
Member 14743579 16-Apr-21 5:52am
When I check the all sockets one by one it shows me every time empty. I mean there is no data available to read.
I have another question if I sent the message to particulars client using socket then why I am not receiving messages from client using same socket
Richard MacCutchan 16-Apr-21 6:19am
It is impossible to tell. As I suggested earlier, you need to use the debugger. The only way to find out what is happening is to run the code and examine the various variables inside the different activities as it is running.

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