471,605 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Winsock and C#

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,
Jan 31 '06 #1
3 3306
Hola Jefe,
"Jefe" <Je**@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
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.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 31 '06 #2
Jefe,
if you check this sample google search
http://www.google.com/search?hl=en&q...%22tcp+chat%22
You will see that there are numerous implementations of chat that you can
study.

You do not need to use Winsock directly in C# as the .NET classes provide
this functionality for you.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Jefe" wrote:
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,

Jan 31 '06 #3
| 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.
--
wjs
Jan 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Niels Borg | last post: by
1 post views Thread by Chris Thompson | last post: by
1 post views Thread by Yu Chai | last post: by
5 posts views Thread by kc | last post: by
1 post views Thread by Nicolas Ghesquiere | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.