I am trying to write a client/server application for TCP or UDP actually at this point the protocol is not important, I am trying to sort out the logic / flow diagram of the problem.
I will initially configure 50 different physical machines and on each machine there will be three servers running. There will then be one client machine that will communicate with each of these servers. Each server will perform a different a different task and hence has to be kept separated from the others.
The problem I am having is about the logic of this system.
The client machine will have to create a connection to each server, (50 *3) so 150 client connections have to be made. Originally I wanted to have two threads per client, one to send data and one to receive data. The reason being that data can be sent without receiving data and vice versa so something like
Expand|Select|Wrap|Line Numbers
- NetworkStream myData = socket.GetStream();
- while(true)
- {
- //block and wait for read
- myData.read();
- ProcessData();
- myData.Send(Response);
- }
I was thinking of implementing something more like
Expand|Select|Wrap|Line Numbers
- ReceiveThread()
- {
- While(true){
- //block and wait for data to arrive
- myData.Receive()
- //Use delegates and events to notify data has been received and parsed
- Notify_application_logic_data_received(data)
- }
- }
- SendThread()
- {
- Array SendBuffer = Empty;
- While(true)
- {
- //Block and wait for data to be placed in send buffer
- WaitForNotification();
- myData.send(SendBuffer);
- }
- }
- Main()
- {
- receivedata += notify(ReadData);
- mainSend();
- }
- ReadData()
- {
- /whatever has to be done
- }
- mainSend()
- {
- //append data to SendBuffer
- //wake up SendThread to send the data
- }
I then looked at asynchronous communications and it looks like I will have to constantly poll each connection, to see if data available to read and if the application logic has provided some data to send.
Has anybody encountered such a problem before. Is 300 threads to much to have ? If each thread is actually blocked until it is woken up either by an event saying data available to read or by me saying here is some data to send so wake up. As you have probably noticed this is C# code on a windows machine.
Any suggestions will be greatly appreciated.