I am having issues with the following code. I need to create a server that is multi-threaded that handles 3 threads. Right now the server will connect to 3 threads but on the forth it won't give the server busy but instead throws an exception. Also, I can't get the server to print to the listbox. Bare with me as I am very new to this. After a client disconnects the bool array is reset and that isn't working either. Any help would be appreciated.
Code:
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
ThreadGPSServerRun gps = new ThreadGPSServerRun(serverlist);
gps.RunServer();
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void serverlist_SelectedIndexChanged_1(object sender, System.EventArgs e)
{
}
}
class ThreadGPSServerRun
{
public bool[] used = new bool[3 + 1]; // boolean array + 1 more element
public ListBox servprint;
public ThreadGPSServerRun(ListBox a)
{
servprint = a;
}
public void RunServer()
{
TcpListener server = null;
int port = 5757;
Socket connection = null;
NetworkStream socketStream = null;
BinaryWriter writer = null;
//wait for a client connection and display the text the client sends
try
{
// In the server class with thread pool:
Thread[] threads = new Thread[3]; // a thread pool of 3 Threads
Person[] persons = new Person[3]; // an array pool of 3 Person objects
bool[] used = new bool[3 + 1]; // boolean array + 1 more element
server = new TcpListener(port); // bound to port 5757
server.Start(); // start the server socket
this.servprint.Text = "Waiting for connection...";
while (true)
{
Socket clientS = server.AcceptSocket(); // wait for next client, 4th denied!
int i = 0;
while (used[i] == true) i++; // scan for the first free thread
if (i < 3)
{ // if it is an accepted client
used[i] = true; // indicate this thread is now being used
persons[i] = new Person(clientS, this, i); // or ( clientS, this, i ) if needed
threads[i] = new Thread(new ThreadStart(persons[i].runThread));
threads[i].Start(); // start this thread to handle this client connection
}
else
{
// create NetworkStream object associated with socket
socketStream = new NetworkStream(clientS); // get stream
// chain it to BinaryWriter
writer = new BinaryWriter(socketStream); // chain it to BinaryWriter
// if it is the 4th denied client
writer.Write("Server is busy...."); // send server busy message to the denied client through clientS
}
}
}
catch (Exception e)
{
servprint.Text = e.ToString();
}
connection.Close(); // close client connection (not TcpListener!)
}
}
class Person
{
double income = 100;
int exemptions = 1;
int b;
string msg;
BinaryWriter writer = null;
BinaryReader reader = null;
NetworkStream socketStream = null;
Socket clientServer;
ThreadGPSServerRun gps;
public Person(Socket cServer, ThreadGPSServerRun ts, int a)
{
clientServer = cServer;
gps = ts;
b = a;
}
public void runThread()
{
// create NetworkStream object associated with socket
socketStream = new NetworkStream(clientServer); // get stream
// chain it to BinaryWriter and BinaryReader
writer = new BinaryWriter(socketStream); // chain it to BinaryWriter
reader = new BinaryReader(socketStream); // and BinaryReader
// get client's IP address and port??
IPEndPoint ipend = (IPEndPoint)clientServer.RemoteEndPoint;
// write the request to the client
writer.Write("What is your total income");
// read the request from the client
income = reader.ReadString();
// write the request to the client
writer.Write("How many exemptions do you have?");
// read the request from the client
exemptions = reader.ReadString();
calculator(income, exemptions);
// show where the client is from and the HTTP request
gps.servprint.Text = "Connection from " + ipend.Address
+ " via its port " + ipend.Port;
writer.Write(msg);
gps.used[b] = false;
clientServer.Close();
}