Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm writing code to send random numbers to client. The code at server is below. But I got trouble with receiving random numbers at client and store them in a collection, and send back message to server that client already received them
C#
public static void SendCallingNumber(string Message)
        {
            StreamWriter swSenderSender;
            // Create an array of TCP clients, the size of the number of users we have
            TcpClient[] tcpClients = new TcpClient[ServerController.htUsers.Count];
            // Copy the TcpClient objects into the array
            ServerController.htUsers.Values.CopyTo(tcpClients, 0);
            // Loop through the list of TCP clients
            for (int i = 0; i < tcpClients.Length; i++)
            {
                // Try sending a message to each
                try
                {
                    // If the message is blank or the connection is null, break out
                    if (Message.Trim() == "" || tcpClients[i] == null)
                    {
                        continue;
                    }
                    // Send the message to the current user in the loop
                    swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                    swSenderSender.WriteLine(Message+" is calling number");
                    swSenderSender.Flush();
                    swSenderSender = null;
                    
                }
                catch // If there was a problem, the user is not there anymore, remove him
                {
                    RemoveUser(tcpClients[i]);
                }
            }
        }

In the client, I wrote code to accept these messages

C#
ArrayList numbers= new ArrayList();
private void btnConnect_Click(object sender, EventArgs e)
 {
     // If we are not currently connected but awaiting to connect
     if (Connected == false)
     {
         // Initialize the connection
         ipAddr = IPAddress.Parse(txtIP.Text);
         tcpServer = new TcpClient();
         tcpServer.Connect(ipAddr, 1986);
         Connected = true;
         UserName = txtUsername.Text;
         btnConnect.Text = "Disconnect";
         SendMessage(txtUsername.Text);
         thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
         thrMessaging.Start();
     }
     else
     {
         CloseConnection("Disconnected at user's request.");
     }
 }
 private void SendMessage(string message)
 {
     swSender = new StreamWriter(tcpServer.GetStream());
     swSender.WriteLine(message);
     swSender.Flush();
 }
 private void ReceiveMessages()
 {
     srReceiver = new StreamReader(tcpServer.GetStream());
     string strReceive = srReceiver.ReadLine();
     if (strReceive[0] == '1')
     {
         this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "Connected Successfully!" });
     }
     else
     {
         string Reason = "Not Connected: ";
         Reason += strReceive.Substring(2, strReceive.Length - 2);
         this.Invoke(new CloseConnectionCallback(this.CloseConnection), new object[] { Reason });
         return;
     }
     while (Connected)// I think problem is here
     {
         this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { srReceiver.ReadLine() });
         numbers.Add(srReceiver.ReadLine().Substring(0,2));//This code will make a latency in client
       // if server send a random numbers, client cannot receive
         SendMessage("Already received random numbers");// This code will send back many same message to server.


     }
 private void UpdateLog(string strMessage)
 {
     // Append text also scrolls the TextBox to the bottom each time
     txtContent.AppendText(strMessage + "\r\n");
 }

Could anyone give me any advice for this issue? Thanks for any help
Posted
Updated 16-Apr-12 15:59pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Apr-12 17:39pm    
"I got trouble..." is not informative. You should provide more descriptive explanation of the problem.
--SA
anh chau 16-Apr-12 21:23pm    
As I explained in the code, when I write code in the loop while(connected), server can send another random number, and client cannot receive, there's an latency in client,in this code clients can receive a string like :Message+" is calling number", this message will display on the textbox of clients. I cannot save these random numbers in a collection and send back to server that clients already received the random numbers from server.
Thanks for any help.
Eugene Sadovoi 20-Apr-12 16:44pm    
Are you trying to acheive duplex communication where server and a client send data at the same time?
What exactly does not work in your code?
Your question is a bit ambigous, it is hard to understand what are you looking for.

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