none
how to ensure about sending multiple serial data from server to client RRS feed

  • Question

  • My program is divided into two parts , a server and a client. when the client connects to the server , At this time, the server should send numbers 1 to 10 to the client. but only one or two first number is send.

    this is the server side : in the AcceptClient sub i create a for loop to send numbers 1 to 10 to client.

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    class Module1
    {
        private static TcpClient pClient;
        private static TcpListener Listener;
        private static System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
    
        static void Main()
        {
            mre.Reset();
            Listener = new TcpListener(IPAddress.Any, 6000);
            Listener.Start();
            Console.WriteLine("SERVER : Server is start ...");
            Listener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), Listener);
            mre.WaitOne();
        }
    
        static void AcceptClient(IAsyncResult ar)
        {
            pClient = Listener.EndAcceptTcpClient(ar);
            Listener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), Listener);
            for (int i = 1; (i <= 10); i++)
            {
                Module1.Send(i.ToString());
            }
    
        }
    
        public static void Send(string Messsage)
        {
            StreamWriter sendMessage = new StreamWriter(pClient.GetStream());
            sendMessage.WriteLine(Messsage);
            sendMessage.Flush();
        }
    }

    and this is client side :

    using System;
    using System.IO;
    using System.Net.Sockets;
    class Module1
    {
        private static TcpClient client;
        private static System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
    
        static void Main()
        {
            mre.Reset();
            try
            {
                client = new TcpClient("localhost", 6000);
                Console.WriteLine("CLIENT : client is connected ...");
                client.GetStream().BeginRead(new byte[] { 0 }, 0, 0, new AsyncCallback(read), client.GetStream());
            }
            catch (Exception ex)
            {
                Console.WriteLine("CLIENT : " + ex.Message);
            }
            mre.WaitOne();
        }
    
        static void read(IAsyncResult ar)
        {
            try
            {
                NetworkStream ns = (NetworkStream) ar.AsyncState;
                Int16 l = (Int16)ns.EndRead(ar);
                string msg = (new StreamReader(client.GetStream()).ReadLine());
                Console.WriteLine(msg);
                ns.BeginRead(new byte[] { 0 }, 0, 0, new AsyncCallback(read), ns);
            }
            catch (Exception ex)
            {
                Console.WriteLine("CLIENT : " + ex.Message);
                return;
            }
        }
    }

    run each side in a separate console and first run server side.

    i want to know how can i correct this code to send all of these numbers?



    • Edited by mahmoodi1072 Tuesday, April 23, 2019 2:35 PM Correct the Errors
    Tuesday, April 23, 2019 10:44 AM

Answers

  • Your msg = (new StreamReader(client.GetStream()).ReadLine()) should be changed to read buffer loop until some form of end-of-data marker is received in the buffer. (Since apparently you're using EOLN as marker, you're advised to use NetworkStream.Read() directly yourself, check for EOLN characters, then perform Encoding.GetString() yourself to convert to string. This gives additional benefit of preventing garbage character if your data contains multiple-byte-character-set characters)

    ReadLine can return as soon as there are *some* data in the networkstream until the buffer is emptied, however on network data written at once can arrive at multiple batches, especially when TCP resend mechanism is involved.

    This is not spelt out in StreamReader.ReadLine documentation because the documentation is for reading general form of Stream (file). You need to combine NetworkStream.Read() documentation and ReferenceSource implementation of StreamReader.ReadLine() to see this.



    Wednesday, April 24, 2019 5:58 AM
    Answerer
  • Hi

    Thanks for the feedback.

    I have reproduced your problem, you could try to add the following bold code in your server.

    Code:

     for (int i = 1; i <= 10; i++)
       {
                    Module1.Send(i.ToString());
                    Thread.Sleep(100);      
       }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, April 24, 2019 6:00 AM
    Moderator

All replies

  • The code you posted cannot compile. You are referencing variables that have never been declared. Your Send method is not even accepting the correct type that you are passing it. We'd be guessing to assume that the problem with the code you posted is actually the problem in your real code. On just a guess I believe the issue is with your read code as it doesn't look correct to me. However it references stuff that doesn't exist so I cannot tell.

    Is there any particular reason you're using the low level stream stuff rather than the far easier and cleaner TcpClient/TcpListener classes? I suspect your problems would go away if you were using them.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 23, 2019 1:47 PM
    Moderator
  • Sorry and thanks ... i fixed all errors.
    Tuesday, April 23, 2019 2:36 PM
  • Hi

    Thank you for posting here.

    It seems that your problem has been solved. If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 24, 2019 1:47 AM
    Moderator
  • Hi

    Thank you for posting here.

    It seems that your problem has been solved. If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    hi

    the main problem still remains .i just fixed the errors to run program .

    this errors was some syntax errors. but main problem is not about syntax


    Wednesday, April 24, 2019 3:11 AM
  • Your msg = (new StreamReader(client.GetStream()).ReadLine()) should be changed to read buffer loop until some form of end-of-data marker is received in the buffer. (Since apparently you're using EOLN as marker, you're advised to use NetworkStream.Read() directly yourself, check for EOLN characters, then perform Encoding.GetString() yourself to convert to string. This gives additional benefit of preventing garbage character if your data contains multiple-byte-character-set characters)

    ReadLine can return as soon as there are *some* data in the networkstream until the buffer is emptied, however on network data written at once can arrive at multiple batches, especially when TCP resend mechanism is involved.

    This is not spelt out in StreamReader.ReadLine documentation because the documentation is for reading general form of Stream (file). You need to combine NetworkStream.Read() documentation and ReferenceSource implementation of StreamReader.ReadLine() to see this.



    Wednesday, April 24, 2019 5:58 AM
    Answerer
  • Hi

    Thanks for the feedback.

    I have reproduced your problem, you could try to add the following bold code in your server.

    Code:

     for (int i = 1; i <= 10; i++)
       {
                    Module1.Send(i.ToString());
                    Thread.Sleep(100);      
       }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, April 24, 2019 6:00 AM
    Moderator
  • Your msg = (new StreamReader(client.GetStream()).ReadLine()) should be changed to read buffer loop until some form of end-of-data marker is received in the buffer. (Since apparently you're using EOLN as marker, you're advised to use NetworkStream.Read() directly yourself, check for EOLN characters, then perform Encoding.GetString() yourself to convert to string. This gives additional benefit of preventing garbage character if your data contains multiple-byte-character-set characters)

    ReadLine can return as soon as there are *some* data in the networkstream until the buffer is emptied, however on network data written at once can arrive at multiple batches, especially when TCP resend mechanism is involved.

    This is not spelt out in StreamReader.ReadLine documentation because the documentation is for reading general form of Stream (file). You need to combine NetworkStream.Read() documentation and ReferenceSource implementation of StreamReader.ReadLine() to see this.



    thanks ... i'll try it
    Wednesday, April 24, 2019 11:53 AM
  • Hi

    Thanks for the feedback.

    I have reproduced your problem, you could try to add the following bold code in your server.

    Code:

     for (int i = 1; i <= 10; i++)
       {
                    Module1.Send(i.ToString());
                    Thread.Sleep(100);      
       }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    thanks , it seems a good solution
    • Marked as answer by mahmoodi1072 Wednesday, April 24, 2019 11:54 AM
    • Unmarked as answer by mahmoodi1072 Wednesday, April 24, 2019 11:54 AM
    Wednesday, April 24, 2019 11:54 AM