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. how to implement this code for multiple clients using c sharp

how to implement this code for multiple clients using c sharp

Scheduled Pinned Locked Moved C#
csharplinqgraphicssysadminjson
9 Posts 7 Posters 1 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.
  • M Offline
    M Offline
    Member 10619797
    wrote on last edited by
    #1

    the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it. SERVER code:-

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.Remoting;
    using System.Linq;
    using System.Text;
    using System.Net .Sockets ;
    using System.Windows.Forms;
    using System.Threading ;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Net;

    namespace simpleserver
    {
    public partial class Form2 : Form
    {
    private readonly int port;
    private TcpClient client;
    private TcpListener server;
    //static public int MAX_CONN = 5;
    private NetworkStream mainstream;
    //private string ipList;
    //TcpListener myList = null;

    //static int thrd = -1;

        private readonly Thread Listening;
        private readonly Thread Getimg;
        public Form2(int Port)
        {
            port = Port;
            client = new TcpClient();
            Listening = new Thread(StartListening);
            Getimg = new Thread(ReceiveImage);
            InitializeComponent();
        }
    
    
        private void StartListening()
        {
            while (!client.Connected)
            {
                //ipList = new string\[MAX\_CONN\];
                //myList = new TcpListener(hostIP, hostPort);
                //myList.Start();
    
               server.Start();
               client = server.AcceptTcpClient();
    
               
            }
            Getimg.Start();
            
        }
        private void stop()
        {
            server.Stop();
            client = null;
            if (Listening .IsAlive )Listening .Abort ();
            if ( Getimg .IsAlive )Getimg .Abort ();
        }
        private void ReceiveImage()
        {
            BinaryFormatter binformatter = new BinaryFormatter();
            while (client.Connected)
            {
                mainstream = client.GetStream();
                pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
              //  pictureBox2 .Image =(Image )binformatter .Deserialize (mainstream );
    
            }
    
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            server = new TcpListener(IPAddress .Any ,port  );
            Listening .Start ();
        }
    
    OriginalGriffO P P P J 5 Replies Last reply
    0
    • M Member 10619797

      the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it. SERVER code:-

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Runtime.Remoting;
      using System.Linq;
      using System.Text;
      using System.Net .Sockets ;
      using System.Windows.Forms;
      using System.Threading ;
      using System.Runtime.Serialization.Formatters.Binary;
      using System.Net;

      namespace simpleserver
      {
      public partial class Form2 : Form
      {
      private readonly int port;
      private TcpClient client;
      private TcpListener server;
      //static public int MAX_CONN = 5;
      private NetworkStream mainstream;
      //private string ipList;
      //TcpListener myList = null;

      //static int thrd = -1;

          private readonly Thread Listening;
          private readonly Thread Getimg;
          public Form2(int Port)
          {
              port = Port;
              client = new TcpClient();
              Listening = new Thread(StartListening);
              Getimg = new Thread(ReceiveImage);
              InitializeComponent();
          }
      
      
          private void StartListening()
          {
              while (!client.Connected)
              {
                  //ipList = new string\[MAX\_CONN\];
                  //myList = new TcpListener(hostIP, hostPort);
                  //myList.Start();
      
                 server.Start();
                 client = server.AcceptTcpClient();
      
                 
              }
              Getimg.Start();
              
          }
          private void stop()
          {
              server.Stop();
              client = null;
              if (Listening .IsAlive )Listening .Abort ();
              if ( Getimg .IsAlive )Getimg .Abort ();
          }
          private void ReceiveImage()
          {
              BinaryFormatter binformatter = new BinaryFormatter();
              while (client.Connected)
              {
                  mainstream = client.GetStream();
                  pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
                //  pictureBox2 .Image =(Image )binformatter .Deserialize (mainstream );
      
              }
      
          }
          protected override void OnLoad(EventArgs e)
          {
              base.OnLoad(e);
              server = new TcpListener(IPAddress .Any ,port  );
              Listening .Start ();
          }
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Apart from that being terrible code - VS default names for everything, commented out blocks, assumptions that users never type mistakes, ignoring exception detail when you do catch them, and so forth - we have no idea what you mean by "make it work for multiple clients", or what you problem with doing it yourself is. And when you add that we aren't here to do your job for you I think it's very unlikely that you will get what you want with this question. Instead, sit down and work out exactly what you are trying to do, and work out what you need to implement in order to do it. Then make a list of what you do and don;t know how to do, and implement the stuff you do. For the stuff you don't start with google, and ask specific questions when you can't find what you need, or don't understand whatyou find. But just saving "provide help to implement it" isn't going to get you very far...

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M Member 10619797

        the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it. SERVER code:-

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Runtime.Remoting;
        using System.Linq;
        using System.Text;
        using System.Net .Sockets ;
        using System.Windows.Forms;
        using System.Threading ;
        using System.Runtime.Serialization.Formatters.Binary;
        using System.Net;

        namespace simpleserver
        {
        public partial class Form2 : Form
        {
        private readonly int port;
        private TcpClient client;
        private TcpListener server;
        //static public int MAX_CONN = 5;
        private NetworkStream mainstream;
        //private string ipList;
        //TcpListener myList = null;

        //static int thrd = -1;

            private readonly Thread Listening;
            private readonly Thread Getimg;
            public Form2(int Port)
            {
                port = Port;
                client = new TcpClient();
                Listening = new Thread(StartListening);
                Getimg = new Thread(ReceiveImage);
                InitializeComponent();
            }
        
        
            private void StartListening()
            {
                while (!client.Connected)
                {
                    //ipList = new string\[MAX\_CONN\];
                    //myList = new TcpListener(hostIP, hostPort);
                    //myList.Start();
        
                   server.Start();
                   client = server.AcceptTcpClient();
        
                   
                }
                Getimg.Start();
                
            }
            private void stop()
            {
                server.Stop();
                client = null;
                if (Listening .IsAlive )Listening .Abort ();
                if ( Getimg .IsAlive )Getimg .Abort ();
            }
            private void ReceiveImage()
            {
                BinaryFormatter binformatter = new BinaryFormatter();
                while (client.Connected)
                {
                    mainstream = client.GetStream();
                    pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
                  //  pictureBox2 .Image =(Image )binformatter .Deserialize (mainstream );
        
                }
        
            }
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                server = new TcpListener(IPAddress .Any ,port  );
                Listening .Start ();
            }
        
        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #3

        First of all, have you considered using

        code

        pre tags when posting code? It makes it much easier to read. Second, what have you attempted so far?

        "I've seen more information on a frickin' sticky note!" - Dave Kreskowiak

        P 1 Reply Last reply
        0
        • P Paul Conrad

          First of all, have you considered using

          code

          pre tags when posting code? It makes it much easier to read. Second, what have you attempted so far?

          "I've seen more information on a frickin' sticky note!" - Dave Kreskowiak

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          (remedied)

          1 Reply Last reply
          0
          • M Member 10619797

            the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it. SERVER code:-

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Runtime.Remoting;
            using System.Linq;
            using System.Text;
            using System.Net .Sockets ;
            using System.Windows.Forms;
            using System.Threading ;
            using System.Runtime.Serialization.Formatters.Binary;
            using System.Net;

            namespace simpleserver
            {
            public partial class Form2 : Form
            {
            private readonly int port;
            private TcpClient client;
            private TcpListener server;
            //static public int MAX_CONN = 5;
            private NetworkStream mainstream;
            //private string ipList;
            //TcpListener myList = null;

            //static int thrd = -1;

                private readonly Thread Listening;
                private readonly Thread Getimg;
                public Form2(int Port)
                {
                    port = Port;
                    client = new TcpClient();
                    Listening = new Thread(StartListening);
                    Getimg = new Thread(ReceiveImage);
                    InitializeComponent();
                }
            
            
                private void StartListening()
                {
                    while (!client.Connected)
                    {
                        //ipList = new string\[MAX\_CONN\];
                        //myList = new TcpListener(hostIP, hostPort);
                        //myList.Start();
            
                       server.Start();
                       client = server.AcceptTcpClient();
            
                       
                    }
                    Getimg.Start();
                    
                }
                private void stop()
                {
                    server.Stop();
                    client = null;
                    if (Listening .IsAlive )Listening .Abort ();
                    if ( Getimg .IsAlive )Getimg .Abort ();
                }
                private void ReceiveImage()
                {
                    BinaryFormatter binformatter = new BinaryFormatter();
                    while (client.Connected)
                    {
                        mainstream = client.GetStream();
                        pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
                      //  pictureBox2 .Image =(Image )binformatter .Deserialize (mainstream );
            
                    }
            
                }
                protected override void OnLoad(EventArgs e)
                {
                    base.OnLoad(e);
                    server = new TcpListener(IPAddress .Any ,port  );
                    Listening .Start ();
                }
            
            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            As I suppose you have found, a point-to-point socket connection is not what you want for this. You will likely find success by using a Web Service or similar, but I expect a database connection would be even simpler because you wouldn't actually need to implement anything on the server.

            Richard Andrew x64R 1 Reply Last reply
            0
            • M Member 10619797

              the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it. SERVER code:-

              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.Data;
              using System.Drawing;
              using System.Runtime.Remoting;
              using System.Linq;
              using System.Text;
              using System.Net .Sockets ;
              using System.Windows.Forms;
              using System.Threading ;
              using System.Runtime.Serialization.Formatters.Binary;
              using System.Net;

              namespace simpleserver
              {
              public partial class Form2 : Form
              {
              private readonly int port;
              private TcpClient client;
              private TcpListener server;
              //static public int MAX_CONN = 5;
              private NetworkStream mainstream;
              //private string ipList;
              //TcpListener myList = null;

              //static int thrd = -1;

                  private readonly Thread Listening;
                  private readonly Thread Getimg;
                  public Form2(int Port)
                  {
                      port = Port;
                      client = new TcpClient();
                      Listening = new Thread(StartListening);
                      Getimg = new Thread(ReceiveImage);
                      InitializeComponent();
                  }
              
              
                  private void StartListening()
                  {
                      while (!client.Connected)
                      {
                          //ipList = new string\[MAX\_CONN\];
                          //myList = new TcpListener(hostIP, hostPort);
                          //myList.Start();
              
                         server.Start();
                         client = server.AcceptTcpClient();
              
                         
                      }
                      Getimg.Start();
                      
                  }
                  private void stop()
                  {
                      server.Stop();
                      client = null;
                      if (Listening .IsAlive )Listening .Abort ();
                      if ( Getimg .IsAlive )Getimg .Abort ();
                  }
                  private void ReceiveImage()
                  {
                      BinaryFormatter binformatter = new BinaryFormatter();
                      while (client.Connected)
                      {
                          mainstream = client.GetStream();
                          pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
                        //  pictureBox2 .Image =(Image )binformatter .Deserialize (mainstream );
              
                      }
              
                  }
                  protected override void OnLoad(EventArgs e)
                  {
                      base.OnLoad(e);
                      server = new TcpListener(IPAddress .Any ,port  );
                      Listening .Start ();
                  }
              
              P Offline
              P Offline
              Philippe Mori
              wrote on last edited by
              #6

              Well, even though as others as pointed out, this is not good code... One easy answer is simply that each client code append say a block of a given length before the data for identification purpose (assuming this is for educative purpose).

              Philippe Mori

              1 Reply Last reply
              0
              • P PIEBALDconsult

                As I suppose you have found, a point-to-point socket connection is not what you want for this. You will likely find success by using a Web Service or similar, but I expect a database connection would be even simpler because you wouldn't actually need to implement anything on the server.

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                PIEBALDconsult wrote:

                but I expect a database connection would be even simpler

                Isn't it not good to expose the database across the network like that?

                The difficult we do right away... ...the impossible takes slightly longer.

                P 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  PIEBALDconsult wrote:

                  but I expect a database connection would be even simpler

                  Isn't it not good to expose the database across the network like that?

                  The difficult we do right away... ...the impossible takes slightly longer.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  I assume it's a local network, but if not, then a Web Service may provide better security.

                  1 Reply Last reply
                  0
                  • M Member 10619797

                    the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it. SERVER code:-

                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel;
                    using System.Data;
                    using System.Drawing;
                    using System.Runtime.Remoting;
                    using System.Linq;
                    using System.Text;
                    using System.Net .Sockets ;
                    using System.Windows.Forms;
                    using System.Threading ;
                    using System.Runtime.Serialization.Formatters.Binary;
                    using System.Net;

                    namespace simpleserver
                    {
                    public partial class Form2 : Form
                    {
                    private readonly int port;
                    private TcpClient client;
                    private TcpListener server;
                    //static public int MAX_CONN = 5;
                    private NetworkStream mainstream;
                    //private string ipList;
                    //TcpListener myList = null;

                    //static int thrd = -1;

                        private readonly Thread Listening;
                        private readonly Thread Getimg;
                        public Form2(int Port)
                        {
                            port = Port;
                            client = new TcpClient();
                            Listening = new Thread(StartListening);
                            Getimg = new Thread(ReceiveImage);
                            InitializeComponent();
                        }
                    
                    
                        private void StartListening()
                        {
                            while (!client.Connected)
                            {
                                //ipList = new string\[MAX\_CONN\];
                                //myList = new TcpListener(hostIP, hostPort);
                                //myList.Start();
                    
                               server.Start();
                               client = server.AcceptTcpClient();
                    
                               
                            }
                            Getimg.Start();
                            
                        }
                        private void stop()
                        {
                            server.Stop();
                            client = null;
                            if (Listening .IsAlive )Listening .Abort ();
                            if ( Getimg .IsAlive )Getimg .Abort ();
                        }
                        private void ReceiveImage()
                        {
                            BinaryFormatter binformatter = new BinaryFormatter();
                            while (client.Connected)
                            {
                                mainstream = client.GetStream();
                                pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
                              //  pictureBox2 .Image =(Image )binformatter .Deserialize (mainstream );
                    
                            }
                    
                        }
                        protected override void OnLoad(EventArgs e)
                        {
                            base.OnLoad(e);
                            server = new TcpListener(IPAddress .Any ,port  );
                            Listening .Start ();
                        }
                    
                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    Context for the solution helps. So is this a commercial solution, something for school, experimenting, etc. As others have noted there are standard idioms that you can research for doing client/server setups. If you want to roll you own then 1. Separate out communication from processing. Different classes. So you need classes that 'send' a message and classes that 'receive' a message. Then different classes that package a 'desktop image' as a message 2. On the server side when a server accepts a client connection then the server spins up a new thread to handle that request (for messages between the original client and the server.) It does that for each client and perhaps for each message that a unique client sends. A quick glance at your code suggests that you are not creating a new thread for each request.

                    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