Multi-Threaded Server Help

This is a discussion on Multi-Threaded Server Help within the C# Programming forums, part of the General Programming Boards category; I am having issues with the following code. I need to create a server that is multi-threaded that handles 3 ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    1

    Multi-Threaded Server Help

    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();
    }

  2. #2
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    Code:
    ...
    while (used[i] == true) i++; // scan for the first free thread
    ...
    This seems like it would try to enumerate higher than your "3+1" range. or at least its possible.

    Whats the exception thats being thrown
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  3. #3
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    I got the exception on
    Code:
    ...
    income = reader.ReadString();
    ...
    saying that it hits the end of the file, you should check for end of file.
    But i was able to connect 5 instances where 2 of them had

    Code:
    ↕Server is busy....
    Are you hitting end of line exception?

    why do you not read in double and int since thats what those are.

    Code:
    // read the request from the client
    income = reader.ReadDouble();
    
    // write the request to the client
    writer.Write("How many exemptions do you have?");
    // read the request from the client
    exemptions = reader.ReadInt32();
    and it looks like your thread is trying to access items within the main form.

    and you also have

    Code:
    writer.Write(msg);
    but msg is only instantiated and never used.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    C# throws an exception when the end of file is reached? That's got to be the dumbest thing in the entire universe.

  5. #5
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    haha, well the exception i got said unable to read beyond the stream.
    he was doing ReadString() into a double though.
    I do not get this exception when i do ReadDouble().

    Im just showing what I got, since he did not post the exception he got
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by AtomRiot View Post
    haha, well the exception i got said unable to read beyond the stream.
    he was doing ReadString() into a double though.
    I do not get this exception when i do ReadDouble().

    Im just showing what I got, since he did not post the exception he got
    Well sure, I'm not saying you're dumb, I'm saying C# is dumb :-)

    Actually, my limited experience with it has all been good, but what I'm hearing now does not bode well. If reaching the end of a file is considered an "exceptional" event in C# I hate to guess what other completely normal things are considered exceptional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 06:31 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21