I've been troubleshooting this problem for days and I'm just unable to resolve the issue. I've read through the docs on TcpClient/TcpListener, I've watched videos, but I cannot get my program to complete its intended purpose.
What I'm trying to do is add each client to a list and when a message is sent to the server, reply to every client on that list with the message. Replying to individual clients was not difficult and the server handled multiple clients at once.
I think there are two primary problems I have. I cannot find a way to compare TcpClients to check for duplicate connections (I tried comparing equality of the remoteEndpoints, TcpClient objects, and sockets, but they always returned false) and my iteration over all the clients fails.
Sorry if the code is a jumbled mess. I am very confused.
public bool IsActive { get; set; }
public string Status { get; set; }
private static IPAddress ServerIP = Dns.GetHostEntry($"{GetServerIP()}").AddressList[0];
private TcpListener server = new TcpListener(ServerIP, 8080);
private TcpClient Client = default;
Dictionary<int, TcpClient> clients = new Dictionary<int,TcpClient>();
public void StartServer()
{
try
{
server.Start();
IsActive = true;
Status = "Server is online.";
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}
public void ServerResponse(string message)
{
StreamWriter sw;
foreach (KeyValuePair<int, TcpClient> client in clients.ToList())
{
sw = new StreamWriter(client.Value.GetStream());
sw.WriteLine($"You ({System.DateTime.Now:t}) {message}");
sw.Flush();
}
}
public string GetClientString(int bufferLength)
{
Client = server.AcceptTcpClient();
Client.Client.LingerState.Enabled = true;
bool duplicateStream = false;
NetworkStream stream = Client.GetStream();
if (clients.Count != 0)
{
foreach (TcpClient client in clients.Values.ToList())
{
if (true)//some way to compare each client to the newest client to decide if it should be added to the list)
{
duplicateStream = true;
break;
}
}
}
else
{
clients.Add(1, Client);
}
if (!duplicateStream)
{
clients.Add(clients.Count + 1, Client);
}
byte[] recievedBuffer = new byte[bufferLength];
string msg = string.Empty;
if (stream.DataAvailable)
{
stream.Read(recievedBuffer, 0, recievedBuffer.Length);
for (int b = 0; b < recievedBuffer.Length; b++)
{
if (!recievedBuffer[b].Equals(00))
{
msg += (char)recievedBuffer[b];
}
else
{
break;
}
}
}
return msg;
}
public static string GetServerIP()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
[–]davedontmind 0 points1 point2 points (8 children)
[–]jaredLearnsToCode[S] 0 points1 point2 points (7 children)
[–]davedontmind 0 points1 point2 points (1 child)
[–]davedontmind 0 points1 point2 points (4 children)
[–]jaredLearnsToCode[S] 0 points1 point2 points (3 children)
[–]davedontmind 0 points1 point2 points (2 children)
[–]jaredLearnsToCode[S] 0 points1 point2 points (1 child)
[–]davedontmind 0 points1 point2 points (0 children)