Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to connect single client to multiple servers . now my client able to connect to one server only . I made code which works fine to connects to multiple servers. through different or same ports. but the issue is that it comunicates with both servers but when i send message from server to this client it replies to first server only one time and and to 2nd server multiple times. i dont get why it happends ? Is it broke connection with 1st server after connecting second?



here is my program for two server connections ,

C#
public partial class Form1 : Form
   {
       int counter = 0;

       public Form1()
       {
           InitializeComponent();
       }

       Socket clsock;

       private void button1_Click(object sender, EventArgs e)
       {
           Thread th = new Thread(() => StartClient());
           th.Start();
       }


       public void StartClient()
       {
           try
           {
               clsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
               int port = Convert.ToInt32(txtport.Text);
               IPAddress ip = IPAddress.Parse(txtip.Text);
               clsock.Connect(ip, port);
               counter += 1;
               AppendlblData(Convert.ToString(counter) + " Server Connected " + clsock.RemoteEndPoint + Environment.NewLine);
               Thread th = new Thread(() => ReceiveData());
               th.Start();
           }
           catch { }
       }


       public void ReceiveData()
       {
           try
           {
               for (int i = 0; i >= 0; i++)
               {
                   byte[] data = new byte[128];
                   clsock.Receive(data);
                   string dataReceived = Encoding.ASCII.GetString(data);
                   char[] c = { '*', '$', '!', '@' };

                   if (dataReceived != null)
                   {
                       string res = dataReceived.TrimStart(c);
                       MessageBox.Show(res);
                       // SendKeys.SendWait(res);
                   }
               }
           }

           catch(Exception es)
           {
               if (clsock.Connected !=true)
               {
                   StopClient();
                   AppendlblData("Connection closed from Server");
               }
           }
       }


       private void Form1_Load(object sender, EventArgs e)
       {
           MaximizeBox = false;
       }


       public void AppendlblData(string myval)
       {
           if (InvokeRequired)
           {
               this.Invoke(new Action<string>(AppendlblData), new object[] { myval });
               return;
           }
           richtxtbox.Text += myval;
       }


       private void button2_Click(object sender, EventArgs e)
       {
           StopClient();
       }

       public void StopClient()
       {
           try
           {
               AppendlblData("Disconnected from Server -" + clsock.RemoteEndPoint + Environment.NewLine);
               clsock.Close();
           }
           catch { }
       }
   }
Posted
Updated 2-Aug-15 18:54pm
v2

1 solution

You do exactly what you are doing, but have multiple client variables, and update SendDataToApp to have client as a variable, not a global so that it uses the correct client info for sending data.

see:
http://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread[^]
 
Share this answer
 
Comments
Member 11543226 1-Aug-15 2:26am    
how to initialise multiple client variables

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900