Reputation: 1431
I need to write a TCP client to communicate with a server. Looking at code samples (Socket, TcpClient?), I don't seem to be able to find anything which feels like a "modern" way to do approach this, given what I've seen elsewhere in my C# adventures.
I suppose specifically I was expecting to see Tasks and code like await socket.Connect(args)
, but instead I see callback-based async or BeginX methods...
What are my options, here? Do I just get on with it and work with something without TPL, or are there other approaches?
Many thanks!
Upvotes: 2
Views: 977
Reputation: 456457
There's no TAP-based raw socket API, no. I believe that the BCL team took a look at the Socket
class - which already supports a complete synchronous API and two complete asynchronous APIs - and decided that adding a third complete asynchronous API would just be too much.
It's easy enough to use Task.Factory.FromAsync
to wrap the existing Begin
/End
methods. Personally, I like to do this with extension methods so they can be called more naturally.
But first you should take a step back and see if there's any way to avoid using raw sockets. In particular, see if SignalR is a possibility. Writing correct raw socket code is extremely difficult, and using a higher-level abstraction would be much easier.
Upvotes: 6