Winsock and C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jefe

    #1

    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,
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Winsock and C#

    Hola Jefe,


    "Jefe" <Jefe@discussio ns.microsoft.co m> wrote in message
    news:E2FDA5C5-5227-4BF7-938C-8A1F38F09319@mi crosoft.com...[color=blue]
    > 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[/color]

    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
    [color=blue]
    >, but in my application both clients could send and receive messages
    > at the same time,[/color]

    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.
    [color=blue]
    >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?[/color]

    No multiple messages, you have to receive them sequentially.


    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    Comment

    • Peter Bromberg [C# MVP]

      #3
      RE: Winsock and C#

      Jefe,
      if you check this sample google search

      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:

      UnBlog:





      "Jefe" wrote:
      [color=blue]
      > 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,[/color]

      Comment

      • William Stacey [MVP]

        #4
        Re: Winsock and C#

        | 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.R ead() and enque into receiver
        queue. You could then do a gui ThreadWorker that blocks on receive queue
        and posts to your textbox.
        --
        wjs


        Comment

        Working...