Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey, i am writting a chat based application based on c# .net 3.5 and mysql database. It uses sockets for tcp client and server,both of this instances run on one form for chatting,because i need a server aka TcpLister and client aka TcpClient for the whole chatting process,where i use the server for listerning to incoming connections and the client for outgoing request.

So i'm having a thread on each of my classes one for server and one on the other class for client. so i'm wondering is there an easier method for doing this and do i really need to threads running on one form? and any suggestions on how can i abstract the complexity of these TCP instances? maybe like a .net class i can use for both incoming and outgoing connections?

By the way this application is just for my interest in socket programming, i'm a first year student with 9+ months c# experience from php.
Thanks inadvance
Shauny
Posted

Hello, mshauny

I think, I can make it even more complex on an abstract level. You might think, that using two threads sounds complicated, but that is because of the [TCP/IP protocol>] has a good level of complexity, per se. This is theory :-D .

The implementation: the base socket model implementation goes back to "Berkeley socket model" for handling all kinds of network communications. An implementation of this model can be run in one of two modes, one is synchron (wait until the operation is ready) and asynchron (look if this kind of operation event is there and, if it is not there leave the operation, otherwise run the operation) the other one; per socket and independently. But either way will open a single independent socket running on different i/o-channels for safety reasons.

So, there will be a good and, of course, a bad answer, and the good one will promptly follow.

If you really anticipate not to waste much time by communicating over the chat channel (r/w on the network), then you are able to use one thread for all the actions - listening (which is really accepting connections) and communicating - looping one after another in asynchron mode, only; but you will have some work with timing considerations.

If a socket is run in synchron mode, there is nothing you can do to circumvent a two-threads application, because of accepting (incomming) connections - if nothing is to accept, the application is really at one with the world and appreciated nothing else but waiting -- a would-be running chat communication channel will be waiting forever.

By the way, how many chat channels will your application open, :-\ ?

The chat client may work as a round-robin task dispatcher (no threads, but in principle) or the chat channels may write their messages into a queue...

As you see, there is much to be considered...

regards,

:cool:h.p.
(software-forge-stghp)

-- don't regret a bad accent - regret a soft brain...:thumbsup:
 
Share this answer
 
Thank you very much,and sorry for returning late. I will write the application to my level best then upload it here so that you guys can help mee improve it,after all its for open source.

and as for the channels, it will support peer-To-peer chat and one open chat.

Thank you very much
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900