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]); } } }
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"); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)