Winsock and C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Guyz
well I wanna implement a tcp chat program with C# and winsock, and I know
that I should use Asynchronous methods so my windows program won't be
blocked, but in my application both clients could send and receive messages
at the same time, which means I can not begin recieving after sending a
message or vice versa, it's possible that my application would recieve
multiple messages, so how should I handle that?
Regards,
 
Hola Jefe,


Jefe said:
Hi Guyz
well I wanna implement a tcp chat program with C# and winsock, and I know
that I should use Asynchronous methods so my windows program won't be
blocked

not really, you can use a thread that send/receive info, once it gets/send
the info it send an event to the UI thread. IMO this is the easiest approach
, but in my application both clients could send and receive messages
at the same time,

You could use two threads, one for send and another to receive, you will
have to make sure that there is no concurrency conflict though.
An easier solution is to use one thread only and in a loop check for data to
send, send it, check for data received, receive it and notify the UI and
repeat the loop again.
which means I can not begin recieving after sending a
message or vice versa, it's possible that my application would recieve
multiple messages, so how should I handle that?

No multiple messages, you have to receive them sequentially.
 
| You could use two threads, one for send and another to receive, you will
| have to make sure that there is no concurrency conflict though.

That is the way I would go. Have two threads - sender and receiver
contained in your Chat Class. GUI has nothing to do with it at this point.
Your sender will block on a blocking circular queue (i.e. my one lock
blocking CQ on codeproject) and when it gets a post, it sends. Likewise
your receiver will block on networkstream.Read() and enque into receiver
queue. You could then do a gui ThreadWorker that blocks on receive queue
and posts to your textbox.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top