How to send data to all connected socket connections using Tcpclient from another class

public void send(string _msg)
        {
            a2 = Encoding.ASCII.GetBytes(_msg);
            networkStream = clientSocket.GetStream();
            networkStream.Write(a2, 0, a2.Length);
            networkStream.Flush();
        }

This send () method sends data to all clients, but I call this method when I click a button from another class, and then the send () method sends data to the last connected client.

+3
source share
2 answers

TCP- , clientSocket . , , , , , , clientSocket ( ). , . ( clientSocket):

public void sendMessage(Socket clientSocket, string _msg)
{
    a2 = Encoding.ASCII.GetBytes(_msg);
    networkStream = clientSocket.GetStream();
    networkStream.Write(a2, 0, a2.Length);
    networkStream.Flush();
}

, , (, ), . :

ArrayList arrSocket = new ArrayList(); // Declaration for holding dynamic array of socket
........
arrSocket.Add(tcpListener.AcceptSocket()); // For adding new connected client to the dynamic array
........
foreach (object obj in arrSocket) // Repeat for each connected client (socket held in a dynamic array)
{
    Socket socket = (Socket)obj;
    sendMessage(socket, "your message here"); // call the above sendMessage function for sending message to a client
}
+3

. , . .

+2

All Articles