server scenario - variables in the right spot?



i think i just realized i'm an idiot. again.

(not syntactically correct code... just pieces to illustrate)

class StateObject
{
members like socket, receiveBuffer, receiveBufferSize, StringBuilder
etc..
}

class program
{
//class globals
public AutoResetEvent clientConnected = new AutoResetEvent(false);
public AutoResetEvent sendDone = new AutoResetEvent(false);
public AutoResetEvent receiveDone = new AutoResetEvent(false);

static void main(string[] args)
{
//main server thread
StartServer();
}

static StartServer()
{
//main server thread
setup server socket, bind, listen, etc..
while(true)
{
socket.beginAccept(yada yada, AcceptCallback, listenerSocket);
clientConnected.WaitOne();
{
}

static void AcceptCallback(IAsyncResult ar)
{
//thread for handling client
clientConnected.Set(); //signal main thread to continue, keep
listening for and accept other clients
StateObject state = new StateObject();
state.ComSocket = ((socket)ar.AsyncState).EndAccept(ar);
receive(state);
receiveDone.WaitOne();
send(state);
sendDone.WaitOne();

and so on handling client's session
}

static void recieve(StateObject state)
{
does a state.ComSocket.BeginReceive passing state as the state
object
}

static void ReceiveCallback(IAsyncResult ar)
{
//client's worker thread
yada yada and
receiveDone.Set() //notify client's thread receive is done
}
}

my question pertians to the class global variables which are all
autoResetEvents above but I don't think the type matters with respect to my
question, but please do let me know if it does. When I follow scenario of a
client connecting this works out fine... actual code works also. But when I
think of scenario of many clients connecting I began questioning the
placement of my AutoResetEvents (class globals). What happens when many
clients are being serviced at the same time? Will they all be using those
SAME AutoResetEvents and screwing each other up?

If so, I assume moving them into the StateObject class so each client gets a
separate instance would solve the problem? Would this be a typical solution
or am I way off here with my server's code structure?

I've used similar setups for clients where its one instance of your program
running so no issue. Now that I'm 'attempting' to create a server the
scenario is obviously different, although I know not technically accurate,
i'm thinking of it like each client session is like another instance of your
program running, so those variables that are visible to the whole program
wind up being shared by all the client sessions?

I need enlightenment. again.



.



Relevant Pages

  • Re: Event Processing
    ... public event MessageArrivedHandler MessageArrivedLocally; ... static void Main ... Console.WriteLine("Event Server started, press to ... Client Code: ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Event Processing
    ... public class BroadcastEventWrapper: MarshalByRefObject ... static void Main ... Console.WriteLine("Event Server started, press to ... Client Code: ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Reading data on Aysnchronous socket server
    ... This could be because they wanted to save a buffer copy to join the ... | To test this I have a simulated client that sends 100 byte packets. ... | I have set up the socket server so that its buffer is bigger than this. ... | public static void AcceptCallback{ ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Create Form from Async-Callback
    ... Client that connects would open a Form. ... static void Main{ ... Control ctl = new Control; ... the most important part is that I added an SynchronizationContext ...
    (microsoft.public.dotnet.general)
  • Re: dip Notions 2 Major Errors
    ... "static void Main" is *NOT* the first executed line of code. ... configuration files, evaluates code access security policies, creates ... To the Runtime the client is ... writing an additional remoting channel, you implement their interface, ...
    (comp.object)