7

I am developing an app, in which c sharp server communicates with android client. Server needs to send multiple message to the Android tcpClient. As to send message I have to close the tcpClient Object on the server. otherwise it does not send. Once tcpClient is closed how can i communicate with my client again , how can i keep track and send multiple messages, once i close tcpClient, or there is any other way of sending without closing it. If the question is still unclear, please comment below

it sends one message easlity but i need to send more messages from time to time

Here is the snippet of code for server

//in a thread
void receivingMessages(object param)
    {
        try
        {
            var paramArray = (object[])param;
            var id = paramArray[0];
            var client = paramArray[1] as TcpClient;

            var stream = client.GetStream();

            while (true)
            {
                byte[] buffer = new byte[2048];
                int bytesRead = stream.Read(buffer, 0, 2048);

                if (bytesRead > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    string v = Encoding.ASCII.GetString(buffer);

                    int index = v.IndexOf('\0');
                    string trimmedXml = v.TrimEnd(new char[] { '\0' });

                    var root = XDocument.Parse(trimmedXml).Root;
                    //to get the type of xml like it is login register or message
                    string xmlType = root.Name.ToString();

                    //some checks     
                    string result = " server messages";
                    SendMessage(client, result);

                }

                //Thread.Sleep(10);
            }
        }
        catch (Exception)
        {

        }

    }


    public void SendMessage(TcpClient client, string message)
    {

        byte[] buffer = Encoding.ASCII.GetBytes(message);

        NetworkStream stream = client.GetStream();
        stream.Write(buffer, 0, buffer.Length);

        client.Close();
    }
}
}
0

1 Answer 1

3

Try this:

public void SendMessage(TcpClient client, string message)
{

    //byte[] buffer = Encoding.ASCII.GetBytes(message);

    NetworkStream stream = client.GetStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.WriteLine(message);
    writer.Flush();

}
Sign up to request clarification or add additional context in comments.

9 Comments

@L.B It sounds to me like he hadn't been able to send the message without closing the stream, but he wants to keep it open: "is there any way of sending without closing..." so I rewrote it to write the message, without closing.
@0_______0 still not able to send message, unless i close it tcpclient
@mindFreezer Maybe try setting client.NoDelay=true; in the SendMessage method, I tested my code and it worked fine so I'm not sure what is wrong.
@0_______0 hmmm, i will give it a try, then will let u know
didn't work this way either, now i send a response to server after every message so server will surely have tcpclient to send to ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.