Help Designing Async Socket Program
Posted on 2004-11-18
I am working on a rewriting a chat program. It's a peer-2-peer chat program that seeks out new connections as well as listening for incoming connections. I have a working model in Delphi, but I'm trying to port it to VB.Net and eventually to PocketPC. I'm just having some trouble with the conversion and figuring out how to do things in DotNet.
The general design is as follows...
2. Launch a few hundred Async connects to clients on a certain port for my current sub-net. This is only done once at startup.
3. Start listening for incoming connections on a certain port for incoming connections
4. If anything connects to us, or we establish a connection with a remote machine, start a new thread to handle the conversation
5. ASync connections that are refused or timeout are closed
Looking at converting this from the old handles and API calls to DotNet, I plan on having three classes.
* Listener class *
Listens for an incoming connection on a port, accepts the connection and launches a new Chat thread to handle incoming connections. There would only be a single instance of this class.
* Connector class *
Tries connecting to multiple remove machines on a certain port. If a connection is established, start a new chat client thread to handle incoming connections. So far I just store this as the Async state, then pull it out again in the OnConnect.
* Chat class *
Performs actual communications with an established connection initiaed by one of the previous two classes.
It will contain a timer to send out keep-alive packets (just chr(255)) every few seconds to let the other end know we're still here
I have written an ASync port/subnet scanner that works well. I understand that part. I have made a simple listener that responds with current time. So far I haven't used any threading, just BeginConnect, BeginSend, etc.
Q1. What would the best way to design these classes, and do I need to create a seperate thread for each chat client? Does my current design look alright?
Q2. How do I pass the newly established connection from the connector or listener to the chat class?
Q3. Can I get by with ASync callbacks, or will I need threads for this?
I assume this is one question, but I'll make new question if I must.