0

Are there any fast way to learn Asynchronous socket tcp programming or are there any libraries available to utilize system.socket in a short amount of time?

My synchronous tcp client couldn't hold the program I'm working on because it needs to listen for a long period of time (10 to 60 seconds), which of course doesn't work in synchronous connection.

Thanks.

stivlo
  • 83,644
  • 31
  • 142
  • 199
user741630
  • 23
  • 5
  • I suppose you could quickly whip up some code copycatting from msdn's async [server](http://msdn.microsoft.com/en-us/library/5w7b7x5f.aspx) and [client](http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx) samples. then again, by the time it took you and me to post you could probably have mastered the basics already... – mtijn Aug 09 '11 at 11:39
  • this post may be a (partial) duplicate of this: http://stackoverflow.com/questions/104617/what-is-a-good-tutorial-howto-on-net-c-socket-programming in which is refered to an old but great MSDN tutorial on sockets – mtijn Aug 09 '11 at 11:44
  • @mtijn: Actually I checked msdn and googled for hours for possible solutions, but I couldn't get them to work. How about libraries are there any? I'm using c# – user741630 Aug 09 '11 at 11:57
  • well what's wrong with just the .NET Sockets? They support async sockets too, as demonstrated in the 2 articles of my first comment. if you have a demo of your attempts post it so we can better help you overcome any difficulties. – mtijn Aug 09 '11 at 12:12
  • This is a perfect use case for the Async CTP - there are multiple samples on using the async/await keywords to use the waiting time to do other useful work. – Stephen Chung Aug 10 '11 at 02:35

1 Answers1

0

I finally got it to work. I stand corrected. It appears that an synchronous socket does work for listening for long period of time. The reason why my earlier attempt wasn't working is because I'm using a NetworkStream, but I found out that it's possible to use a socket without a NetworkStream.

Socket server = new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream, ProtocolType.Tcp);

Then use

socket.Send and socket.Receive

dandan78
  • 13,328
  • 13
  • 64
  • 78
user741630
  • 23
  • 5