Question how to ensure about sending multiple serial data from server to client

pooya1072

New member
Joined
Apr 23, 2019
Messages
1
Programming Experience
1-3
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.
Server side code:
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();
    }
}
Client side code:
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?
 
Last edited:
Your server code is not thread safe. You have only one pClient in a static class variable. What happens when two connections or more connections are accepted near simultaneously and the execution of AcceptClient() and or Send() overlap each other?
 

Latest posts

Back
Top Bottom