Link to home
Start Free TrialLog in
Avatar of deepu Lovelesh
deepu LoveleshFlag for India

asked on

tcp socket buffer problem

I am having a tcp socket which is receiving data but the data is not received as a whole. the buffer size is declared as private static byte[] result = new byte[0x1000];. the code that i am using to get the data is

private void Anal_Cmd(byte[] recv, int nrecv)
        {
            String str;
            str = Encoding.UTF8.GetString(recv);
            SetText(str);
}

kindly help how i can receive the complete data as a whole as it is not providing the whole data.


thanks in advance experts
Avatar of Aditya Milan
Aditya Milan
Flag of India image

you need to loop through until complete buffer is read. use concatenation operator to store the value in str to preserv complete data. In the end use SetText
Avatar of deepu Lovelesh

ASKER

kindly help out with the code
thanks
use this function to read the buffer

private byte[] GetSocketData(Socket socket, int lenght)
{
    byte[] data = new byte[lenght];  
    int size = lenght;
    var total = 0;
    var dataleft = size;

    while (total < size) 
    {
        var recv = socket.Receive(data, total, dataleft, SocketFlags.None);
        if (recv == 0)
        {
            data = null;
            break;
        }
        total += recv;
        dataleft -= recv;
    }
    return data;
}

Open in new window

i tried but did not get the desired result. request to help out . sharing whole code to check.
private void btnStart_Click(object sender, EventArgs e)
        {
            m_stripport = Convert.ToInt32(textBox_ip.Text);
            //IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPAddress ip = IPAddress.Parse("10.0.0.102");
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(ip, m_stripport));
            serverSocket.Listen(10);

            Thread myThread = new Thread(ListenClientConnect);
            myThread.Start();
            Console.ReadLine();
           
        }
        private void ListenClientConnect()
        {
            while (true)
            {
                Socket clientSocket = serverSocket.Accept();
                clientSocket.Send(Encoding.UTF8.GetBytes("Server Say Hello"));
                //clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));
                Thread receiveThread = new Thread(ReceiveMessage);
                receiveThread.Start(clientSocket);
            }
        }
       
        private void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            while (true)
            {
                try
                {
                    int receiveNumber = myClientSocket.Receive(result);
                    Anal_Cmd(result, receiveNumber);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }

            }

        }

        private void Anal_Cmd(byte[] recv, int nrecv)
        {
            String str;
            str = Encoding.ASCII.GetString(recv);
            SetText(str);

      }
thanks in advance. will be a great help .
ASKER CERTIFIED SOLUTION
Avatar of deepu Lovelesh
deepu Lovelesh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial