My server code is currently reading String
data over a TCP connection form 2 different client computers. To do this I am duplicating the StreamReader
functions for the two separate clients. How can I simplify my code into just one method without having to duplicate for every client? (I want to do 4 clients in the future, duplicating a part of the code 4 times is very tedious and clumsy).
public class AsynchIOServer
{
static TcpListener tcpListener = new TcpListener(15);
static TcpListener tcpListener2 = new TcpListener(10);
static void Listeners()
{
using (Socket socketForClient = tcpListener.AcceptSocket())
{
if (socketForClient.Connected)
{
Console.WriteLine("Client:" + socketForClient.RemoteEndPoint + " now connected to server.");
using (NetworkStream networkStream = new NetworkStream(socketForClient))
//using (NetworkStream networkStream2 = new NetworkStream(socketForClient))
using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream))
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream))
//using (System.IO.StreamReader streamReader2 = new System.IO.StreamReader(networkStream2))
{
try
{
while (true)
{
string theString = streamReader.ReadLine();
if (string.IsNullOrEmpty(theString) == false)
{
Console.WriteLine("Kinect1:" + theString);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
Console.WriteLine("Press any key to exit from server program");
Console.ReadKey();
}
//---------------------------------------------------------------------------
static void Listeners2()
{
using (Socket socketForClient2 = tcpListener2.AcceptSocket())
{
if (socketForClient2.Connected)
{
Console.WriteLine("Client:" + socketForClient2.RemoteEndPoint + " now connected to server.");
//using (NetworkStream networkStream = new NetworkStream(socketForClient))
using (NetworkStream networkStream2 = new NetworkStream(socketForClient2))
using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream2))
//using (System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream))
using (System.IO.StreamReader streamReader2 = new System.IO.StreamReader(networkStream2))
{
try
{
while (true)
{
string theString2 = streamReader2.ReadLine();
if (string.IsNullOrEmpty(theString2) == false)
{
Console.WriteLine("Kinect2:" + theString2);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
Console.WriteLine("Press any key to exit from server program");
Console.ReadKey();
}
public static void Main()
{
tcpListener.Start();
tcpListener2.Start();
Console.WriteLine("************This is Server program************");
Console.WriteLine("How many clients are going to connect to this server?:");
int numberOfClientsYouNeedToConnect =int.Parse( Console.ReadLine());
for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
{
Thread newThread = new Thread(new ThreadStart(Listeners));
newThread.Start();
}
for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
{
Thread newThread2 = new Thread(new ThreadStart(Listeners2));
newThread2.Start();
}
}
}
Listeners2
? Did you forget to paste it? Please post the complete code, without editing it. Only then we can suggest you how to optimize it. \$\endgroup\$