user6609676
user6609676

Reputation:

C# Socket Send the string multiple times (How to remove the while(true) loop)

I have a C# server that sends a string to an Android JAVA Client.

My Android code already receives once but I want to send that string many times per second.

        public void serverthread()
    {
        Thread serverthread = new Thread(server);
        serverthread.Start();
    }


    public void server()
    {

        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.2.5"),6000));
        estado = 1;
        socket.Listen(100);

        Socket accept = socket.Accept();

        while (true)
        {
            buffer = Encoding.Default.GetBytes("Hello World");
            accept.Send(buffer, 0, buffer.Length, 0);
        }
}

Eventually my Android Client crashes when I'm using this while(true) loop. How can i improve my implementation?

Upvotes: 0

Views: 175

Answers (2)

Ashkan S
Ashkan S

Reputation: 11471

I couldn't understand what is your question completely, but if you want to prevent your client from crashing, you probably have to let it use your data. after all the client is slower than your server. use Thread.Sleep(time); like this

    while (true)
    {
        buffer = Encoding.Default.GetBytes("Hello World");
        accept.Send(buffer, 0, buffer.Length, 0);
        Thread.Sleep(10);
    }

Upvotes: 1

Rahul
Rahul

Reputation: 77846

Use Timer Class instead and on every Elapsed event do send the string to your Android client

Upvotes: 0

Related Questions