Eric lippert - Amazon Web Services
Eric lippert - Amazon Web Services
Eric lippert - Amazon Web Services
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Real-time network<br />
applications in the past<br />
using .NET<br />
There are multiple ways to solve different scenarios regarding<br />
real-time systems using .NET. You might have found yourself<br />
using raw sockets to have full control over everything you do. The<br />
biggest problem is that if you are creating a real-time application<br />
using .NET and rely on sockets to help you, you most likely write a<br />
nice wrapper or library that you can use.<br />
As with anything else, this ends up in lots and lots of different<br />
libraries to solve real-time systems over sockets. There’s certainly<br />
nothing wrong with going down on the lowest possible layer,<br />
but sometimes abstractions can reduce complexity and speed up<br />
development.<br />
Next up, we need to connect to this server and to do this, we have<br />
the following code.<br />
var client = new Socket(SocketType.Stream, ProtocolType.<br />
Tcp);<br />
client.Connect(new IPEndPoint(IPAddress.<br />
Parse(“127.0.0.1”), 3425));<br />
var buffer = new byte[1024];<br />
while (true)<br />
{<br />
Console.WriteLine(“Connected to server!”);<br />
client.Receive(buffer);<br />
Console.WriteLine(Encoding.Default.GetString(buffer));<br />
}<br />
As you might have noticed, there are a couple of problems with<br />
the server and the client. First of all, the buffer is big and we don’t<br />
handle data sent which is more than 1024 bytes. This would need<br />
to be fixed if this was a real world application. Writing up all this<br />
boilerplate code for a real-time application, can be cumbersome<br />
and we tend to do the same thing over and over again. The next<br />
step when we have a client and a server, is to come up with a<br />
“protocol” to use and now I am not talking about the<br />
actually DTOs that are sent, but in what way they are sent.<br />
If you want to handle disconnects, reconnects and broadcasts,<br />
you’ll first and foremost need a basic list of all connected and<br />
possibly disconnected clients as you can see above. There’s<br />
absolutely nothing wrong with this, but so far we’ve just started<br />
to talk about the most basic things. Imagine what this adds up to<br />
once the real-time system grows.<br />
Next up, you need a unified language that all your clients and<br />
servers use, so that you can ensure that each entry point gets what<br />
they should.<br />
The following code shows how to create a basic server that listens<br />
on TCP for clients to connect and once they connect, it simply<br />
sends a message to the client greeting it.<br />
var socket = new Socket(SocketType.Stream, ProtocolType.<br />
Tcp);<br />
socket.Bind(new IPEndPoint(IPAddress.Parse(“127.0.0.1”),<br />
3425));<br />
socket.Listen(5000);<br />
while (true)<br />
{<br />
var client = socket.Accept();<br />
Console.WriteLine(“Client connected!”);<br />
client.Send(Encoding.Default.GetBytes(“Hello!”));<br />
}<br />
This all adds up and in the end you will end up with something<br />
that someone else has also written lots of times before. Therefore<br />
it is now time to present the solution of repetitive boilerplate for<br />
near real-time communication between Client and Server – a.k.a.<br />
SignalR.<br />
Creating real-time network<br />
applications with SignalR<br />
SignalR is a real-time network library that has an awesome and<br />
well-engineered abstraction over sockets. Instead of having to<br />
write all the socket code yourself, you use SignalR to fire up a<br />
server which is as simple as saying the port number out loud,<br />
a couple of times. Then you have very useful features that will<br />
let you direct messages to one client, multiple clients or to just<br />
broadcast the entire message.<br />
In SignalR, you have abstractions that will even help you to do<br />
things even more magically. This actually means that there is a<br />
“raw” way for working with SignalR and there’s a more easy way to<br />
work with clients and the server.<br />
SignalR has fall back mechanisms built in on different protocols<br />
and technologies. For instance, if you set up a server, and the<br />
client only has support for long polling; that is what is going to be<br />
used.<br />
DNcmagazine www.dotnetcurry.com | 15