Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TCPClient to multiple systems

TCPClient to multiple systems

Scheduled Pinned Locked Moved C#
csharphelpwpfwinformssysadmin
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gopi Nath
    wrote on last edited by
    #1

    Hello Everybody, I am writing a sender and receiver application, sender in C#,WPF and receiver in C# WinForms. In sender, will be having a list of task to do, which will be sent to multiple receivers running in multiple systems. Here is the code in Sender

    for (int j = 0; j < tmpfilled.Count; j++)
    {
    // some statements related to send to receiver
    ////////////
    Thread thFromReceiver = new Thread(new ThreadStart(thFromReceiverFunction));
    thFromReceiver.Start();
    }

    //Body of the thFromReceiverFunction()

    public void thFromReceiverFunction()
    {
    // here again two thread, for read and write
    try
    {
    TcpClient client = new TcpClient();
    IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip_address), portNo);
    client.Connect(serverEndPoint);

          Thread readThreadRec = new Thread(new ParameterizedThreadStart(readFromReceiver));
          Thread writeThreadRec = new Thread(new ParameterizedThreadStart(writeToReceiver));
    
          readThreadRec.Start(client);
          writeThreadRec.Start(client);
     }
     catch(Exception ex)
     {
    
     }
    

    In Receiver,

    // inside a thread
    TcpListener tcpListener = new TcpListener(IPAddress.Any, portNo); // same port number
    tcpListener.Start();

    while (true)
    {
         //blocks until a client has connected to the server
         TcpClient client = tcpListener.AcceptTcpClient();
    
         Thread readThread = new Thread(new ParameterizedThreadStart(read\_thread));
         readThread.IsBackground = true;
    
         Thread writeThread = new Thread(new ParameterizedThreadStart(write\_thread));
         writeThread.IsBackground = true;
    
         readThread.Start(client);
         writeThread.Start(client);
    
         readThread.Join();
         writeThread.Join();
    
         client.Close();
     }
    

    My problem is, if I am sending to single receiver, everything is working fine. If multiple receivers are running in multiple systems, then, ipaddress which I am sending the request, shows right, but whichever the client receives the first request, all other requests are going to the same client, even though I am sending to different ipaddress (through thFromReceiverFunction() funtion). I think I explained the issue properly, if not please let me know. Thanks in advance. Regards, Gopinath.

    L 1 Reply Last reply
    0
    • G Gopi Nath

      Hello Everybody, I am writing a sender and receiver application, sender in C#,WPF and receiver in C# WinForms. In sender, will be having a list of task to do, which will be sent to multiple receivers running in multiple systems. Here is the code in Sender

      for (int j = 0; j < tmpfilled.Count; j++)
      {
      // some statements related to send to receiver
      ////////////
      Thread thFromReceiver = new Thread(new ThreadStart(thFromReceiverFunction));
      thFromReceiver.Start();
      }

      //Body of the thFromReceiverFunction()

      public void thFromReceiverFunction()
      {
      // here again two thread, for read and write
      try
      {
      TcpClient client = new TcpClient();
      IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip_address), portNo);
      client.Connect(serverEndPoint);

            Thread readThreadRec = new Thread(new ParameterizedThreadStart(readFromReceiver));
            Thread writeThreadRec = new Thread(new ParameterizedThreadStart(writeToReceiver));
      
            readThreadRec.Start(client);
            writeThreadRec.Start(client);
       }
       catch(Exception ex)
       {
      
       }
      

      In Receiver,

      // inside a thread
      TcpListener tcpListener = new TcpListener(IPAddress.Any, portNo); // same port number
      tcpListener.Start();

      while (true)
      {
           //blocks until a client has connected to the server
           TcpClient client = tcpListener.AcceptTcpClient();
      
           Thread readThread = new Thread(new ParameterizedThreadStart(read\_thread));
           readThread.IsBackground = true;
      
           Thread writeThread = new Thread(new ParameterizedThreadStart(write\_thread));
           writeThread.IsBackground = true;
      
           readThread.Start(client);
           writeThread.Start(client);
      
           readThread.Join();
           writeThread.Join();
      
           client.Close();
       }
      

      My problem is, if I am sending to single receiver, everything is working fine. If multiple receivers are running in multiple systems, then, ipaddress which I am sending the request, shows right, but whichever the client receives the first request, all other requests are going to the same client, even though I am sending to different ipaddress (through thFromReceiverFunction() funtion). I think I explained the issue properly, if not please let me know. Thanks in advance. Regards, Gopinath.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You have shown all the code related to receivers, but it is the sender that is causing the problem. Please edit your question and show the problem code.

      G 1 Reply Last reply
      0
      • L Lost User

        You have shown all the code related to receivers, but it is the sender that is causing the problem. Please edit your question and show the problem code.

        G Offline
        G Offline
        Gopi Nath
        wrote on last edited by
        #3

        Hello Richard, Thanks for your reply. First two session in earlier post is the code from sender. Here are the code for the functions mentioned in the thread.

        public void readFromReceiver(object clients)
        {
        ASCIIEncoding encoder = new ASCIIEncoding();
        TcpClient client = (TcpClient)clients;
        NetworkStream clientStream = (client).GetStream();
        byte[] msg_type = new byte[1]; // to read header
        bool flag = false;
        while(!flag)
        {
        try
        {
        // need to get back msg from receiver about status
        int bytesRead = 0;
        bytesRead = clientStream.Read(msg_type, 0, 1);
        int msg = (int)(msg_type[0]);
        string retVal = "";
        if (msg == 3) // return success
        {
        retVal = receiveString(clientStream); // return message
        }
        }
        catch(Exception ex)
        {

              }
          }
        

        }

        public void writeToReceiver(object clients)
        {
        ASCIIEncoding encoder = new ASCIIEncoding();
        TcpClient client = (TcpClient)clients;
        NetworkStream clientStream = (client).GetStream();

          bool flag = false;
          while(!flag)
          {
               while (nQReceiveProjName.Count > 0) // list of request
               {
                   try
                   {
                       string cmdToPass = fnGetXMLString(nQReceiveProjName\[0\]);
                       nQReceiveProjName.RemoveAt(0);
                       sendCommand(clientStream, 5);
                       sendString(clientStream, cmdToPass);
                   }
                   catch(Exception ex)
                   {
                   }
               }
           }
        

        }

        In both the cases, client remote end point is holding the ip address of the client in which the first request is received. Regards, Gopinath.

        L 1 Reply Last reply
        0
        • G Gopi Nath

          Hello Richard, Thanks for your reply. First two session in earlier post is the code from sender. Here are the code for the functions mentioned in the thread.

          public void readFromReceiver(object clients)
          {
          ASCIIEncoding encoder = new ASCIIEncoding();
          TcpClient client = (TcpClient)clients;
          NetworkStream clientStream = (client).GetStream();
          byte[] msg_type = new byte[1]; // to read header
          bool flag = false;
          while(!flag)
          {
          try
          {
          // need to get back msg from receiver about status
          int bytesRead = 0;
          bytesRead = clientStream.Read(msg_type, 0, 1);
          int msg = (int)(msg_type[0]);
          string retVal = "";
          if (msg == 3) // return success
          {
          retVal = receiveString(clientStream); // return message
          }
          }
          catch(Exception ex)
          {

                }
            }
          

          }

          public void writeToReceiver(object clients)
          {
          ASCIIEncoding encoder = new ASCIIEncoding();
          TcpClient client = (TcpClient)clients;
          NetworkStream clientStream = (client).GetStream();

            bool flag = false;
            while(!flag)
            {
                 while (nQReceiveProjName.Count > 0) // list of request
                 {
                     try
                     {
                         string cmdToPass = fnGetXMLString(nQReceiveProjName\[0\]);
                         nQReceiveProjName.RemoveAt(0);
                         sendCommand(clientStream, 5);
                         sendString(clientStream, cmdToPass);
                     }
                     catch(Exception ex)
                     {
                     }
                 }
             }
          

          }

          In both the cases, client remote end point is holding the ip address of the client in which the first request is received. Regards, Gopinath.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I don't see anything anywhere where you are storing or using an IP address. Once a socket connection is established between server and client, that socket remains open until closed by either end. Each message using the correct socket endpoint will be sent to the correct client. Google for "client server C#" to see other examples.

          G 1 Reply Last reply
          0
          • L Lost User

            I don't see anything anywhere where you are storing or using an IP address. Once a socket connection is established between server and client, that socket remains open until closed by either end. Each message using the correct socket endpoint will be sent to the correct client. Google for "client server C#" to see other examples.

            G Offline
            G Offline
            Gopi Nath
            wrote on last edited by
            #5

            Here is where I am using the ip address to send the request.

            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip_address), portNo);
            client.Connect(serverEndPoint);

            Let me check in google also as you mentioned. Thanks again. Regards, Gopinath.

            L 1 Reply Last reply
            0
            • G Gopi Nath

              Here is where I am using the ip address to send the request.

              IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip_address), portNo);
              client.Connect(serverEndPoint);

              Let me check in google also as you mentioned. Thanks again. Regards, Gopinath.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You only do that once, when the client first connects. From then on you must use just the socket that is returned on the connection.

              G 1 Reply Last reply
              0
              • L Lost User

                You only do that once, when the client first connects. From then on you must use just the socket that is returned on the connection.

                G Offline
                G Offline
                Gopi Nath
                wrote on last edited by
                #7

                Hello Richard, There was an issue from my side. While sending the request (writetosender() function), I am not segregating the request according to the client. Now it is fixed and working fine. Thanks again. Regards, Gopinath.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups