简体   繁体   中英

How to write a tcp server use Future or .net async or others?

yes,there is so many sample for a tcp server,but I can't find one use scala future or c#/f# async/awite

Is future/async suitable for writing a simple tcp server,like echo server?

or if there is a server/client modle like smtp,the server and client will talk many times in a session(helo/ok from/ok rcpt/ok data/ok quit/ok),is future or async suitable for this modle?is there some possible that the serve A first get HELO from client A but then talk to client B with other smtp command like "mail from"?

where to find the sample code that a echo server use Future or async/awit/Task?

Thanks!

TCP servers are pretty complex beasts, and since the data is a stream (not a series of bounded packets), it isn't usually directly amenable to just an "await some data" API. The raw sockets API has an async component, but it isn't the shape you would normally expect.

Kestrel can be used to construct an async TCP server using the "pipelines" API, which deals with all the things like back-buffer management (for when you can't yet consume an incomplete frame) - I have a blog series here on that , however: no official client-side API exists (yet) for "pipelines"; again, the same blog series discusses how you can use Pipelines.Sockets.Unofficial to bridge that gap.

However! I wonder if what you really want here is a message passing library that sits on top of the TCP layer, so you can just say "send a message" and "await a reply message". Many such libraries exist, but they invariably change the shape of the underlying data protocol, as they will be introducing "framing" etc. This means that they may not be suitable choices if you intend to implement a pre-existing protocol (as the choices won't align).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM